annotate lib/dirname-lgpl.c @ 437:84950ae2e2bf 3.0

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