1/* Return the basename of a pathname. 2 This file is in the public domain. */ 3 4/* 5 6@deftypefn Supplemental char* basename (const char *@var{name}) 7 8Returns a pointer to the last component of pathname @var{name}. 9Behavior is undefined if the pathname ends in a directory separator. 10 11@end deftypefn 12 13*/ 14 15#ifdef HAVE_CONFIG_H 16#include "config.h" 17#endif 18#include "ansidecl.h" 19#include "libiberty.h" 20#include "safe-ctype.h" 21 22#ifndef DIR_SEPARATOR 23#define DIR_SEPARATOR '/' 24#endif 25 26#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \ 27 defined (__OS2__) 28#define HAVE_DOS_BASED_FILE_SYSTEM 29#ifndef DIR_SEPARATOR_2 30#define DIR_SEPARATOR_2 '\\' 31#endif 32#endif 33 34/* Define IS_DIR_SEPARATOR. */ 35#ifndef DIR_SEPARATOR_2 36# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) 37#else /* DIR_SEPARATOR_2 */ 38# define IS_DIR_SEPARATOR(ch) \ 39 (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) 40#endif /* DIR_SEPARATOR_2 */ 41 42char * 43basename (const char *name) 44{ 45 const char *base; 46 47#if defined (HAVE_DOS_BASED_FILE_SYSTEM) 48 /* Skip over the disk name in MSDOS pathnames. */ 49 if (ISALPHA (name[0]) && name[1] == ':') 50 name += 2; 51#endif 52 53 for (base = name; *name; name++) 54 { 55 if (IS_DIR_SEPARATOR (*name)) 56 { 57 base = name + 1; 58 } 59 } 60 return (char *) base; 61} 62 63