merge from gcc
[binutils.git] / libiberty / basename.c
blob0f2c069f0ccf5a7d91e4913548e068c247e12efb
1 /* Return the basename of a pathname.
2 This file is in the public domain. */
4 /*
6 @deftypefn Supplemental char* basename (const char *@var{name})
8 Returns a pointer to the last component of pathname @var{name}.
9 Behavior is undefined if the pathname ends in a directory separator.
11 @end deftypefn
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 #include "ansidecl.h"
19 #include "libiberty.h"
20 #include "safe-ctype.h"
22 #ifndef DIR_SEPARATOR
23 #define DIR_SEPARATOR '/'
24 #endif
26 #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
27 defined (__OS2__)
28 #define HAVE_DOS_BASED_FILE_SYSTEM
29 #ifndef DIR_SEPARATOR_2
30 #define DIR_SEPARATOR_2 '\\'
31 #endif
32 #endif
34 /* Define IS_DIR_SEPARATOR. */
35 #ifndef DIR_SEPARATOR_2
36 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
37 #else /* DIR_SEPARATOR_2 */
38 # define IS_DIR_SEPARATOR(ch) \
39 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
40 #endif /* DIR_SEPARATOR_2 */
42 char *
43 basename (const char *name)
45 const char *base;
47 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
48 /* Skip over the disk name in MSDOS pathnames. */
49 if (ISALPHA (name[0]) && name[1] == ':')
50 name += 2;
51 #endif
53 for (base = name; *name; name++)
55 if (IS_DIR_SEPARATOR (*name))
57 base = name + 1;
60 return (char *) base;