xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/lbasename.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* Libiberty basename.  Like basename, but is not overridden by the
22a6b7db3Sskrll    system C library.
3*cb63e24eSchristos    Copyright (C) 2001-2024 Free Software Foundation, Inc.
42a6b7db3Sskrll 
52a6b7db3Sskrll This file is part of the libiberty library.
62a6b7db3Sskrll Libiberty is free software; you can redistribute it and/or
72a6b7db3Sskrll modify it under the terms of the GNU Library General Public
82a6b7db3Sskrll License as published by the Free Software Foundation; either
92a6b7db3Sskrll version 2 of the License, or (at your option) any later version.
102a6b7db3Sskrll 
112a6b7db3Sskrll Libiberty is distributed in the hope that it will be useful,
122a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
132a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
142a6b7db3Sskrll Library General Public License for more details.
152a6b7db3Sskrll 
162a6b7db3Sskrll You should have received a copy of the GNU Library General Public
172a6b7db3Sskrll License along with libiberty; see the file COPYING.LIB.  If
182a6b7db3Sskrll not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
192a6b7db3Sskrll Boston, MA 02110-1301, USA.  */
202a6b7db3Sskrll 
212a6b7db3Sskrll /*
222a6b7db3Sskrll 
232a6b7db3Sskrll @deftypefn Replacement {const char*} lbasename (const char *@var{name})
242a6b7db3Sskrll 
252a6b7db3Sskrll Given a pointer to a string containing a typical pathname
262a6b7db3Sskrll (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
272a6b7db3Sskrll last component of the pathname (@samp{ls.c} in this case).  The
282a6b7db3Sskrll returned pointer is guaranteed to lie within the original
292a6b7db3Sskrll string.  This latter fact is not true of many vendor C
302a6b7db3Sskrll libraries, which return special strings or modify the passed
312a6b7db3Sskrll strings for particular input.
322a6b7db3Sskrll 
332a6b7db3Sskrll In particular, the empty string returns the same empty string,
342a6b7db3Sskrll and a path ending in @code{/} returns the empty string after it.
352a6b7db3Sskrll 
362a6b7db3Sskrll @end deftypefn
372a6b7db3Sskrll 
382a6b7db3Sskrll */
392a6b7db3Sskrll 
402a6b7db3Sskrll #ifdef HAVE_CONFIG_H
412a6b7db3Sskrll #include "config.h"
422a6b7db3Sskrll #endif
432a6b7db3Sskrll #include "ansidecl.h"
442a6b7db3Sskrll #include "libiberty.h"
452a6b7db3Sskrll #include "safe-ctype.h"
462a6b7db3Sskrll #include "filenames.h"
472a6b7db3Sskrll 
482a6b7db3Sskrll const char *
unix_lbasename(const char * name)49be9ac0eaSchristos unix_lbasename (const char *name)
502a6b7db3Sskrll {
512a6b7db3Sskrll   const char *base;
522a6b7db3Sskrll 
532a6b7db3Sskrll   for (base = name; *name; name++)
54be9ac0eaSchristos     if (IS_UNIX_DIR_SEPARATOR (*name))
552a6b7db3Sskrll       base = name + 1;
562a6b7db3Sskrll 
572a6b7db3Sskrll   return base;
582a6b7db3Sskrll }
59be9ac0eaSchristos 
60be9ac0eaSchristos const char *
dos_lbasename(const char * name)61be9ac0eaSchristos dos_lbasename (const char *name)
62be9ac0eaSchristos {
63be9ac0eaSchristos   const char *base;
64be9ac0eaSchristos 
65be9ac0eaSchristos   /* Skip over a possible disk name.  */
66be9ac0eaSchristos   if (ISALPHA (name[0]) && name[1] == ':')
67be9ac0eaSchristos     name += 2;
68be9ac0eaSchristos 
69be9ac0eaSchristos   for (base = name; *name; name++)
70be9ac0eaSchristos     if (IS_DOS_DIR_SEPARATOR (*name))
71be9ac0eaSchristos       base = name + 1;
72be9ac0eaSchristos 
73be9ac0eaSchristos   return base;
74be9ac0eaSchristos }
75be9ac0eaSchristos 
76be9ac0eaSchristos const char *
lbasename(const char * name)77be9ac0eaSchristos lbasename (const char *name)
78be9ac0eaSchristos {
79be9ac0eaSchristos #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
80be9ac0eaSchristos   return dos_lbasename (name);
81be9ac0eaSchristos #else
82be9ac0eaSchristos   return unix_lbasename (name);
83be9ac0eaSchristos #endif
84be9ac0eaSchristos }
85