diff options
author | Bruno Haible <bruno@clisp.org> | 2001-01-04 16:39:09 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2001-01-04 16:39:09 +0000 |
commit | eaf396d26252324da43cfc02edef4163af4c8f5a (patch) | |
tree | 9d1f659f740b89cf95cfe11728fc06b8844ffacb /lib/basename.c | |
parent | b5b46b39517187905714bc8d00363e5af9f207ca (diff) | |
download | external_gettext-eaf396d26252324da43cfc02edef4163af4c8f5a.zip external_gettext-eaf396d26252324da43cfc02edef4163af4c8f5a.tar.gz external_gettext-eaf396d26252324da43cfc02edef4163af4c8f5a.tar.bz2 |
Update the lib directory with new files from glibc-2.2, fetish utils etc.
Diffstat (limited to 'lib/basename.c')
-rw-r--r-- | lib/basename.c | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/lib/basename.c b/lib/basename.c index 52ecb51..db7d658 100644 --- a/lib/basename.c +++ b/lib/basename.c @@ -1,5 +1,5 @@ /* Return the name-within-directory of a file name. - Copyright (C) 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. @@ -23,7 +23,15 @@ # include <config.h> #endif -#include <string.h> +#include <assert.h> + +#ifndef FILESYSTEM_PREFIX_LEN +# define FILESYSTEM_PREFIX_LEN(Filename) 0 +#endif + +#ifndef ISSLASH +# define ISSLASH(C) ((C) == '/') +#endif #ifndef _LIBC /* We cannot generally use the name `basename' since XPG defines an unusable @@ -31,11 +39,32 @@ # define basename gnu_basename #endif +/* In general, we can't use the builtin `basename' function if available, + since it has different meanings in different environments. + In some environments the builtin `basename' modifies its argument. + If NAME is all slashes, be sure to return `/'. */ char * -basename (filename) - const char *filename; +basename (char const *name) { - char *p = strrchr (filename, '/'); - return p ? p + 1 : (char *) filename; + char const *base = name += FILESYSTEM_PREFIX_LEN (name); + int all_slashes = 1; + char const *p; + + for (p = name; *p; p++) + { + if (ISSLASH (*p)) + base = p + 1; + else + all_slashes = 0; + } + + /* If NAME is all slashes, arrange to return `/'. */ + if (*base == '\0' && ISSLASH (*name) && all_slashes) + --base; + + /* Make sure the last byte is not a slash. */ + assert (all_slashes || !ISSLASH (*(p - 1))); + + return (char *) base; } |