1 |
/* Libiberty basename. Like basename, but is not overridden by the |
/* Libiberty basename. Like basename, but is not overridden by the |
2 |
system C library. |
system C library. |
3 |
Copyright (C) 2001 Free Software Foundation, Inc. |
Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of the libiberty library. |
This file is part of the libiberty library. |
6 |
Libiberty is free software; you can redistribute it and/or |
Libiberty is free software; you can redistribute it and/or |
19 |
Boston, MA 02111-1307, USA. */ |
Boston, MA 02111-1307, USA. */ |
20 |
|
|
21 |
/* |
/* |
|
NAME |
|
|
lbasename -- return pointer to last component of a pathname |
|
22 |
|
|
23 |
SYNOPSIS |
@deftypefn Replacement {const char*} lbasename (const char *@var{name}) |
|
char *lbasename (const char *name) |
|
24 |
|
|
25 |
DESCRIPTION |
Given a pointer to a string containing a typical pathname |
26 |
Given a pointer to a string containing a typical pathname |
(@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the |
27 |
(/usr/src/cmd/ls/ls.c for example), returns a pointer to the |
last component of the pathname (@samp{ls.c} in this case). The |
28 |
last component of the pathname ("ls.c" in this case). The |
returned pointer is guaranteed to lie within the original |
29 |
returned pointer is guaranteed to lie within the original |
string. This latter fact is not true of many vendor C |
30 |
string. This latter fact is not true of many vendor C |
libraries, which return special strings or modify the passed |
31 |
libraries, which return special strings or modify the passed |
strings for particular input. |
32 |
strings for particular input. |
|
33 |
|
In particular, the empty string returns the same empty string, |
34 |
|
and a path ending in @code{/} returns the empty string after it. |
35 |
|
|
36 |
|
@end deftypefn |
37 |
|
|
|
In particular, the empty string returns the same empty string, |
|
|
and a path ending in '/' returns the empty string after it. |
|
38 |
*/ |
*/ |
39 |
|
|
40 |
#include "ansidecl.h" |
#include "ansidecl.h" |
41 |
#include "libiberty.h" |
#include "libiberty.h" |
42 |
#include "safe-ctype.h" |
#include "safe-ctype.h" |
43 |
|
#include "filenames.h" |
44 |
|
|
45 |
#ifndef DIR_SEPARATOR |
const char * |
|
# 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. VMS uses '::', ':', '[...]' and '<...>' to |
|
|
separate the different components of a file specification. It's a |
|
|
bit of a stretch to call ':', ']' and '>' directory separators, so |
|
|
just define the test to find the file name component. */ |
|
|
#ifdef VMS |
|
|
# define IS_DIR_SEPARATOR(ch) ((ch) == ':' || (ch) == ']' || (ch) == '>') |
|
|
#else |
|
|
# ifndef DIR_SEPARATOR_2 |
|
|
# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) |
|
|
# else |
|
|
# define IS_DIR_SEPARATOR(ch) \ |
|
|
(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) |
|
|
# endif |
|
|
#endif |
|
|
|
|
|
char * |
|
46 |
lbasename (name) |
lbasename (name) |
47 |
const char *name; |
const char *name; |
48 |
{ |
{ |
58 |
if (IS_DIR_SEPARATOR (*name)) |
if (IS_DIR_SEPARATOR (*name)) |
59 |
base = name + 1; |
base = name + 1; |
60 |
|
|
61 |
return (char *) base; |
return base; |
62 |
} |
} |