annotate lib/basename.c @ 276:23034db7dd8a 2.5

fixed expression 'slots' to not store null on a pass 1 store
author lost
date Mon, 31 Aug 2009 08:39:36 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* basename.c -- return the last element in a file name
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006 Free
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4 Software Foundation, Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6 This program is free software: you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9 (at your option) any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19 #include <config.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21 #include "dirname.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23 #include <string.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24 #include "xalloc.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25 #include "xstrndup.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27 /* Return the address of the last file name component of NAME. If
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28 NAME has no relative file name components because it is a file
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29 system root, return the empty string. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31 char *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32 last_component (char const *name)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34 char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35 char const *p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 bool saw_slash = false;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 while (ISSLASH (*base))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39 base++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 for (p = base; *p; p++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43 if (ISSLASH (*p))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 saw_slash = true;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45 else if (saw_slash)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47 base = p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 saw_slash = false;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 return (char *) base;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 /* In general, we can't use the builtin `basename' function if available,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57 since it has different meanings in different environments.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58 In some environments the builtin `basename' modifies its argument.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 Return the last file name component of NAME, allocated with
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61 xmalloc. On systems with drive letters, a leading "./"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62 distinguishes relative names that would otherwise look like a drive
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63 letter. Unlike POSIX basename(), NAME cannot be NULL,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64 base_name("") returns "", and the first trailing slash is not
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 stripped.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67 If lstat (NAME) would succeed, then { chdir (dir_name (NAME));
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 lstat (base_name (NAME)); } will access the same file. Likewise,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69 if the sequence { chdir (dir_name (NAME));
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 rename (base_name (NAME), "foo"); } succeeds, you have renamed NAME
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71 to "foo" in the same directory NAME was in. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 char *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 base_name (char const *name)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 char const *base = last_component (name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77 size_t length;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79 /* If there is no last component, then name is a file system root or the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80 empty string. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81 if (! *base)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82 return xstrndup (name, base_len (name));
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 /* Collapse a sequence of trailing slashes into one. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 length = base_len (base);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86 if (ISSLASH (base[length]))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87 length++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89 /* On systems with drive letters, `a/b:c' must return `./b:c' rather
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90 than `b:c' to avoid confusion with a drive letter. On systems
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91 with pure POSIX semantics, this is not an issue. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92 if (FILE_SYSTEM_PREFIX_LEN (base))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94 char *p = xmalloc (length + 3);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95 p[0] = '.';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 p[1] = '/';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97 memcpy (p + 2, base, length);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 p[length + 2] = '\0';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99 return p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102 /* Finally, copy the basename. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103 return xstrndup (base, length);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106 /* Return the length of the basename NAME. Typically NAME is the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107 value returned by base_name or last_component. Act like strlen
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 (NAME), except omit all trailing slashes. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
109
d5392bb5da3c Added generated files
lost
parents:
diff changeset
110 size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
111 base_len (char const *name)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
112 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
113 size_t len;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
114 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
115
d5392bb5da3c Added generated files
lost
parents:
diff changeset
116 for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
117 continue;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
118
d5392bb5da3c Added generated files
lost
parents:
diff changeset
119 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
d5392bb5da3c Added generated files
lost
parents:
diff changeset
120 && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
d5392bb5da3c Added generated files
lost
parents:
diff changeset
121 return 2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
122
d5392bb5da3c Added generated files
lost
parents:
diff changeset
123 if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
d5392bb5da3c Added generated files
lost
parents:
diff changeset
124 && len == prefix_len && ISSLASH (name[prefix_len]))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
125 return prefix_len + 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
126
d5392bb5da3c Added generated files
lost
parents:
diff changeset
127 return len;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
128 }