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