]>
Commit | Line | Data |
---|---|---|
22312b71 | 1 | /* dirname.c -- return all but the last element in a path |
e9665d16 | 2 | Copyright 1990, 1998, 2000, 2001, 2003 Free Software Foundation, Inc. |
22312b71 AD |
3 | |
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software Foundation, | |
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
17 | ||
18 | #if HAVE_CONFIG_H | |
19 | # include <config.h> | |
20 | #endif | |
21 | ||
eb956856 | 22 | #if STDC_HEADERS || HAVE_STRING_H |
22312b71 | 23 | # include <string.h> |
22312b71 AD |
24 | #endif |
25 | ||
26 | #include "dirname.h" | |
eb956856 | 27 | #include "xalloc.h" |
22312b71 | 28 | |
eb956856 PE |
29 | /* Return the length of `dirname (PATH)', or zero if PATH is |
30 | in the working directory. Works properly even if | |
31 | there are trailing slashes (by effectively ignoring them). */ | |
32 | size_t | |
33 | dir_len (char const *path) | |
22312b71 | 34 | { |
eb956856 PE |
35 | size_t prefix_length = FILESYSTEM_PREFIX_LEN (path); |
36 | size_t length; | |
22312b71 | 37 | |
eb956856 PE |
38 | /* Strip the basename and any redundant slashes before it. */ |
39 | for (length = base_name (path) - path; prefix_length < length; length--) | |
40 | if (! ISSLASH (path[length - 1])) | |
41 | return length; | |
22312b71 | 42 | |
eb956856 PE |
43 | /* But don't strip the only slash from "/". */ |
44 | return prefix_length + ISSLASH (path[prefix_length]); | |
22312b71 AD |
45 | } |
46 | ||
47 | /* Return the leading directories part of PATH, | |
eb956856 | 48 | allocated with xmalloc. |
22312b71 AD |
49 | Works properly even if there are trailing slashes |
50 | (by effectively ignoring them). */ | |
51 | ||
52 | char * | |
53 | dir_name (char const *path) | |
54 | { | |
eb956856 PE |
55 | size_t length = dir_len (path); |
56 | int append_dot = (length == FILESYSTEM_PREFIX_LEN (path)); | |
57 | char *newpath = xmalloc (length + append_dot + 1); | |
58 | memcpy (newpath, path, length); | |
22312b71 AD |
59 | if (append_dot) |
60 | newpath[length++] = '.'; | |
61 | newpath[length] = 0; | |
62 | return newpath; | |
63 | } | |
64 | ||
65 | #ifdef TEST_DIRNAME | |
66 | /* | |
67 | ||
68 | Run the test like this (expect no output): | |
eb956856 | 69 | gcc -DHAVE_CONFIG_H -DTEST_DIRNAME -I.. -O -Wall \ |
e9665d16 | 70 | basename.c dirname.c xmalloc.c error.c |
22312b71 AD |
71 | sed -n '/^BEGIN-DATA$/,/^END-DATA$/p' dirname.c|grep -v DATA|./a.out |
72 | ||
e9665d16 PE |
73 | If it's been built on a DOS or Windows platforms, run another test like |
74 | this (again, expect no output): | |
75 | sed -n '/^BEGIN-DOS-DATA$/,/^END-DOS-DATA$/p' dirname.c|grep -v DATA|./a.out | |
76 | ||
22312b71 AD |
77 | BEGIN-DATA |
78 | foo//// . | |
79 | bar/foo//// bar | |
80 | foo/ . | |
81 | / / | |
82 | . . | |
83 | a . | |
84 | END-DATA | |
85 | ||
e9665d16 PE |
86 | BEGIN-DOS-DATA |
87 | c:///// c:/ | |
88 | c:/ c:/ | |
89 | c:/. c:/ | |
90 | c:foo c:. | |
91 | c:foo/bar c:foo | |
92 | END-DOS-DATA | |
93 | ||
22312b71 AD |
94 | */ |
95 | ||
96 | # define MAX_BUFF_LEN 1024 | |
97 | # include <stdio.h> | |
22312b71 | 98 | |
e9665d16 PE |
99 | char *program_name; |
100 | ||
22312b71 | 101 | int |
e9665d16 | 102 | main (int argc, char *argv[]) |
22312b71 AD |
103 | { |
104 | char buff[MAX_BUFF_LEN + 1]; | |
105 | ||
e9665d16 PE |
106 | program_name = argv[0]; |
107 | ||
22312b71 AD |
108 | buff[MAX_BUFF_LEN] = 0; |
109 | while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0]) | |
110 | { | |
111 | char path[MAX_BUFF_LEN]; | |
112 | char expected_result[MAX_BUFF_LEN]; | |
113 | char const *result; | |
114 | sscanf (buff, "%s %s", path, expected_result); | |
115 | result = dir_name (path); | |
116 | if (strcmp (result, expected_result)) | |
117 | printf ("%s: got %s, expected %s\n", path, result, expected_result); | |
118 | } | |
eb956856 | 119 | return 0; |
22312b71 AD |
120 | } |
121 | #endif |