annotate lib/dirname.c @ 286:701132971855 2.3

Fixed regression related to force linking a library file
author lost
date Sat, 25 Apr 2009 02:18:06 +0000
parents 2cb097e760a5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
1 /* dirname.c -- return all but the last element in a file name
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
2
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
3 Copyright (C) 1990, 1998, 2000, 2001, 2003, 2004, 2005, 2006 Free Software
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
4 Foundation, Inc.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
5
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
6 This program is free software: you can redistribute it and/or modify
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
9 (at your option) any later version.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
10
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
14 GNU General Public License for more details.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
15
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
18
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
19 #include <config.h>
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
20
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
21 #include "dirname.h"
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
22
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
23 #include <string.h>
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
24 #include "xalloc.h"
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
25
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
26 /* Return the length of the prefix of FILE that will be used by
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
27 dir_name. If FILE is in the working directory, this returns zero
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
28 even though `dir_name (FILE)' will return ".". Works properly even
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
29 if there are trailing slashes (by effectively ignoring them). */
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
30
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
31 size_t
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
32 dir_len (char const *file)
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
33 {
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
34 size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
35 size_t length;
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
36
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
37 /* Advance prefix_length beyond important leading slashes. */
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
38 prefix_length += (prefix_length != 0
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
39 ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
40 && ISSLASH (file[prefix_length]))
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
41 : (ISSLASH (file[0])
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
42 ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
43 && ISSLASH (file[1]) && ! ISSLASH (file[2])
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
44 ? 2 : 1))
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
45 : 0));
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
46
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
47 /* Strip the basename and any redundant slashes before it. */
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
48 for (length = last_component (file) - file;
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
49 prefix_length < length; length--)
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
50 if (! ISSLASH (file[length - 1]))
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
51 break;
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
52 return length;
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
53 }
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
54
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
55
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
56 /* In general, we can't use the builtin `dirname' function if available,
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
57 since it has different meanings in different environments.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
58 In some environments the builtin `dirname' modifies its argument.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
59
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
60 Return the leading directories part of FILE, allocated with xmalloc.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
61 Works properly even if there are trailing slashes (by effectively
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
62 ignoring them). Unlike POSIX dirname(), FILE cannot be NULL.
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
63
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
64 If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
65 lstat (base_name (FILE)); } will access the same file. Likewise,
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
66 if the sequence { chdir (dir_name (FILE));
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
67 rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
68 to "foo" in the same directory FILE was in. */
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
69
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
70 char *
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
71 dir_name (char const *file)
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
72 {
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
73 size_t length = dir_len (file);
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
74 bool append_dot = (length == 0
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
75 || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
76 && length == FILE_SYSTEM_PREFIX_LEN (file)
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
77 && file[2] != '\0' && ! ISSLASH (file[2])));
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
78 char *dir = xmalloc (length + append_dot + 1);
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
79 memcpy (dir, file, length);
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
80 if (append_dot)
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
81 dir[length++] = '.';
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
82 dir[length] = '\0';
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
83 return dir;
2cb097e760a5 Added various generated files in prep for release
lost
parents:
diff changeset
84 }