X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=libiberty%2Fbasename.c;h=0f2c069f0ccf5a7d91e4913548e068c247e12efb;hb=ee959deb576c46b85b6174626d700e1393256adf;hp=f544c853910c243cc58302176f22fcd431ff6b90;hpb=5b64ad42d36e6d487e1f7287d37fbc243a178e72;p=platform%2Fupstream%2Fbinutils.git diff --git a/libiberty/basename.c b/libiberty/basename.c index f544c85..0f2c069 100644 --- a/libiberty/basename.c +++ b/libiberty/basename.c @@ -2,36 +2,61 @@ 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. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #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; +basename (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; } +