116dce513Schristos /* Libiberty basename. Like basename, but is not overridden by the
216dce513Schristos system C library.
3*e992f068Schristos Copyright (C) 2001-2022 Free Software Foundation, Inc.
416dce513Schristos
516dce513Schristos This file is part of the libiberty library.
616dce513Schristos Libiberty is free software; you can redistribute it and/or
716dce513Schristos modify it under the terms of the GNU Library General Public
816dce513Schristos License as published by the Free Software Foundation; either
916dce513Schristos version 2 of the License, or (at your option) any later version.
1016dce513Schristos
1116dce513Schristos Libiberty is distributed in the hope that it will be useful,
1216dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1316dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1416dce513Schristos Library General Public License for more details.
1516dce513Schristos
1616dce513Schristos You should have received a copy of the GNU Library General Public
1716dce513Schristos License along with libiberty; see the file COPYING.LIB. If
1816dce513Schristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1916dce513Schristos Boston, MA 02110-1301, USA. */
2016dce513Schristos
2116dce513Schristos /*
2216dce513Schristos
2316dce513Schristos @deftypefn Replacement {const char*} lbasename (const char *@var{name})
2416dce513Schristos
2516dce513Schristos Given a pointer to a string containing a typical pathname
2616dce513Schristos (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
2716dce513Schristos last component of the pathname (@samp{ls.c} in this case). The
2816dce513Schristos returned pointer is guaranteed to lie within the original
2916dce513Schristos string. This latter fact is not true of many vendor C
3016dce513Schristos libraries, which return special strings or modify the passed
3116dce513Schristos strings for particular input.
3216dce513Schristos
3316dce513Schristos In particular, the empty string returns the same empty string,
3416dce513Schristos and a path ending in @code{/} returns the empty string after it.
3516dce513Schristos
3616dce513Schristos @end deftypefn
3716dce513Schristos
3816dce513Schristos */
3916dce513Schristos
4016dce513Schristos #ifdef HAVE_CONFIG_H
4116dce513Schristos #include "config.h"
4216dce513Schristos #endif
4316dce513Schristos #include "ansidecl.h"
4416dce513Schristos #include "libiberty.h"
4516dce513Schristos #include "safe-ctype.h"
4616dce513Schristos #include "filenames.h"
4716dce513Schristos
4816dce513Schristos const char *
unix_lbasename(const char * name)4916dce513Schristos unix_lbasename (const char *name)
5016dce513Schristos {
5116dce513Schristos const char *base;
5216dce513Schristos
5316dce513Schristos for (base = name; *name; name++)
5416dce513Schristos if (IS_UNIX_DIR_SEPARATOR (*name))
5516dce513Schristos base = name + 1;
5616dce513Schristos
5716dce513Schristos return base;
5816dce513Schristos }
5916dce513Schristos
6016dce513Schristos const char *
dos_lbasename(const char * name)6116dce513Schristos dos_lbasename (const char *name)
6216dce513Schristos {
6316dce513Schristos const char *base;
6416dce513Schristos
6516dce513Schristos /* Skip over a possible disk name. */
6616dce513Schristos if (ISALPHA (name[0]) && name[1] == ':')
6716dce513Schristos name += 2;
6816dce513Schristos
6916dce513Schristos for (base = name; *name; name++)
7016dce513Schristos if (IS_DOS_DIR_SEPARATOR (*name))
7116dce513Schristos base = name + 1;
7216dce513Schristos
7316dce513Schristos return base;
7416dce513Schristos }
7516dce513Schristos
7616dce513Schristos const char *
lbasename(const char * name)7716dce513Schristos lbasename (const char *name)
7816dce513Schristos {
7916dce513Schristos #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
8016dce513Schristos return dos_lbasename (name);
8116dce513Schristos #else
8216dce513Schristos return unix_lbasename (name);
8316dce513Schristos #endif
8416dce513Schristos }
85