lbasename.c revision 1.1.1.8 1 1.1 mrg /* Libiberty basename. Like basename, but is not overridden by the
2 1.1 mrg system C library.
3 1.1.1.8 mrg Copyright (C) 2001-2020 Free Software Foundation, Inc.
4 1.1 mrg
5 1.1 mrg This file is part of the libiberty library.
6 1.1 mrg Libiberty is free software; you can redistribute it and/or
7 1.1 mrg modify it under the terms of the GNU Library General Public
8 1.1 mrg License as published by the Free Software Foundation; either
9 1.1 mrg version 2 of the License, or (at your option) any later version.
10 1.1 mrg
11 1.1 mrg Libiberty is distributed in the hope that it will be useful,
12 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 1.1 mrg Library General Public License for more details.
15 1.1 mrg
16 1.1 mrg You should have received a copy of the GNU Library General Public
17 1.1 mrg License along with libiberty; see the file COPYING.LIB. If
18 1.1 mrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 1.1 mrg Boston, MA 02110-1301, USA. */
20 1.1 mrg
21 1.1 mrg /*
22 1.1 mrg
23 1.1 mrg @deftypefn Replacement {const char*} lbasename (const char *@var{name})
24 1.1 mrg
25 1.1 mrg Given a pointer to a string containing a typical pathname
26 1.1 mrg (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
27 1.1 mrg last component of the pathname (@samp{ls.c} in this case). The
28 1.1 mrg returned pointer is guaranteed to lie within the original
29 1.1 mrg string. This latter fact is not true of many vendor C
30 1.1 mrg libraries, which return special strings or modify the passed
31 1.1 mrg strings for particular input.
32 1.1 mrg
33 1.1 mrg In particular, the empty string returns the same empty string,
34 1.1 mrg and a path ending in @code{/} returns the empty string after it.
35 1.1 mrg
36 1.1 mrg @end deftypefn
37 1.1 mrg
38 1.1 mrg */
39 1.1 mrg
40 1.1 mrg #ifdef HAVE_CONFIG_H
41 1.1 mrg #include "config.h"
42 1.1 mrg #endif
43 1.1 mrg #include "ansidecl.h"
44 1.1 mrg #include "libiberty.h"
45 1.1 mrg #include "safe-ctype.h"
46 1.1 mrg #include "filenames.h"
47 1.1 mrg
48 1.1 mrg const char *
49 1.1.1.2 mrg unix_lbasename (const char *name)
50 1.1.1.2 mrg {
51 1.1.1.2 mrg const char *base;
52 1.1.1.2 mrg
53 1.1.1.2 mrg for (base = name; *name; name++)
54 1.1.1.2 mrg if (IS_UNIX_DIR_SEPARATOR (*name))
55 1.1.1.2 mrg base = name + 1;
56 1.1.1.2 mrg
57 1.1.1.2 mrg return base;
58 1.1.1.2 mrg }
59 1.1.1.2 mrg
60 1.1.1.2 mrg const char *
61 1.1.1.2 mrg dos_lbasename (const char *name)
62 1.1 mrg {
63 1.1 mrg const char *base;
64 1.1 mrg
65 1.1 mrg /* Skip over a possible disk name. */
66 1.1 mrg if (ISALPHA (name[0]) && name[1] == ':')
67 1.1 mrg name += 2;
68 1.1 mrg
69 1.1 mrg for (base = name; *name; name++)
70 1.1.1.2 mrg if (IS_DOS_DIR_SEPARATOR (*name))
71 1.1 mrg base = name + 1;
72 1.1 mrg
73 1.1 mrg return base;
74 1.1 mrg }
75 1.1.1.2 mrg
76 1.1.1.2 mrg const char *
77 1.1.1.2 mrg lbasename (const char *name)
78 1.1.1.2 mrg {
79 1.1.1.2 mrg #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
80 1.1.1.2 mrg return dos_lbasename (name);
81 1.1.1.2 mrg #else
82 1.1.1.2 mrg return unix_lbasename (name);
83 1.1.1.2 mrg #endif
84 1.1.1.2 mrg }
85