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