3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
29 static enum { mkdirFAIL, mkdirSUCCESS, mkdirSKIP }
30 mkdirSingle(char const *path, char *end_ptr, int good_err)
33 if (mkdir(path, 0700)!=-1 || errno==EEXIST) {
37 else if (errno==good_err) {
46 rstrchr(char *str, char c)
48 while (*str!=c) --str;
53 mkdirRecursive(char const *path)
55 char buf[strlen(path)+1];
56 char * ptr = buf + sizeof(buf) - 2;
58 if (path[0]!='/') return false; // only absolute paths
62 while (ptr>buf && (ptr = rstrchr(ptr, '/'))!=0) {
63 switch (mkdirSingle(buf, ptr, ENOENT)) {
64 case mkdirSUCCESS : break;
65 case mkdirSKIP : --ptr; continue;
66 case mkdirFAIL : return false;
69 break; // implied by mkdirSUCCESS
75 while ((ptr=strchr(ptr, '/'))!=0) {
76 switch (mkdirSingle(buf, ptr, 0)) {
78 case mkdirFAIL : return false;
79 case mkdirSUCCESS : ++ptr; continue;