Back to index

cell-binutils  2.17cvs20070401
basename.c
Go to the documentation of this file.
00001 /* Return the basename of a pathname.
00002    This file is in the public domain. */
00003 
00004 /*
00005 
00006 @deftypefn Supplemental char* basename (const char *@var{name})
00007 
00008 Returns a pointer to the last component of pathname @var{name}.
00009 Behavior is undefined if the pathname ends in a directory separator.
00010 
00011 @end deftypefn
00012 
00013 */
00014 
00015 #ifdef HAVE_CONFIG_H
00016 #include "config.h"
00017 #endif
00018 #include "ansidecl.h"
00019 #include "libiberty.h"
00020 #include "safe-ctype.h"
00021 
00022 #ifndef DIR_SEPARATOR
00023 #define DIR_SEPARATOR '/'
00024 #endif
00025 
00026 #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
00027   defined (__OS2__)
00028 #define HAVE_DOS_BASED_FILE_SYSTEM
00029 #ifndef DIR_SEPARATOR_2 
00030 #define DIR_SEPARATOR_2 '\\'
00031 #endif
00032 #endif
00033 
00034 /* Define IS_DIR_SEPARATOR.  */
00035 #ifndef DIR_SEPARATOR_2
00036 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
00037 #else /* DIR_SEPARATOR_2 */
00038 # define IS_DIR_SEPARATOR(ch) \
00039        (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
00040 #endif /* DIR_SEPARATOR_2 */
00041 
00042 char *
00043 basename (const char *name)
00044 {
00045   const char *base;
00046 
00047 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
00048   /* Skip over the disk name in MSDOS pathnames. */
00049   if (ISALPHA (name[0]) && name[1] == ':') 
00050     name += 2;
00051 #endif
00052 
00053   for (base = name; *name; name++)
00054     {
00055       if (IS_DIR_SEPARATOR (*name))
00056        {
00057          base = name + 1;
00058        }
00059     }
00060   return (char *) base;
00061 }
00062