Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / basename.c
blob8fd3438273611f3a052d18b3e63da9ea50c940ea
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
10 later version.
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,
20 USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 /* Specification. */
27 #include "basename.h"
29 #if !(__GLIBC__ >= 2)
31 #include <stdio.h>
32 #include <assert.h>
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')) \
38 && (P)[1] == ':')
39 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
40 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
41 #endif
43 #ifndef FILE_SYSTEM_PREFIX_LEN
44 # define FILE_SYSTEM_PREFIX_LEN(Filename) 0
45 #endif
47 #ifndef ISSLASH
48 # define ISSLASH(C) ((C) == '/')
49 #endif
51 #ifndef _LIBC
52 /* We cannot generally use the name `basename' since XPG defines an unusable
53 variant of the function but we cannot use it. */
54 # undef basename
55 # define basename gnu_basename
56 #endif
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 `/'. */
63 char *
64 basename (char const *name)
66 char const *base = name += FILE_SYSTEM_PREFIX_LEN (name);
67 int all_slashes = 1;
68 char const *p;
70 for (p = name; *p; p++)
72 if (ISSLASH (*p))
73 base = p + 1;
74 else
75 all_slashes = 0;
78 /* If NAME is all slashes, arrange to return `/'. */
79 if (*base == '\0' && ISSLASH (*name) && all_slashes)
80 --base;
82 /* Make sure the last byte is not a slash. */
83 assert (all_slashes || !ISSLASH (*(p - 1)));
85 return (char *) base;
88 #endif