Mercurial > hg > octave-kai > gnulib-hg
annotate lib/basename.c @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
5907 | 1 /* basename.c -- return the last element in a file name |
4633 | 2 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16235
diff
changeset
|
3 Copyright (C) 1990, 1998-2001, 2003-2006, 2009-2013 Free Software |
12559
c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents:
12518
diff
changeset
|
4 Foundation, Inc. |
1250 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
1250 | 7 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
9 (at your option) any later version. |
1250 | 10 |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9 | 18 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6912
diff
changeset
|
19 #include <config.h> |
379
0c05dc8a5d05
(basename): Use strrchr, not rindex.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
20 |
3243
025019cda2d6
Use "", not <> to include dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3241
diff
changeset
|
21 #include "dirname.h" |
1952
8c28becce781
(base_name): Add prototype. From Akim Demaille.
Jim Meyering <jim@meyering.net>
parents:
1738
diff
changeset
|
22 |
6912 | 23 #include <string.h> |
24 #include "xalloc.h" | |
25 #include "xstrndup.h" | |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
26 |
6912 | 27 char * |
28 base_name (char const *name) | |
29 { | |
30 char const *base = last_component (name); | |
31 size_t length; | |
32 | |
33 /* If there is no last component, then name is a file system root or the | |
34 empty string. */ | |
35 if (! *base) | |
36 return xstrndup (name, base_len (name)); | |
37 | |
38 /* Collapse a sequence of trailing slashes into one. */ | |
39 length = base_len (base); | |
40 if (ISSLASH (base[length])) | |
41 length++; | |
42 | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
43 /* On systems with drive letters, "a/b:c" must return "./b:c" rather |
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
44 than "b:c" to avoid confusion with a drive letter. On systems |
6912 | 45 with pure POSIX semantics, this is not an issue. */ |
46 if (FILE_SYSTEM_PREFIX_LEN (base)) | |
47 { | |
48 char *p = xmalloc (length + 3); | |
49 p[0] = '.'; | |
50 p[1] = '/'; | |
51 memcpy (p + 2, base, length); | |
52 p[length + 2] = '\0'; | |
53 return p; | |
54 } | |
55 | |
56 /* Finally, copy the basename. */ | |
57 return xstrndup (base, length); | |
58 } |