chiark / gitweb /
Fix HOSTNAME thing in configure.in
[inn-innduct.git] / lib / makedir.c
1 #include "config.h"
2 #include "clibrary.h"
3 #include <errno.h>
4 #include <sys/stat.h>
5
6 #include "libinn.h"
7
8 /*
9 **  Try to make one directory.  Return false on error.
10 */
11 static bool MakeDir(char *Name)
12 {
13     struct stat         Sb;
14
15     if (mkdir(Name, GROUPDIR_MODE) >= 0) {
16         return true;
17     }
18
19     /* See if it failed because it already exists. */
20     if (stat(Name, &Sb) >= 0 && S_ISDIR(Sb.st_mode)) {
21         errno = 0;
22         return true;
23     }
24     return false;
25 }
26
27
28 /*
29 **  Given a directory, comp/foo/bar, create that directory and all
30 **  intermediate directories needed.  Return true if ok, else false.
31 */
32 bool MakeDirectory(char *Name, bool Recurse)
33 {
34     char                *p;
35     bool                made;
36
37     /* Optimize common case -- parent almost always exists. */
38     if (MakeDir(Name))
39         return true;
40
41     if (!Recurse)
42         return false;
43
44     /* Try to make each of comp and comp/foo in turn. */
45     for (p = (Name[0] == '/') ? &Name[1] : Name; *p; p++)
46         if (*p == '/') {
47             *p = '\0';
48             made = MakeDir(Name);
49             *p = '/';
50             if (!made)
51                 return false;
52         }
53
54     return MakeDir(Name);
55 }