Override <sys/socket.h> when it exists but is incomplete.
[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 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 /* Disable the definition of mkdir to rpl_mkdir (from config.h) in this
24    file.  Otherwise, we'd get conflicting prototypes for rpl_mkdir on
25    most systems.  */
26 #undef mkdir
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "dirname.h"
35 #include "xalloc.h"
36
37 /* This function is required at least for NetBSD 1.5.2.  */
38
39 int
40 rpl_mkdir (char const *dir, mode_t mode)
41 {
42   int ret_val;
43   char *tmp_dir;
44   size_t len = strlen (dir);
45
46   if (len && dir[len - 1] == '/')
47     {
48       tmp_dir = xstrdup (dir);
49       strip_trailing_slashes (tmp_dir);
50     }
51   else
52     {
53       tmp_dir = (char *) dir;
54     }
55
56   ret_val = mkdir (tmp_dir, mode);
57
58   if (tmp_dir != dir)
59     free (tmp_dir);
60
61   return ret_val;
62 }