]> diplodocus.org Git - nmh/blob - sbr/makedir.c
Improve fmttest output
[nmh] / sbr / makedir.c
1 /* makedir.c -- make a directory
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 /*
9 * Modified to try recursive create.
10 */
11
12 #include "h/mh.h"
13 #include "context_save.h"
14 #include "context_find.h"
15 #include "atooi.h"
16 #include "error.h"
17 #include "makedir.h"
18 #include <sys/file.h>
19
20 int
21 makedir (const char *dir)
22 {
23 char path[PATH_MAX];
24 char* folder_perms_ASCII;
25 mode_t folder_perms, saved_umask;
26 char* c;
27
28 context_save(); /* save the context file */
29 fflush(stdout);
30
31 if (!(folder_perms_ASCII = context_find ("folder-protect")))
32 folder_perms_ASCII = foldprot; /* defaults to "700" */
33
34 /* Because mh-profile.man documents "Folder-Protect:" as an octal constant,
35 and we don't want to force the user to remember to include a leading
36 zero, we call atooi(folder_perms_ASCII) here rather than
37 strtoul(folder_perms_ASCII, NULL, 0). Therefore, if anyone ever tries to
38 specify a mode in say, hex, they'll get garbage. (I guess nmh uses its
39 atooi() function rather than calling strtoul() with a radix of 8 because
40 some ancient platforms are missing that functionality. */
41 folder_perms = atooi(folder_perms_ASCII);
42
43 /* Folders have definite desired permissions that are set -- we don't want
44 to interact with the umask. Clear it temporarily. */
45 saved_umask = umask(0);
46
47 c = strncpy(path, dir, sizeof(path));
48
49 bool had_an_error = false;
50 while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
51 *c = '\0';
52 if (access(path, X_OK)) {
53 if (errno != ENOENT){
54 advise (dir, "unable to create directory");
55 had_an_error = true;
56 }
57 /* Create an outer directory. */
58 if (mkdir(path, folder_perms)) {
59 advise (dir, "unable to create directory");
60 had_an_error = true;
61 }
62 }
63 *c = '/';
64 }
65
66 if (!had_an_error) {
67 /* Create the innermost nested subdirectory of the path we're being
68 asked to create. */
69 if (mkdir (dir, folder_perms) == -1) {
70 advise (dir, "unable to create directory");
71 had_an_error = true;
72 }
73 }
74
75 umask(saved_umask); /* put the user's umask back */
76
77 if (had_an_error)
78 return 0; /* opposite of UNIX error return convention */
79 return 1;
80 }