binutils-gdb/libiberty/basename.c

67 lines
1.4 KiB
C
Raw Normal View History

1999-05-03 09:29:11 +02:00
/* Return the basename of a pathname.
This file is in the public domain. */
/*
NAME
basename -- return pointer to last component of a pathname
SYNOPSIS
char *basename (const char *name)
DESCRIPTION
Given a pointer to a string containing a typical pathname
(/usr/src/cmd/ls/ls.c for example), returns a pointer to the
last component of the pathname ("ls.c" in this case).
BUGS
2000-02-22 16:59:20 +01:00
Presumes a UNIX or DOS/Windows style path with UNIX or DOS/Windows
style separators.
1999-05-03 09:29:11 +02:00
*/
#include "ansidecl.h"
#include "libiberty.h"
2000-02-22 16:59:20 +01:00
#include <ctype.h>
#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 09:29:11 +02:00
char *
basename (name)
const char *name;
{
2000-02-22 16:59:20 +01:00
const char *base;
1999-05-03 09:29:11 +02:00
2000-02-22 16:59:20 +01:00
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
/* Skip over the disk name in MSDOS pathnames. */
if (isalpha (name[0]) && name[1] == ':')
name += 2;
#endif
for (base = name; *name; name++)
1999-05-03 09:29:11 +02:00
{
2000-02-22 16:59:20 +01:00
if (IS_DIR_SEPARATOR (*name))
1999-05-03 09:29:11 +02:00
{
2000-02-22 16:59:20 +01:00
base = name + 1;
1999-05-03 09:29:11 +02:00
}
}
return (char *) base;
}
2000-02-22 16:59:20 +01:00