1 /* Return the name-within-directory of a file name.
2 Copyright (C) 1996-1999, 2000-2002, 2004 Free Software Foundation, Inc.
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.
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
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.
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,
34 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
35 /* Win32, Cygwin, OS/2, DOS */
36 # define HAS_DEVICE(P) \
37 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
39 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
40 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
43 #ifndef FILE_SYSTEM_PREFIX_LEN
44 # define FILE_SYSTEM_PREFIX_LEN(Filename) 0
48 # define ISSLASH(C) ((C) == '/')
52 /* We cannot generally use the name `basename' since XPG defines an unusable
53 variant of the function but we cannot use it. */
55 # define basename gnu_basename
58 /* In general, we can't use the builtin `basename' function if available,
59 since it has different meanings in different environments.
60 In some environments the builtin `basename' modifies its argument.
61 If NAME is all slashes, be sure to return `/'. */
64 basename (char const *name
)
66 char const *base
= name
+= FILE_SYSTEM_PREFIX_LEN (name
);
70 for (p
= name
; *p
; p
++)
78 /* If NAME is all slashes, arrange to return `/'. */
79 if (*base
== '\0' && ISSLASH (*name
) && all_slashes
)
82 /* Make sure the last byte is not a slash. */
83 assert (all_slashes
|| !ISSLASH (*(p
- 1)));