Back to index

cell-binutils  2.17cvs20070401
lbasename.c
Go to the documentation of this file.
00001 /* Libiberty basename.  Like basename, but is not overridden by the
00002    system C library.
00003    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00004 
00005 This file is part of the libiberty library.
00006 Libiberty is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Library General Public
00008 License as published by the Free Software Foundation; either
00009 version 2 of the License, or (at your option) any later version.
00010 
00011 Libiberty is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Library General Public License for more details.
00015 
00016 You should have received a copy of the GNU Library General Public
00017 License along with libiberty; see the file COPYING.LIB.  If
00018 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019 Boston, MA 02110-1301, USA.  */
00020 
00021 /*
00022 
00023 @deftypefn Replacement {const char*} lbasename (const char *@var{name})
00024 
00025 Given a pointer to a string containing a typical pathname
00026 (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
00027 last component of the pathname (@samp{ls.c} in this case).  The
00028 returned pointer is guaranteed to lie within the original
00029 string.  This latter fact is not true of many vendor C
00030 libraries, which return special strings or modify the passed
00031 strings for particular input.
00032 
00033 In particular, the empty string returns the same empty string,
00034 and a path ending in @code{/} returns the empty string after it.
00035 
00036 @end deftypefn
00037 
00038 */
00039 
00040 #ifdef HAVE_CONFIG_H
00041 #include "config.h"
00042 #endif
00043 #include "ansidecl.h"
00044 #include "libiberty.h"
00045 #include "safe-ctype.h"
00046 #include "filenames.h"
00047 
00048 const char *
00049 lbasename (const char *name)
00050 {
00051   const char *base;
00052 
00053 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
00054   /* Skip over a possible disk name.  */
00055   if (ISALPHA (name[0]) && name[1] == ':') 
00056     name += 2;
00057 #endif
00058 
00059   for (base = name; *name; name++)
00060     if (IS_DIR_SEPARATOR (*name))
00061       base = name + 1;
00062 
00063   return base;
00064 }