1169695Skan/* Libiberty basename.  Like basename, but is not overridden by the
2169695Skan   system C library.
3169695Skan   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4169695Skan
5169695SkanThis file is part of the libiberty library.
6169695SkanLibiberty is free software; you can redistribute it and/or
7169695Skanmodify it under the terms of the GNU Library General Public
8169695SkanLicense as published by the Free Software Foundation; either
9169695Skanversion 2 of the License, or (at your option) any later version.
10169695Skan
11169695SkanLibiberty is distributed in the hope that it will be useful,
12169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
13169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14169695SkanLibrary General Public License for more details.
15169695Skan
16169695SkanYou should have received a copy of the GNU Library General Public
17169695SkanLicense along with libiberty; see the file COPYING.LIB.  If
18169695Skannot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19169695SkanBoston, MA 02110-1301, USA.  */
20169695Skan
21169695Skan/*
22169695Skan
23169695Skan@deftypefn Replacement {const char*} lbasename (const char *@var{name})
24169695Skan
25169695SkanGiven a pointer to a string containing a typical pathname
26169695Skan(@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
27169695Skanlast component of the pathname (@samp{ls.c} in this case).  The
28169695Skanreturned pointer is guaranteed to lie within the original
29169695Skanstring.  This latter fact is not true of many vendor C
30169695Skanlibraries, which return special strings or modify the passed
31169695Skanstrings for particular input.
32169695Skan
33169695SkanIn particular, the empty string returns the same empty string,
34169695Skanand a path ending in @code{/} returns the empty string after it.
35169695Skan
36169695Skan@end deftypefn
37169695Skan
38169695Skan*/
39169695Skan
40169695Skan#ifdef HAVE_CONFIG_H
41169695Skan#include "config.h"
42169695Skan#endif
43169695Skan#include "ansidecl.h"
44169695Skan#include "libiberty.h"
45169695Skan#include "safe-ctype.h"
46169695Skan#include "filenames.h"
47169695Skan
48169695Skanconst char *
49169695Skanlbasename (const char *name)
50169695Skan{
51169695Skan  const char *base;
52169695Skan
53169695Skan#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
54169695Skan  /* Skip over a possible disk name.  */
55169695Skan  if (ISALPHA (name[0]) && name[1] == ':')
56169695Skan    name += 2;
57169695Skan#endif
58169695Skan
59169695Skan  for (base = name; *name; name++)
60169695Skan    if (IS_DIR_SEPARATOR (*name))
61169695Skan      base = name + 1;
62169695Skan
63169695Skan  return base;
64169695Skan}
65