annotate lib/dirname-lgpl.c @ 423:079d43b6967b 3.0-beta2 3.0-beta2

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