1 | /* Return the name-within-directory of a file name. |
---|
2 | Copyright (C) 1996-1999, 2000, 2001 Free Software Foundation, Inc. |
---|
3 | |
---|
4 | NOTE: The canonical source of this file is maintained with the GNU C Library. |
---|
5 | Bugs can be reported to bug-glibc@gnu.org. |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify it |
---|
8 | under the terms of the GNU General Public License as published by the |
---|
9 | Free Software Foundation; either version 2, or (at your option) any |
---|
10 | later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with this program; if not, write to the Free Software |
---|
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
20 | USA. */ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | # include <config.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <assert.h> |
---|
28 | |
---|
29 | #ifndef FILESYSTEM_PREFIX_LEN |
---|
30 | # define FILESYSTEM_PREFIX_LEN(Filename) 0 |
---|
31 | #endif |
---|
32 | |
---|
33 | #ifndef ISSLASH |
---|
34 | # define ISSLASH(C) ((C) == '/') |
---|
35 | #endif |
---|
36 | |
---|
37 | #ifndef _LIBC |
---|
38 | /* We cannot generally use the name `basename' since XPG defines an unusable |
---|
39 | variant of the function but we cannot use it. */ |
---|
40 | # define basename gnu_basename |
---|
41 | #endif |
---|
42 | |
---|
43 | /* In general, we can't use the builtin `basename' function if available, |
---|
44 | since it has different meanings in different environments. |
---|
45 | In some environments the builtin `basename' modifies its argument. |
---|
46 | If NAME is all slashes, be sure to return `/'. */ |
---|
47 | |
---|
48 | char * |
---|
49 | basename (name) |
---|
50 | char const *name; |
---|
51 | { |
---|
52 | char const *base = name += FILESYSTEM_PREFIX_LEN (name); |
---|
53 | int all_slashes = 1; |
---|
54 | char const *p; |
---|
55 | |
---|
56 | for (p = name; *p; p++) |
---|
57 | { |
---|
58 | if (ISSLASH (*p)) |
---|
59 | base = p + 1; |
---|
60 | else |
---|
61 | all_slashes = 0; |
---|
62 | } |
---|
63 | |
---|
64 | /* If NAME is all slashes, arrange to return `/'. */ |
---|
65 | if (*base == '\0' && ISSLASH (*name) && all_slashes) |
---|
66 | --base; |
---|
67 | |
---|
68 | /* Make sure the last byte is not a slash. */ |
---|
69 | assert (all_slashes || !ISSLASH (*(p - 1))); |
---|
70 | |
---|
71 | return (char *) base; |
---|
72 | } |
---|