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