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