Fix compilation errors related to rpl_mkdir on mingw.
[gnulib.git] / lib / mkdir.c
1 /* On some systems, mkdir ("foo/", 0700) fails because of the trailing
2    slash.  On those systems, this wrapper removes the trailing slash.
3
4    Copyright (C) 2001, 2003, 2006, 2008 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "dirname.h"
32 #include "xalloc.h"
33
34 /* Disable the definition of mkdir to rpl_mkdir (from the <sys/stat.h>
35    substitute) in this file.  Otherwise, we'd get an endless recursion.  */
36 #undef mkdir
37
38 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
39    Additionally, it declares _mkdir (and depending on compile flags, an
40    alias mkdir), only in the nonstandard io.h.  */
41 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
42 # define mkdir(name,mode) _mkdir (name)
43 #endif
44
45 /* This function is required at least for NetBSD 1.5.2.  */
46
47 int
48 rpl_mkdir (char const *dir, mode_t mode)
49 {
50   int ret_val;
51   char *tmp_dir;
52   size_t len = strlen (dir);
53
54   if (len && dir[len - 1] == '/')
55     {
56       tmp_dir = xstrdup (dir);
57       strip_trailing_slashes (tmp_dir);
58     }
59   else
60     {
61       tmp_dir = (char *) dir;
62     }
63
64   ret_val = mkdir (tmp_dir, mode);
65
66   if (tmp_dir != dir)
67     free (tmp_dir);
68
69   return ret_val;
70 }