Implement window size passing between real tty and virtual console.
[dragonfly/vkernel-mp.git] / contrib / tar / lib / dirname.c
blob9fb5f09374d1ce1c232e5ad18263cfbe763954a2
1 /* dirname.c -- return all but the last element in a path
2 Copyright 1990, 1998, 2000, 2001 Free Software Foundation, Inc.
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.
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.
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. */
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #if STDC_HEADERS || HAVE_STRING_H
23 # include <string.h>
24 #endif
26 #include "dirname.h"
27 #include "xalloc.h"
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)
35 size_t prefix_length = FILESYSTEM_PREFIX_LEN (path);
36 size_t length;
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;
43 /* But don't strip the only slash from "/". */
44 return prefix_length + ISSLASH (path[prefix_length]);
47 /* Return the leading directories part of PATH,
48 allocated with xmalloc.
49 Works properly even if there are trailing slashes
50 (by effectively ignoring them). */
52 char *
53 dir_name (char const *path)
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);
59 if (append_dot)
60 newpath[length++] = '.';
61 newpath[length] = 0;
62 return newpath;
65 #ifdef TEST_DIRNAME
68 Run the test like this (expect no output):
69 gcc -DHAVE_CONFIG_H -DTEST_DIRNAME -I.. -O -Wall \
70 basename.c dirname.c xmalloc.c
71 sed -n '/^BEGIN-DATA$/,/^END-DATA$/p' dirname.c|grep -v DATA|./a.out
73 BEGIN-DATA
74 foo//// .
75 bar/foo//// bar
76 foo/ .
77 / /
78 . .
79 a .
80 END-DATA
84 # define MAX_BUFF_LEN 1024
85 # include <stdio.h>
87 int
88 main ()
90 char buff[MAX_BUFF_LEN + 1];
92 buff[MAX_BUFF_LEN] = 0;
93 while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
95 char path[MAX_BUFF_LEN];
96 char expected_result[MAX_BUFF_LEN];
97 char const *result;
98 sscanf (buff, "%s %s", path, expected_result);
99 result = dir_name (path);
100 if (strcmp (result, expected_result))
101 printf ("%s: got %s, expected %s\n", path, result, expected_result);
103 return 0;
105 #endif