2 ** makedir.c -- make a directory
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 ** Modified to try recursive create.
15 #include <sys/param.h>
22 char* folder_perms_ASCII;
24 mode_t folder_perms, saved_umask;
27 context_save(); /* save the context file */
30 if (!(folder_perms_ASCII = context_find("folder-protect")))
31 folder_perms_ASCII = foldprot; /* defaults to "700" */
34 ** Because mh-profile.man documents "Folder-Protect:" as an octal
35 ** constant, and we don't want to force the user to remember to
36 ** include a leading zero, we call atooi(folder_perms_ASCII) here
37 ** rather than strtoul(folder_perms_ASCII, NULL, 0). Therefore,
38 ** if anyone ever tries to specify a mode in say, hex, they'll
39 ** get garbage. (I guess nmh uses its atooi() function rather
40 ** than calling strtoul() with a radix of 8 because some ancient
41 ** platforms are missing that functionality.
43 folder_perms = atooi(folder_perms_ASCII);
46 ** Folders have definite desired permissions that are set -- we
47 ** don't want to interact with the umask. Clear it temporarily.
49 saved_umask = umask(0);
51 c = strncpy(path, dir, sizeof(path));
53 while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
55 /* Create an outer directory. */
56 if (mkdir(path, folder_perms) == -1 &&
58 advise(dir, "unable to create directory");
65 ** Create the innermost nested subdirectory of the
66 ** path we're being asked to create.
68 if (!had_an_error && mkdir(dir, folder_perms)==-1) {
69 advise(dir, "unable to create directory");
72 umask(saved_umask); /* put the user's umask back */
74 return (had_an_error) ? 0 : 1;