Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/basename.c @ 6912:314715e0260d
Merge from coreutils.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 03 Jul 2006 08:32:46 +0000 |
parents | 96c32553b4c6 |
children | 8a1a9361108c |
rev | line source |
---|---|
5907 | 1 /* basename.c -- return the last element in a file name |
4633 | 2 |
6912 | 3 Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006 Free |
5122
d5943c753225
filesystem -> file system
Paul Eggert <eggert@cs.ucla.edu>
parents:
4633
diff
changeset
|
4 Software Foundation, Inc. |
1250 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
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 | |
17 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5122
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
9 | 19 |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5907
diff
changeset
|
20 #ifdef HAVE_CONFIG_H |
653 | 21 # include <config.h> |
379
0c05dc8a5d05
(basename): Use strrchr, not rindex.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
22 #endif |
0c05dc8a5d05
(basename): Use strrchr, not rindex.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
23 |
3243
025019cda2d6
Use "", not <> to include dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3241
diff
changeset
|
24 #include "dirname.h" |
1952
8c28becce781
(base_name): Add prototype. From Akim Demaille.
Jim Meyering <jim@meyering.net>
parents:
1738
diff
changeset
|
25 |
6912 | 26 #include <string.h> |
27 #include "xalloc.h" | |
28 #include "xstrndup.h" | |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
29 |
6912 | 30 /* Return the address of the last file name component of NAME. If |
31 NAME has no relative file name components because it is a file | |
32 system root, return the empty string. */ | |
9 | 33 |
34 char * | |
6912 | 35 last_component (char const *name) |
9 | 36 { |
5122
d5943c753225
filesystem -> file system
Paul Eggert <eggert@cs.ucla.edu>
parents:
4633
diff
changeset
|
37 char const *base = name + FILE_SYSTEM_PREFIX_LEN (name); |
1737
c6a6decaf2ad
(base_name): If NAME is all slashes, return `/' (in
Jim Meyering <jim@meyering.net>
parents:
1251
diff
changeset
|
38 char const *p; |
6912 | 39 bool saw_slash = false; |
40 | |
41 while (ISSLASH (*base)) | |
42 base++; | |
9 | 43 |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
44 for (p = base; *p; p++) |
1737
c6a6decaf2ad
(base_name): If NAME is all slashes, return `/' (in
Jim Meyering <jim@meyering.net>
parents:
1251
diff
changeset
|
45 { |
c6a6decaf2ad
(base_name): If NAME is all slashes, return `/' (in
Jim Meyering <jim@meyering.net>
parents:
1251
diff
changeset
|
46 if (ISSLASH (*p)) |
6912 | 47 saw_slash = true; |
48 else if (saw_slash) | |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
49 { |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
50 base = p; |
6912 | 51 saw_slash = false; |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
52 } |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
53 } |
2763
8a832ac25a04
(base_name): Add an assertion.
Jim Meyering <jim@meyering.net>
parents:
2718
diff
changeset
|
54 |
590
cf44c49093fd
(basename): Rewrite so it doesn't rely on strrchr,
Jim Meyering <jim@meyering.net>
parents:
421
diff
changeset
|
55 return (char *) base; |
9 | 56 } |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
57 |
6912 | 58 |
59 /* In general, we can't use the builtin `basename' function if available, | |
60 since it has different meanings in different environments. | |
61 In some environments the builtin `basename' modifies its argument. | |
62 | |
63 Return the last file name component of NAME, allocated with | |
64 xmalloc. On systems with drive letters, a leading "./" | |
65 distinguishes relative names that would otherwise look like a drive | |
66 letter. Unlike POSIX basename(), NAME cannot be NULL, | |
67 base_name("") returns "", and the first trailing slash is not | |
68 stripped. | |
69 | |
70 If lstat (NAME) would succeed, then { chdir (dir_name (NAME)); | |
71 lstat (base_name (NAME)); } will access the same file. Likewise, | |
72 if the sequence { chdir (dir_name (NAME)); | |
73 rename (base_name (NAME), "foo"); } succeeds, you have renamed NAME | |
74 to "foo" in the same directory NAME was in. */ | |
75 | |
76 char * | |
77 base_name (char const *name) | |
78 { | |
79 char const *base = last_component (name); | |
80 size_t length; | |
81 | |
82 /* If there is no last component, then name is a file system root or the | |
83 empty string. */ | |
84 if (! *base) | |
85 return xstrndup (name, base_len (name)); | |
86 | |
87 /* Collapse a sequence of trailing slashes into one. */ | |
88 length = base_len (base); | |
89 if (ISSLASH (base[length])) | |
90 length++; | |
91 | |
92 /* On systems with drive letters, `a/b:c' must return `./b:c' rather | |
93 than `b:c' to avoid confusion with a drive letter. On systems | |
94 with pure POSIX semantics, this is not an issue. */ | |
95 if (FILE_SYSTEM_PREFIX_LEN (base)) | |
96 { | |
97 char *p = xmalloc (length + 3); | |
98 p[0] = '.'; | |
99 p[1] = '/'; | |
100 memcpy (p + 2, base, length); | |
101 p[length + 2] = '\0'; | |
102 return p; | |
103 } | |
104 | |
105 /* Finally, copy the basename. */ | |
106 return xstrndup (base, length); | |
107 } | |
108 | |
109 /* Return the length of the basename NAME. Typically NAME is the | |
110 value returned by base_name or last_component. Act like strlen | |
111 (NAME), except omit all trailing slashes. */ | |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
112 |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
113 size_t |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
114 base_len (char const *name) |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
115 { |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
116 size_t len; |
6912 | 117 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name); |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
118 |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
119 for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--) |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
120 continue; |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
121 |
6912 | 122 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1 |
123 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2]) | |
124 return 2; | |
125 | |
126 if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len | |
127 && len == prefix_len && ISSLASH (name[prefix_len])) | |
128 return prefix_len + 1; | |
129 | |
3241
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
130 return len; |
3de6cd0813b1
(FILESYSTEM_PREFIX_LEN, PARAMS, ISSLASH): Remove; now in dirname.h.
Jim Meyering <jim@meyering.net>
parents:
3107
diff
changeset
|
131 } |