159243Sobrien/* Libiberty basename.  Like basename, but is not overridden by the
259243Sobrien   system C library.
369408Sache   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
459243Sobrien
559243SobrienThis file is part of the libiberty library.
659243SobrienLibiberty is free software; you can redistribute it and/or
759243Sobrienmodify it under the terms of the GNU Library General Public
859243SobrienLicense as published by the Free Software Foundation; either
959243Sobrienversion 2 of the License, or (at your option) any later version.
1059243Sobrien
1159243SobrienLibiberty is distributed in the hope that it will be useful,
1259243Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1359243SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1459243SobrienLibrary General Public License for more details.
1559243Sobrien
1659243SobrienYou should have received a copy of the GNU Library General Public
1759243SobrienLicense along with libiberty; see the file COPYING.LIB.  If
1859243Sobriennot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1959243SobrienBoston, MA 02110-1301, USA.  */
2059243Sobrien
2159243Sobrien/*
2259243Sobrien
2359243Sobrien@deftypefn Replacement {const char*} lbasename (const char *@var{name})
2459243Sobrien
2559243SobrienGiven a pointer to a string containing a typical pathname
2659243Sobrien(@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
2759243Sobrienlast component of the pathname (@samp{ls.c} in this case).  The
2859243Sobrienreturned pointer is guaranteed to lie within the original
2959243Sobrienstring.  This latter fact is not true of many vendor C
3059243Sobrienlibraries, which return special strings or modify the passed
3159243Sobrienstrings for particular input.
3259243Sobrien
3359243SobrienIn particular, the empty string returns the same empty string,
3459243Sobrienand a path ending in @code{/} returns the empty string after it.
3559243Sobrien
3659243Sobrien@end deftypefn
3769408Sache
3869408Sache*/
3969408Sache
4069408Sache#ifdef HAVE_CONFIG_H
4159243Sobrien#include "config.h"
4259243Sobrien#endif
4359243Sobrien#include "ansidecl.h"
4459243Sobrien#include "libiberty.h"
4559243Sobrien#include "safe-ctype.h"
4659243Sobrien#include "filenames.h"
4759243Sobrien
4859243Sobrienconst char *
4959243Sobrienlbasename (const char *name)
5059243Sobrien{
5159243Sobrien  const char *base;
5259243Sobrien
5359243Sobrien#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
5459243Sobrien  /* Skip over a possible disk name.  */
5559243Sobrien  if (ISALPHA (name[0]) && name[1] == ':')
5659243Sobrien    name += 2;
5759243Sobrien#endif
5859243Sobrien
5959243Sobrien  for (base = name; *name; name++)
6059243Sobrien    if (IS_DIR_SEPARATOR (*name))
6159243Sobrien      base = name + 1;
6259243Sobrien
6359243Sobrien  return base;
6459243Sobrien}
6559243Sobrien