]> git.cworth.org Git - tar/blob - gnu/mkdir.c
c782a732bf2269b10884503df68a415b6e0becdd
[tar] / gnu / mkdir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* On some systems, mkdir ("foo/", 0700) fails because of the trailing
4    slash.  On those systems, this wrapper removes the trailing slash.
5
6    Copyright (C) 2001, 2003, 2006, 2008-2010 Free Software 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 /* written by Jim Meyering */
22
23 #include <config.h>
24
25 /* Specification.  */
26 #include <sys/stat.h>
27
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "dirname.h"
34
35 /* Disable the definition of mkdir to rpl_mkdir (from the <sys/stat.h>
36    substitute) in this file.  Otherwise, we'd get an endless recursion.  */
37 #undef mkdir
38
39 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
40    Additionally, it declares _mkdir (and depending on compile flags, an
41    alias mkdir), only in the nonstandard io.h.  */
42 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
43 # define mkdir(name,mode) _mkdir (name)
44 # define maybe_unused _GL_UNUSED
45 #else
46 # define maybe_unused /* empty */
47 #endif
48
49 /* This function is required at least for NetBSD 1.5.2.  */
50
51 int
52 rpl_mkdir (char const *dir, mode_t mode maybe_unused)
53 {
54   int ret_val;
55   char *tmp_dir;
56   size_t len = strlen (dir);
57
58   if (len && dir[len - 1] == '/')
59     {
60       tmp_dir = strdup (dir);
61       if (!tmp_dir)
62         {
63           /* Rather than rely on strdup-posix, we set errno ourselves.  */
64           errno = ENOMEM;
65           return -1;
66         }
67       strip_trailing_slashes (tmp_dir);
68     }
69   else
70     {
71       tmp_dir = (char *) dir;
72     }
73 #if FUNC_MKDIR_DOT_BUG
74   /* Additionally, cygwin 1.5 mistakenly creates a directory "d/./".  */
75   {
76     char *last = last_component (tmp_dir);
77     if (*last == '.' && (last[1] == '\0'
78                          || (last[1] == '.' && last[2] == '\0')))
79       {
80         struct stat st;
81         if (stat (tmp_dir, &st) == 0)
82           errno = EEXIST;
83         return -1;
84       }
85   }
86 #endif /* FUNC_MKDIR_DOT_BUG */
87
88   ret_val = mkdir (tmp_dir, mode);
89
90   if (tmp_dir != dir)
91     free (tmp_dir);
92
93   return ret_val;
94 }