gcc/libiberty/basename.c

67 lines
1.4 KiB
C
Raw Normal View History

1997-08-22 00:57:35 +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
Presumes a UNIX or DOS/Windows style path with UNIX or DOS/Windows
style separators.
1997-08-22 00:57:35 +02:00
*/
#include "ansidecl.h"
#include "libiberty.h"
#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 */
1997-08-22 00:57:35 +02:00
char *
basename (name)
const char *name;
{
const char *base;
1997-08-22 00:57:35 +02: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++)
1997-08-22 00:57:35 +02:00
{
if (IS_DIR_SEPARATOR (*name))
1997-08-22 00:57:35 +02:00
{
base = name + 1;
1997-08-22 00:57:35 +02:00
}
}
return (char *) base;
}