]>
diplodocus.org Git - nmh/blob - sbr/makedir.c
1 /* makedir.c -- make a directory
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.
9 * Modified to try recursive create.
13 #include "context_save.h"
14 #include "context_find.h"
21 makedir (const char *dir
)
24 char* folder_perms_ASCII
;
25 mode_t folder_perms
, saved_umask
;
28 context_save(); /* save the context file */
31 if (!(folder_perms_ASCII
= context_find ("folder-protect")))
32 folder_perms_ASCII
= foldprot
; /* defaults to "700" */
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
);
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);
47 c
= strncpy(path
, dir
, sizeof(path
));
49 bool had_an_error
= false;
50 while (!had_an_error
&& (c
= strchr((c
+ 1), '/')) != NULL
) {
52 if (access(path
, X_OK
)) {
54 advise (dir
, "unable to create directory");
57 /* Create an outer directory. */
58 if (mkdir(path
, folder_perms
)) {
59 advise (dir
, "unable to create directory");
67 /* Create the innermost nested subdirectory of the path we're being
69 if (mkdir (dir
, folder_perms
) == -1) {
70 advise (dir
, "unable to create directory");
75 umask(saved_umask
); /* put the user's umask back */
78 return 0; /* opposite of UNIX error return convention */