11541Srgrimes/* Return the basename of a pathname.
210937Swollman   This file is in the public domain. */
31541Srgrimes
41541Srgrimes/*
51541Srgrimes
61541Srgrimes@deftypefn Supplemental char* basename (const char *@var{name})
71541Srgrimes
81541SrgrimesReturns a pointer to the last component of pathname @var{name}.
91541SrgrimesBehavior is undefined if the pathname ends in a directory separator.
101541Srgrimes
111541Srgrimes@end deftypefn
121541Srgrimes
131541Srgrimes*/
141541Srgrimes
151541Srgrimes#ifdef HAVE_CONFIG_H
161541Srgrimes#include "config.h"
171541Srgrimes#endif
181541Srgrimes#include "ansidecl.h"
191541Srgrimes#include "libiberty.h"
201541Srgrimes#include "safe-ctype.h"
211541Srgrimes
221541Srgrimes#ifndef DIR_SEPARATOR
231541Srgrimes#define DIR_SEPARATOR '/'
241541Srgrimes#endif
251541Srgrimes
261541Srgrimes#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
271541Srgrimes  defined (__OS2__)
281541Srgrimes#define HAVE_DOS_BASED_FILE_SYSTEM
291541Srgrimes#ifndef DIR_SEPARATOR_2
301541Srgrimes#define DIR_SEPARATOR_2 '\\'
311541Srgrimes#endif
321541Srgrimes#endif
3310937Swollman
3450477Speter/* Define IS_DIR_SEPARATOR.  */
351541Srgrimes#ifndef DIR_SEPARATOR_2
361541Srgrimes# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
372169Spaul#else /* DIR_SEPARATOR_2 */
382169Spaul# define IS_DIR_SEPARATOR(ch) \
3986764Sjlemon	(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
4086764Sjlemon#endif /* DIR_SEPARATOR_2 */
4198102Shsu
4286764Sjlemonchar *
431541Srgrimesbasename (const char *name)
441541Srgrimes{
451541Srgrimes  const char *base;
4686764Sjlemon
4786764Sjlemon#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
481541Srgrimes  /* Skip over the disk name in MSDOS pathnames. */
4955679Sshin  if (ISALPHA (name[0]) && name[1] == ':')
5055679Sshin    name += 2;
5160938Sjake#endif
5255679Sshin
5355679Sshin  for (base = name; *name; name++)
5455679Sshin    {
5555679Sshin      if (IS_DIR_SEPARATOR (*name))
5660938Sjake	{
5755679Sshin	  base = name + 1;
5855679Sshin	}
5955679Sshin    }
6055679Sshin  return (char *) base;
6155679Sshin}
6255679Sshin
6355679Sshin