Remove; this doesn't belong here.
[gnulib.git] / lib / dirname.c
1 /* dirname.c -- return all but the last element in a path
2    Copyright (C) 1990, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
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
22 #include "dirname.h"
23
24 #include <string.h>
25 #include "xalloc.h"
26
27 /* Return the length of `dirname (PATH)', or zero if PATH is
28    in the working directory.  Works properly even if
29    there are trailing slashes (by effectively ignoring them).  */
30 size_t
31 dir_len (char const *path)
32 {
33   size_t prefix_length = FILESYSTEM_PREFIX_LEN (path);
34   size_t length;
35
36   /* Strip the basename and any redundant slashes before it.  */
37   for (length = base_name (path) - path;  prefix_length < length;  length--)
38     if (! ISSLASH (path[length - 1]))
39       return length;
40
41   /* But don't strip the only slash from "/".  */
42   return prefix_length + ISSLASH (path[prefix_length]);
43 }
44
45 /* Return the leading directories part of PATH,
46    allocated with xmalloc.
47    Works properly even if there are trailing slashes
48    (by effectively ignoring them).  */
49
50 char *
51 dir_name (char const *path)
52 {
53   size_t length = dir_len (path);
54   int append_dot = (length == FILESYSTEM_PREFIX_LEN (path));
55   char *newpath = xmalloc (length + append_dot + 1);
56   memcpy (newpath, path, length);
57   if (append_dot)
58     newpath[length++] = '.';
59   newpath[length] = 0;
60   return newpath;
61 }
62
63 #ifdef TEST_DIRNAME
64 /*
65
66 Run the test like this (expect no output):
67   gcc -DHAVE_CONFIG_H -DTEST_DIRNAME -I.. -O -Wall \
68      basename.c dirname.c xmalloc.c error.c
69   sed -n '/^BEGIN-DATA$/,/^END-DATA$/p' dirname.c|grep -v DATA|./a.out
70
71 If it's been built on a DOS or Windows platforms, run another test like
72 this (again, expect no output):
73   sed -n '/^BEGIN-DOS-DATA$/,/^END-DOS-DATA$/p' dirname.c|grep -v DATA|./a.out
74
75 BEGIN-DATA
76 foo//// .
77 bar/foo//// bar
78 foo/ .
79 / /
80 . .
81 a .
82 END-DATA
83
84 BEGIN-DOS-DATA
85 c:///// c:/
86 c:/ c:/
87 c:/. c:/
88 c:foo c:.
89 c:foo/bar c:foo
90 END-DOS-DATA
91
92 */
93
94 # define MAX_BUFF_LEN 1024
95 # include <stdio.h>
96
97 char *program_name;
98
99 int
100 main (int argc, char *argv[])
101 {
102   char buff[MAX_BUFF_LEN + 1];
103
104   program_name = argv[0];
105
106   buff[MAX_BUFF_LEN] = 0;
107   while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
108     {
109       char path[MAX_BUFF_LEN];
110       char expected_result[MAX_BUFF_LEN];
111       char const *result;
112       sscanf (buff, "%s %s", path, expected_result);
113       result = dir_name (path);
114       if (strcmp (result, expected_result))
115         printf ("%s: got %s, expected %s\n", path, result, expected_result);
116     }
117   return 0;
118 }
119 #endif