xref: /openbsd-src/gnu/lib/libiberty/src/lbasename.c (revision 20fce977aadac3358da45d5027d7d19cdc03b0fe)
19588ddcfSespie /* Libiberty basename.  Like basename, but is not overridden by the
29588ddcfSespie    system C library.
39588ddcfSespie    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
49588ddcfSespie 
59588ddcfSespie This file is part of the libiberty library.
69588ddcfSespie Libiberty is free software; you can redistribute it and/or
79588ddcfSespie modify it under the terms of the GNU Library General Public
89588ddcfSespie License as published by the Free Software Foundation; either
99588ddcfSespie version 2 of the License, or (at your option) any later version.
109588ddcfSespie 
119588ddcfSespie Libiberty is distributed in the hope that it will be useful,
129588ddcfSespie but WITHOUT ANY WARRANTY; without even the implied warranty of
139588ddcfSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
149588ddcfSespie Library General Public License for more details.
159588ddcfSespie 
169588ddcfSespie You should have received a copy of the GNU Library General Public
179588ddcfSespie License along with libiberty; see the file COPYING.LIB.  If
18*20fce977Smiod not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*20fce977Smiod Boston, MA 02110-1301, USA.  */
209588ddcfSespie 
219588ddcfSespie /*
229588ddcfSespie 
239588ddcfSespie @deftypefn Replacement {const char*} lbasename (const char *@var{name})
249588ddcfSespie 
259588ddcfSespie Given a pointer to a string containing a typical pathname
269588ddcfSespie (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
279588ddcfSespie last component of the pathname (@samp{ls.c} in this case).  The
289588ddcfSespie returned pointer is guaranteed to lie within the original
299588ddcfSespie string.  This latter fact is not true of many vendor C
309588ddcfSespie libraries, which return special strings or modify the passed
319588ddcfSespie strings for particular input.
329588ddcfSespie 
339588ddcfSespie In particular, the empty string returns the same empty string,
349588ddcfSespie and a path ending in @code{/} returns the empty string after it.
359588ddcfSespie 
369588ddcfSespie @end deftypefn
379588ddcfSespie 
389588ddcfSespie */
399588ddcfSespie 
40*20fce977Smiod #ifdef HAVE_CONFIG_H
41*20fce977Smiod #include "config.h"
42*20fce977Smiod #endif
439588ddcfSespie #include "ansidecl.h"
449588ddcfSespie #include "libiberty.h"
459588ddcfSespie #include "safe-ctype.h"
46*20fce977Smiod #include "filenames.h"
479588ddcfSespie 
489588ddcfSespie const char *
lbasename(const char * name)49*20fce977Smiod lbasename (const char *name)
509588ddcfSespie {
519588ddcfSespie   const char *base;
529588ddcfSespie 
539588ddcfSespie #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
549588ddcfSespie   /* Skip over a possible disk name.  */
559588ddcfSespie   if (ISALPHA (name[0]) && name[1] == ':')
569588ddcfSespie     name += 2;
579588ddcfSespie #endif
589588ddcfSespie 
599588ddcfSespie   for (base = name; *name; name++)
609588ddcfSespie     if (IS_DIR_SEPARATOR (*name))
619588ddcfSespie       base = name + 1;
629588ddcfSespie 
639588ddcfSespie   return base;
649588ddcfSespie }
65