X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=libiberty%2Fbasename.c;h=69a982d7dca8f5795b69e361347bf84ec0ef0314;hb=05ac439abdc23f21dd7ceb8160de30879f977d39;hp=f544c853910c243cc58302176f22fcd431ff6b90;hpb=3c67ba63fe49eb98772c621befa986b0e8ec6bbc;p=pf3gnuchains%2Fgcc-fork.git diff --git a/libiberty/basename.c b/libiberty/basename.c index f544c853910..69a982d7dca 100644 --- a/libiberty/basename.c +++ b/libiberty/basename.c @@ -2,36 +2,59 @@ This file is in the public domain. */ /* -NAME - basename -- return pointer to last component of a pathname -SYNOPSIS - char *basename (const char *name) +@deftypefn Supplemental char* basename (const char *@var{name}) -DESCRIPTION - Given a pointer to a string containing a typical pathname - (/usr/src/cmd/ls/ls.c for example), returns a pointer to the - last component of the pathname ("ls.c" in this case). +Returns a pointer to the last component of pathname @var{name}. +Behavior is undefined if the pathname ends in a directory separator. + +@end deftypefn -BUGS - Presumes a UNIX style path with UNIX style separators. */ #include "ansidecl.h" #include "libiberty.h" +#include "safe-ctype.h" + +#ifndef DIR_SEPARATOR +#define DIR_SEPARATOR '/' +#endif + +#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \ + defined (__OS2__) +#define HAVE_DOS_BASED_FILE_SYSTEM +#ifndef DIR_SEPARATOR_2 +#define DIR_SEPARATOR_2 '\\' +#endif +#endif + +/* Define IS_DIR_SEPARATOR. */ +#ifndef DIR_SEPARATOR_2 +# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) +#else /* DIR_SEPARATOR_2 */ +# define IS_DIR_SEPARATOR(ch) \ + (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) +#endif /* DIR_SEPARATOR_2 */ char * basename (name) const char *name; { - const char *base = name; + const char *base; + +#if defined (HAVE_DOS_BASED_FILE_SYSTEM) + /* Skip over the disk name in MSDOS pathnames. */ + if (ISALPHA (name[0]) && name[1] == ':') + name += 2; +#endif - while (*name) + for (base = name; *name; name++) { - if (*name++ == '/') + if (IS_DIR_SEPARATOR (*name)) { - base = name; + base = name + 1; } } return (char *) base; } +