]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/basename.c
MAINTAINERS (Various Maintainers): Add self.
[thirdparty/gcc.git] / libiberty / basename.c
CommitLineData
6599da04
JM
1/* Return the basename of a pathname.
2 This file is in the public domain. */
3
4/*
6599da04 5
aaa5f039 6@deftypefn Supplemental char* basename (const char *@var{name})
6599da04 7
aaa5f039
DD
8Returns a pointer to the last component of pathname @var{name}.
9Behavior is undefined if the pathname ends in a directory separator.
10
11@end deftypefn
6599da04 12
6599da04
JM
13*/
14
15#include "ansidecl.h"
16#include "libiberty.h"
f6bbde28 17#include "safe-ctype.h"
7f22ec2e
MK
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 */
6599da04 38
6599da04
JM
39char *
40basename (name)
41 const char *name;
42{
7f22ec2e 43 const char *base;
6599da04 44
7f22ec2e
MK
45#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
46 /* Skip over the disk name in MSDOS pathnames. */
f6bbde28 47 if (ISALPHA (name[0]) && name[1] == ':')
7f22ec2e
MK
48 name += 2;
49#endif
50
51 for (base = name; *name; name++)
6599da04 52 {
7f22ec2e 53 if (IS_DIR_SEPARATOR (*name))
6599da04 54 {
7f22ec2e 55 base = name + 1;
6599da04
JM
56 }
57 }
58 return (char *) base;
59}
7f22ec2e 60