xref: /openbsd-src/gnu/lib/libiberty/src/basename.c (revision 150b7e42cfa21e6546d96ae514ca23e80d970ac7)
100bf4279Sespie /* Return the basename of a pathname.
200bf4279Sespie    This file is in the public domain. */
300bf4279Sespie 
400bf4279Sespie /*
500bf4279Sespie 
637c53322Sespie @deftypefn Supplemental char* basename (const char *@var{name})
700bf4279Sespie 
837c53322Sespie Returns a pointer to the last component of pathname @var{name}.
937c53322Sespie Behavior is undefined if the pathname ends in a directory separator.
1000bf4279Sespie 
1137c53322Sespie @end deftypefn
1237c53322Sespie 
1300bf4279Sespie */
1400bf4279Sespie 
15*150b7e42Smiod #ifdef HAVE_CONFIG_H
16*150b7e42Smiod #include "config.h"
17*150b7e42Smiod #endif
1800bf4279Sespie #include "ansidecl.h"
1900bf4279Sespie #include "libiberty.h"
20f5dd06f4Sespie #include "safe-ctype.h"
2100bf4279Sespie 
2200bf4279Sespie #ifndef DIR_SEPARATOR
2300bf4279Sespie #define DIR_SEPARATOR '/'
2400bf4279Sespie #endif
2500bf4279Sespie 
2600bf4279Sespie #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
2700bf4279Sespie   defined (__OS2__)
2800bf4279Sespie #define HAVE_DOS_BASED_FILE_SYSTEM
2900bf4279Sespie #ifndef DIR_SEPARATOR_2
3000bf4279Sespie #define DIR_SEPARATOR_2 '\\'
3100bf4279Sespie #endif
3200bf4279Sespie #endif
3300bf4279Sespie 
3400bf4279Sespie /* Define IS_DIR_SEPARATOR.  */
3500bf4279Sespie #ifndef DIR_SEPARATOR_2
3600bf4279Sespie # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
3700bf4279Sespie #else /* DIR_SEPARATOR_2 */
3800bf4279Sespie # define IS_DIR_SEPARATOR(ch) \
3900bf4279Sespie 	(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
4000bf4279Sespie #endif /* DIR_SEPARATOR_2 */
4100bf4279Sespie 
4200bf4279Sespie char *
basename(const char * name)43*150b7e42Smiod basename (const char *name)
4400bf4279Sespie {
4500bf4279Sespie   const char *base;
4600bf4279Sespie 
4700bf4279Sespie #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
4800bf4279Sespie   /* Skip over the disk name in MSDOS pathnames. */
49f5dd06f4Sespie   if (ISALPHA (name[0]) && name[1] == ':')
5000bf4279Sespie     name += 2;
5100bf4279Sespie #endif
5200bf4279Sespie 
5300bf4279Sespie   for (base = name; *name; name++)
5400bf4279Sespie     {
5500bf4279Sespie       if (IS_DIR_SEPARATOR (*name))
5600bf4279Sespie 	{
5700bf4279Sespie 	  base = name + 1;
5800bf4279Sespie 	}
5900bf4279Sespie     }
6000bf4279Sespie   return (char *) base;
6100bf4279Sespie }
6200bf4279Sespie 
63