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