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-2021 Free Software Foundation, Inc. 5 6 This file is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as 8 published by the Free Software Foundation; either version 2.1 of the 9 License, or (at your option) any later version. 10 11 This file 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 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 18 19 /* written by Jim Meyering */ 20 21 #include <config.h> 22 23 /* Specification. */ 24 #include <sys/stat.h> 25 26 #include <errno.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 31 #include "dirname.h" 32 33 /* Disable the definition of mkdir to rpl_mkdir (from the <sys/stat.h> 34 substitute) in this file. Otherwise, we'd get an endless recursion. */ 35 #undef mkdir 36 37 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments. 38 Additionally, it declares _mkdir (and depending on compile flags, an 39 alias mkdir), only in the nonstandard includes <direct.h> and <io.h>, 40 which are included in the <sys/stat.h> override. */ 41 #if defined _WIN32 && ! defined __CYGWIN__ 42 # define mkdir(name,mode) _mkdir (name) 43 # define maybe_unused _GL_UNUSED 44 #else 45 # define maybe_unused /* empty */ 46 #endif 47 48 /* This function is required at least for NetBSD 1.5.2. */ 49 50 int 51 rpl_mkdir (char const *dir, maybe_unused mode_t mode) /* */ 52 { 53 int ret_val; 54 char *tmp_dir; 55 size_t len = strlen (dir); 56 57 if (len && dir[len - 1] == '/') 58 { 59 tmp_dir = strdup (dir); 60 if (!tmp_dir) 61 { 62 /* Rather than rely on strdup-posix, we set errno ourselves. */ 63 errno = ENOMEM; 64 return -1; 65 } 66 strip_trailing_slashes (tmp_dir); 67 } 68 else 69 { 70 tmp_dir = (char *) dir; 71 } 72 #if FUNC_MKDIR_DOT_BUG 73 /* Additionally, cygwin 1.5 mistakenly creates a directory "d/./". */ 74 { 75 char *last = last_component (tmp_dir); 76 if (*last == '.' && (last[1] == '\0' 77 || (last[1] == '.' && last[2] == '\0'))) 78 { 79 struct stat st; 80 if (stat (tmp_dir, &st) == 0 || errno == EOVERFLOW) 81 errno = EEXIST; 82 return -1; 83 } 84 } 85 #endif /* FUNC_MKDIR_DOT_BUG */ 86 87 ret_val = mkdir (tmp_dir, mode); 88 89 if (tmp_dir != dir) 90 free (tmp_dir); 91 92 return ret_val; 93 }