binutils-gdb/libiberty/basename.c

63 lines
1.3 KiB
C
Raw Normal View History

1999-05-03 15:29:11 +08:00
/* Return the basename of a pathname.
This file is in the public domain. */
/*
2001-09-27 02:45:50 +08:00
@deftypefn Supplemental char* basename (const char *@var{name})
1999-05-03 15:29:11 +08:00
2001-09-27 02:45:50 +08:00
Returns a pointer to the last component of pathname @var{name}.
Behavior is undefined if the pathname ends in a directory separator.
@end deftypefn
1999-05-03 15:29:11 +08:00
*/
2005-04-16 09:05:05 +08:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-05-03 15:29:11 +08:00
#include "ansidecl.h"
#include "libiberty.h"
#include "safe-ctype.h"
2000-02-22 23:59:20 +08:00
#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif
#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
defined (__OS2__)
#define HAVE_DOS_BASED_FILE_SYSTEM
#ifndef DIR_SEPARATOR_2
#define DIR_SEPARATOR_2 '\\'
#endif
#endif
/* Define IS_DIR_SEPARATOR. */
#ifndef DIR_SEPARATOR_2
# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
#else /* DIR_SEPARATOR_2 */
# define IS_DIR_SEPARATOR(ch) \
(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
#endif /* DIR_SEPARATOR_2 */
1999-05-03 15:29:11 +08:00
char *
2005-03-27 13:28:42 +08:00
basename (const char *name)
1999-05-03 15:29:11 +08:00
{
2000-02-22 23:59:20 +08:00
const char *base;
1999-05-03 15:29:11 +08:00
2000-02-22 23:59:20 +08:00
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
/* Skip over the disk name in MSDOS pathnames. */
if (ISALPHA (name[0]) && name[1] == ':')
2000-02-22 23:59:20 +08:00
name += 2;
#endif
for (base = name; *name; name++)
1999-05-03 15:29:11 +08:00
{
2000-02-22 23:59:20 +08:00
if (IS_DIR_SEPARATOR (*name))
1999-05-03 15:29:11 +08:00
{
2000-02-22 23:59:20 +08:00
base = name + 1;
1999-05-03 15:29:11 +08:00
}
}
return (char *) base;
}
2000-02-22 23:59:20 +08:00