]> git.cworth.org Git - tar/blob - gnu/mkdir.c
Imported Upstream version 1.24
[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 includes <direct.h> and <io.h>,
42    which are included in the <sys/stat.h> override.  */
43 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
44 # define mkdir(name,mode) _mkdir (name)
45 # define maybe_unused _GL_UNUSED
46 #else
47 # define maybe_unused /* empty */
48 #endif
49
50 /* This function is required at least for NetBSD 1.5.2.  */
51
52 int
53 rpl_mkdir (char const *dir, mode_t mode maybe_unused)
54 {
55   int ret_val;
56   char *tmp_dir;
57   size_t len = strlen (dir);
58
59   if (len && dir[len - 1] == '/')
60     {
61       tmp_dir = strdup (dir);
62       if (!tmp_dir)
63         {
64           /* Rather than rely on strdup-posix, we set errno ourselves.  */
65           errno = ENOMEM;
66           return -1;
67         }
68       strip_trailing_slashes (tmp_dir);
69     }
70   else
71     {
72       tmp_dir = (char *) dir;
73     }
74 #if FUNC_MKDIR_DOT_BUG
75   /* Additionally, cygwin 1.5 mistakenly creates a directory "d/./".  */
76   {
77     char *last = last_component (tmp_dir);
78     if (*last == '.' && (last[1] == '\0'
79                          || (last[1] == '.' && last[2] == '\0')))
80       {
81         struct stat st;
82         if (stat (tmp_dir, &st) == 0)
83           errno = EEXIST;
84         return -1;
85       }
86   }
87 #endif /* FUNC_MKDIR_DOT_BUG */
88
89   ret_val = mkdir (tmp_dir, mode);
90
91   if (tmp_dir != dir)
92     free (tmp_dir);
93
94   return ret_val;
95 }