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