xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/lbasename.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
198b9484cSchristos /* Libiberty basename.  Like basename, but is not overridden by the
298b9484cSchristos    system C library.
3*5173eb0aSchristos    Copyright (C) 2001-2024 Free Software Foundation, Inc.
498b9484cSchristos 
598b9484cSchristos This file is part of the libiberty library.
698b9484cSchristos Libiberty is free software; you can redistribute it and/or
798b9484cSchristos modify it under the terms of the GNU Library General Public
898b9484cSchristos License as published by the Free Software Foundation; either
998b9484cSchristos version 2 of the License, or (at your option) any later version.
1098b9484cSchristos 
1198b9484cSchristos Libiberty is distributed in the hope that it will be useful,
1298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1498b9484cSchristos Library General Public License for more details.
1598b9484cSchristos 
1698b9484cSchristos You should have received a copy of the GNU Library General Public
1798b9484cSchristos License along with libiberty; see the file COPYING.LIB.  If
1898b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1998b9484cSchristos Boston, MA 02110-1301, USA.  */
2098b9484cSchristos 
2198b9484cSchristos /*
2298b9484cSchristos 
2398b9484cSchristos @deftypefn Replacement {const char*} lbasename (const char *@var{name})
2498b9484cSchristos 
2598b9484cSchristos Given a pointer to a string containing a typical pathname
2698b9484cSchristos (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
2798b9484cSchristos last component of the pathname (@samp{ls.c} in this case).  The
2898b9484cSchristos returned pointer is guaranteed to lie within the original
2998b9484cSchristos string.  This latter fact is not true of many vendor C
3098b9484cSchristos libraries, which return special strings or modify the passed
3198b9484cSchristos strings for particular input.
3298b9484cSchristos 
3398b9484cSchristos In particular, the empty string returns the same empty string,
3498b9484cSchristos and a path ending in @code{/} returns the empty string after it.
3598b9484cSchristos 
3698b9484cSchristos @end deftypefn
3798b9484cSchristos 
3898b9484cSchristos */
3998b9484cSchristos 
4098b9484cSchristos #ifdef HAVE_CONFIG_H
4198b9484cSchristos #include "config.h"
4298b9484cSchristos #endif
4398b9484cSchristos #include "ansidecl.h"
4498b9484cSchristos #include "libiberty.h"
4598b9484cSchristos #include "safe-ctype.h"
4698b9484cSchristos #include "filenames.h"
4798b9484cSchristos 
4898b9484cSchristos const char *
4998b9484cSchristos unix_lbasename (const char *name)
5098b9484cSchristos {
5198b9484cSchristos   const char *base;
5298b9484cSchristos 
5398b9484cSchristos   for (base = name; *name; name++)
5498b9484cSchristos     if (IS_UNIX_DIR_SEPARATOR (*name))
5598b9484cSchristos       base = name + 1;
5698b9484cSchristos 
5798b9484cSchristos   return base;
5898b9484cSchristos }
5998b9484cSchristos 
6098b9484cSchristos const char *
6198b9484cSchristos dos_lbasename (const char *name)
6298b9484cSchristos {
6398b9484cSchristos   const char *base;
6498b9484cSchristos 
6598b9484cSchristos   /* Skip over a possible disk name.  */
6698b9484cSchristos   if (ISALPHA (name[0]) && name[1] == ':')
6798b9484cSchristos     name += 2;
6898b9484cSchristos 
6998b9484cSchristos   for (base = name; *name; name++)
7098b9484cSchristos     if (IS_DOS_DIR_SEPARATOR (*name))
7198b9484cSchristos       base = name + 1;
7298b9484cSchristos 
7398b9484cSchristos   return base;
7498b9484cSchristos }
7598b9484cSchristos 
7698b9484cSchristos const char *
7798b9484cSchristos lbasename (const char *name)
7898b9484cSchristos {
7998b9484cSchristos #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
8098b9484cSchristos   return dos_lbasename (name);
8198b9484cSchristos #else
8298b9484cSchristos   return unix_lbasename (name);
8398b9484cSchristos #endif
8498b9484cSchristos }
85