annotate lib/basename.c @ 265:68fbca173508 2.6

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