annotate lib/dirname.c @ 266:5f25e7afac21 2.6

Bumped version to 2.6
author lost
date Tue, 22 Dec 2009 05:32:28 +0000
parents 68fbca173508
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 /* dirname.c -- return all but 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, 2000, 2001, 2003, 2004, 2005, 2006 Free Software
68fbca173508 Added generated files for release
lost
parents:
diff changeset
4 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
68fbca173508 Added generated files for release
lost
parents:
diff changeset
26 /* Return the length of the prefix of FILE that will be used by
68fbca173508 Added generated files for release
lost
parents:
diff changeset
27 dir_name. If FILE is in the working directory, this returns zero
68fbca173508 Added generated files for release
lost
parents:
diff changeset
28 even though `dir_name (FILE)' will return ".". Works properly even
68fbca173508 Added generated files for release
lost
parents:
diff changeset
29 if there are trailing slashes (by effectively ignoring them). */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
30
68fbca173508 Added generated files for release
lost
parents:
diff changeset
31 size_t
68fbca173508 Added generated files for release
lost
parents:
diff changeset
32 dir_len (char const *file)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
33 {
68fbca173508 Added generated files for release
lost
parents:
diff changeset
34 size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
35 size_t length;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
36
68fbca173508 Added generated files for release
lost
parents:
diff changeset
37 /* Advance prefix_length beyond important leading slashes. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
38 prefix_length += (prefix_length != 0
68fbca173508 Added generated files for release
lost
parents:
diff changeset
39 ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
68fbca173508 Added generated files for release
lost
parents:
diff changeset
40 && ISSLASH (file[prefix_length]))
68fbca173508 Added generated files for release
lost
parents:
diff changeset
41 : (ISSLASH (file[0])
68fbca173508 Added generated files for release
lost
parents:
diff changeset
42 ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
68fbca173508 Added generated files for release
lost
parents:
diff changeset
43 && ISSLASH (file[1]) && ! ISSLASH (file[2])
68fbca173508 Added generated files for release
lost
parents:
diff changeset
44 ? 2 : 1))
68fbca173508 Added generated files for release
lost
parents:
diff changeset
45 : 0));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
46
68fbca173508 Added generated files for release
lost
parents:
diff changeset
47 /* Strip the basename and any redundant slashes before it. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
48 for (length = last_component (file) - file;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
49 prefix_length < length; length--)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
50 if (! ISSLASH (file[length - 1]))
68fbca173508 Added generated files for release
lost
parents:
diff changeset
51 break;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
52 return length;
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 `dirname' 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 `dirname' 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 leading directories part of FILE, allocated with xmalloc.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
61 Works properly even if there are trailing slashes (by effectively
68fbca173508 Added generated files for release
lost
parents:
diff changeset
62 ignoring them). Unlike POSIX dirname(), FILE cannot be NULL.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
63
68fbca173508 Added generated files for release
lost
parents:
diff changeset
64 If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
65 lstat (base_name (FILE)); } will access the same file. Likewise,
68fbca173508 Added generated files for release
lost
parents:
diff changeset
66 if the sequence { chdir (dir_name (FILE));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
67 rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
68fbca173508 Added generated files for release
lost
parents:
diff changeset
68 to "foo" in the same directory FILE was in. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
69
68fbca173508 Added generated files for release
lost
parents:
diff changeset
70 char *
68fbca173508 Added generated files for release
lost
parents:
diff changeset
71 dir_name (char const *file)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
72 {
68fbca173508 Added generated files for release
lost
parents:
diff changeset
73 size_t length = dir_len (file);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
74 bool append_dot = (length == 0
68fbca173508 Added generated files for release
lost
parents:
diff changeset
75 || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
68fbca173508 Added generated files for release
lost
parents:
diff changeset
76 && length == FILE_SYSTEM_PREFIX_LEN (file)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
77 && file[2] != '\0' && ! ISSLASH (file[2])));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
78 char *dir = xmalloc (length + append_dot + 1);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
79 memcpy (dir, file, length);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
80 if (append_dot)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
81 dir[length++] = '.';
68fbca173508 Added generated files for release
lost
parents:
diff changeset
82 dir[length] = '\0';
68fbca173508 Added generated files for release
lost
parents:
diff changeset
83 return dir;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
84 }