12a6b7db3Sskrll /* Libiberty basename.  Like basename, but is not overridden by the
22a6b7db3Sskrll    system C library.
3*f22f0ef4Schristos    Copyright (C) 2001-2022 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)49b3ac4aedSchristos unix_lbasename (const char *name)
502a6b7db3Sskrll {
512a6b7db3Sskrll   const char *base;
522a6b7db3Sskrll 
532a6b7db3Sskrll   for (base = name; *name; name++)
54b3ac4aedSchristos     if (IS_UNIX_DIR_SEPARATOR (*name))
552a6b7db3Sskrll       base = name + 1;
562a6b7db3Sskrll 
572a6b7db3Sskrll   return base;
582a6b7db3Sskrll }
59b3ac4aedSchristos 
60b3ac4aedSchristos const char *
dos_lbasename(const char * name)61b3ac4aedSchristos dos_lbasename (const char *name)
62b3ac4aedSchristos {
63b3ac4aedSchristos   const char *base;
64b3ac4aedSchristos 
65b3ac4aedSchristos   /* Skip over a possible disk name.  */
66b3ac4aedSchristos   if (ISALPHA (name[0]) && name[1] == ':')
67b3ac4aedSchristos     name += 2;
68b3ac4aedSchristos 
69b3ac4aedSchristos   for (base = name; *name; name++)
70b3ac4aedSchristos     if (IS_DOS_DIR_SEPARATOR (*name))
71b3ac4aedSchristos       base = name + 1;
72b3ac4aedSchristos 
73b3ac4aedSchristos   return base;
74b3ac4aedSchristos }
75b3ac4aedSchristos 
76b3ac4aedSchristos const char *
lbasename(const char * name)77b3ac4aedSchristos lbasename (const char *name)
78b3ac4aedSchristos {
79b3ac4aedSchristos #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
80b3ac4aedSchristos   return dos_lbasename (name);
81b3ac4aedSchristos #else
82b3ac4aedSchristos   return unix_lbasename (name);
83b3ac4aedSchristos #endif
84b3ac4aedSchristos }
85