Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Return the basename of a pathname. |
2 | This file is in the public domain. */ | |
3 | ||
4 | /* | |
252b5132 | 5 | |
39423523 | 6 | @deftypefn Supplemental char* basename (const char *@var{name}) |
252b5132 | 7 | |
39423523 DD |
8 | Returns a pointer to the last component of pathname @var{name}. |
9 | Behavior is undefined if the pathname ends in a directory separator. | |
10 | ||
11 | @end deftypefn | |
252b5132 | 12 | |
252b5132 RH |
13 | */ |
14 | ||
fa99459d DD |
15 | #ifdef HAVE_CONFIG_H |
16 | #include "config.h" | |
17 | #endif | |
252b5132 RH |
18 | #include "ansidecl.h" |
19 | #include "libiberty.h" | |
ac424eb3 | 20 | #include "safe-ctype.h" |
e2eaf477 ILT |
21 | |
22 | #ifndef DIR_SEPARATOR | |
23 | #define DIR_SEPARATOR '/' | |
24 | #endif | |
25 | ||
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 | |
33 | ||
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 */ | |
252b5132 RH |
41 | |
42 | char * | |
9334f9c6 | 43 | basename (const char *name) |
252b5132 | 44 | { |
e2eaf477 | 45 | const char *base; |
252b5132 | 46 | |
e2eaf477 ILT |
47 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
48 | /* Skip over the disk name in MSDOS pathnames. */ | |
ac424eb3 | 49 | if (ISALPHA (name[0]) && name[1] == ':') |
e2eaf477 ILT |
50 | name += 2; |
51 | #endif | |
52 | ||
53 | for (base = name; *name; name++) | |
252b5132 | 54 | { |
e2eaf477 | 55 | if (IS_DIR_SEPARATOR (*name)) |
252b5132 | 56 | { |
e2eaf477 | 57 | base = name + 1; |
252b5132 RH |
58 | } |
59 | } | |
60 | return (char *) base; | |
61 | } | |
e2eaf477 | 62 |