180016Sobrien/* Libiberty basename.  Like basename, but is not overridden by the
280016Sobrien   system C library.
3104834Sobrien   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
480016Sobrien
580016SobrienThis file is part of the libiberty library.
680016SobrienLibiberty is free software; you can redistribute it and/or
780016Sobrienmodify it under the terms of the GNU Library General Public
880016SobrienLicense as published by the Free Software Foundation; either
980016Sobrienversion 2 of the License, or (at your option) any later version.
1080016Sobrien
1180016SobrienLibiberty is distributed in the hope that it will be useful,
1280016Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1380016SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1480016SobrienLibrary General Public License for more details.
1580016Sobrien
1680016SobrienYou should have received a copy of the GNU Library General Public
1780016SobrienLicense along with libiberty; see the file COPYING.LIB.  If
18218822Sdimnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19218822SdimBoston, MA 02110-1301, USA.  */
2080016Sobrien
2180016Sobrien/*
2280016Sobrien
2389857Sobrien@deftypefn Replacement {const char*} lbasename (const char *@var{name})
2480016Sobrien
2589857SobrienGiven a pointer to a string containing a typical pathname
2689857Sobrien(@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
2789857Sobrienlast component of the pathname (@samp{ls.c} in this case).  The
2889857Sobrienreturned pointer is guaranteed to lie within the original
2989857Sobrienstring.  This latter fact is not true of many vendor C
3089857Sobrienlibraries, which return special strings or modify the passed
3189857Sobrienstrings for particular input.
3280016Sobrien
3389857SobrienIn particular, the empty string returns the same empty string,
3489857Sobrienand a path ending in @code{/} returns the empty string after it.
3589857Sobrien
3689857Sobrien@end deftypefn
3789857Sobrien
3880016Sobrien*/
3980016Sobrien
40218822Sdim#ifdef HAVE_CONFIG_H
41218822Sdim#include "config.h"
42218822Sdim#endif
4380016Sobrien#include "ansidecl.h"
4480016Sobrien#include "libiberty.h"
4580016Sobrien#include "safe-ctype.h"
46130561Sobrien#include "filenames.h"
4780016Sobrien
4889857Sobrienconst char *
49218822Sdimlbasename (const char *name)
5080016Sobrien{
5180016Sobrien  const char *base;
5280016Sobrien
5380016Sobrien#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
5480016Sobrien  /* Skip over a possible disk name.  */
5580016Sobrien  if (ISALPHA (name[0]) && name[1] == ':')
5680016Sobrien    name += 2;
5780016Sobrien#endif
5880016Sobrien
5980016Sobrien  for (base = name; *name; name++)
6080016Sobrien    if (IS_DIR_SEPARATOR (*name))
6180016Sobrien      base = name + 1;
6280016Sobrien
6389857Sobrien  return base;
6480016Sobrien}
65