Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Return the basename of a pathname. |
2 | This file is in the public domain. */ | |
3 | ||
4 | /* | |
5 | NAME | |
6 | basename -- return pointer to last component of a pathname | |
7 | ||
8 | SYNOPSIS | |
9 | char *basename (const char *name) | |
10 | ||
11 | DESCRIPTION | |
12 | Given a pointer to a string containing a typical pathname | |
13 | (/usr/src/cmd/ls/ls.c for example), returns a pointer to the | |
14 | last component of the pathname ("ls.c" in this case). | |
15 | ||
16 | BUGS | |
e2eaf477 ILT |
17 | Presumes a UNIX or DOS/Windows style path with UNIX or DOS/Windows |
18 | style separators. | |
252b5132 RH |
19 | */ |
20 | ||
21 | #include "ansidecl.h" | |
22 | #include "libiberty.h" | |
ac424eb3 | 23 | #include "safe-ctype.h" |
e2eaf477 ILT |
24 | |
25 | #ifndef DIR_SEPARATOR | |
26 | #define DIR_SEPARATOR '/' | |
27 | #endif | |
28 | ||
29 | #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \ | |
30 | defined (__OS2__) | |
31 | #define HAVE_DOS_BASED_FILE_SYSTEM | |
32 | #ifndef DIR_SEPARATOR_2 | |
33 | #define DIR_SEPARATOR_2 '\\' | |
34 | #endif | |
35 | #endif | |
36 | ||
37 | /* Define IS_DIR_SEPARATOR. */ | |
38 | #ifndef DIR_SEPARATOR_2 | |
39 | # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) | |
40 | #else /* DIR_SEPARATOR_2 */ | |
41 | # define IS_DIR_SEPARATOR(ch) \ | |
42 | (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) | |
43 | #endif /* DIR_SEPARATOR_2 */ | |
252b5132 RH |
44 | |
45 | char * | |
46 | basename (name) | |
47 | const char *name; | |
48 | { | |
e2eaf477 | 49 | const char *base; |
252b5132 | 50 | |
e2eaf477 ILT |
51 | #if defined (HAVE_DOS_BASED_FILE_SYSTEM) |
52 | /* Skip over the disk name in MSDOS pathnames. */ | |
ac424eb3 | 53 | if (ISALPHA (name[0]) && name[1] == ':') |
e2eaf477 ILT |
54 | name += 2; |
55 | #endif | |
56 | ||
57 | for (base = name; *name; name++) | |
252b5132 | 58 | { |
e2eaf477 | 59 | if (IS_DIR_SEPARATOR (*name)) |
252b5132 | 60 | { |
e2eaf477 | 61 | base = name + 1; |
252b5132 RH |
62 | } |
63 | } | |
64 | return (char *) base; | |
65 | } | |
e2eaf477 | 66 |