133965Sjdp/* Return the basename of a pathname. 233965Sjdp This file is in the public domain. */ 333965Sjdp 433965Sjdp/* 533965Sjdp 689857Sobrien@deftypefn Supplemental char* basename (const char *@var{name}) 733965Sjdp 889857SobrienReturns a pointer to the last component of pathname @var{name}. 989857SobrienBehavior is undefined if the pathname ends in a directory separator. 1033965Sjdp 1189857Sobrien@end deftypefn 1289857Sobrien 1333965Sjdp*/ 1433965Sjdp 15218822Sdim#ifdef HAVE_CONFIG_H 16218822Sdim#include "config.h" 17218822Sdim#endif 1833965Sjdp#include "ansidecl.h" 1933965Sjdp#include "libiberty.h" 2077298Sobrien#include "safe-ctype.h" 2133965Sjdp 2260484Sobrien#ifndef DIR_SEPARATOR 2360484Sobrien#define DIR_SEPARATOR '/' 2460484Sobrien#endif 2533965Sjdp 2660484Sobrien#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \ 2760484Sobrien defined (__OS2__) 2860484Sobrien#define HAVE_DOS_BASED_FILE_SYSTEM 2960484Sobrien#ifndef DIR_SEPARATOR_2 3060484Sobrien#define DIR_SEPARATOR_2 '\\' 3160484Sobrien#endif 3260484Sobrien#endif 3333965Sjdp 3460484Sobrien/* Define IS_DIR_SEPARATOR. */ 3560484Sobrien#ifndef DIR_SEPARATOR_2 3660484Sobrien# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) 3760484Sobrien#else /* DIR_SEPARATOR_2 */ 3860484Sobrien# define IS_DIR_SEPARATOR(ch) \ 3960484Sobrien (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) 4060484Sobrien#endif /* DIR_SEPARATOR_2 */ 4160484Sobrien 4233965Sjdpchar * 43218822Sdimbasename (const char *name) 4433965Sjdp{ 4560484Sobrien const char *base; 4633965Sjdp 4760484Sobrien#if defined (HAVE_DOS_BASED_FILE_SYSTEM) 4860484Sobrien /* Skip over the disk name in MSDOS pathnames. */ 4977298Sobrien if (ISALPHA (name[0]) && name[1] == ':') 5060484Sobrien name += 2; 5160484Sobrien#endif 5260484Sobrien 5360484Sobrien for (base = name; *name; name++) 5433965Sjdp { 5560484Sobrien if (IS_DIR_SEPARATOR (*name)) 5633965Sjdp { 5760484Sobrien base = name + 1; 5833965Sjdp } 5933965Sjdp } 6033965Sjdp return (char *) base; 6133965Sjdp} 6233965Sjdp 63