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