]>
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 | ||
15 | #include "ansidecl.h" | |
16 | #include "libiberty.h" | |
ac424eb3 | 17 | #include "safe-ctype.h" |
e2eaf477 ILT |
18 | |
19 | #ifndef DIR_SEPARATOR | |
20 | #define DIR_SEPARATOR '/' | |
21 | #endif | |
22 | ||
23 | #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \ | |
24 | defined (__OS2__) | |
25 | #define HAVE_DOS_BASED_FILE_SYSTEM | |
26 | #ifndef DIR_SEPARATOR_2 | |
27 | #define DIR_SEPARATOR_2 '\\' | |
28 | #endif | |
29 | #endif | |
30 | ||
31 | /* Define IS_DIR_SEPARATOR. */ | |
32 | #ifndef DIR_SEPARATOR_2 | |
33 | # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) | |
34 | #else /* DIR_SEPARATOR_2 */ | |
35 | # define IS_DIR_SEPARATOR(ch) \ | |
36 | (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) | |
37 | #endif /* DIR_SEPARATOR_2 */ | |
252b5132 RH |
38 | |
39 | char * | |
9334f9c6 | 40 | basename (const char *name) |
252b5132 | 41 | { |
e2eaf477 | 42 | const char *base; |
252b5132 | 43 | |
e2eaf477 ILT |
44 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
45 | /* Skip over the disk name in MSDOS pathnames. */ | |
ac424eb3 | 46 | if (ISALPHA (name[0]) && name[1] == ':') |
e2eaf477 ILT |
47 | name += 2; |
48 | #endif | |
49 | ||
50 | for (base = name; *name; name++) | |
252b5132 | 51 | { |
e2eaf477 | 52 | if (IS_DIR_SEPARATOR (*name)) |
252b5132 | 53 | { |
e2eaf477 | 54 | base = name + 1; |
252b5132 RH |
55 | } |
56 | } | |
57 | return (char *) base; | |
58 | } | |
e2eaf477 | 59 |