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