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;
28 context_save(); /* save the context file */
31 if (!(folder_perms_ASCII = context_find("folder-protect")))
32 folder_perms_ASCII = foldprot; /* defaults to "700" */
35 ** Because mh-profile.man documents "Folder-Protect:" as an octal
36 ** constant, and we don't want to force the user to remember to
37 ** include a leading zero, we call atooi(folder_perms_ASCII) here
38 ** rather than strtoul(folder_perms_ASCII, NULL, 0). Therefore,
39 ** if anyone ever tries to specify a mode in say, hex, they'll
40 ** get garbage. (I guess nmh uses its atooi() function rather
41 ** than calling strtoul() with a radix of 8 because some ancient
42 ** platforms are missing that functionality.
44 folder_perms = atooi(folder_perms_ASCII);
47 ** Folders have definite desired permissions that are set -- we
48 ** don't want to interact with the umask. Clear it temporarily.
50 saved_umask = umask(0);
52 if (getuid() == geteuid()) {
53 c = strncpy(path, dir, sizeof(path));
55 while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
57 if (access(path, X_OK)) {
59 advise(dir, "unable to create directory");
62 /* Create an outer directory. */
63 if (mkdir(path, folder_perms)) {
64 advise(dir, "unable to create directory");
73 ** Create the innermost nested subdirectory of the
74 ** path we're being asked to create.
76 if (mkdir(dir, folder_perms) == -1) {
77 advise(dir, "unable to create directory");
83 ** Ummm, why do we want to avoid creating directories
84 ** with the effective user ID? None of the nmh tools are
85 ** installed such that the effective should be different
86 ** from the real, and if some parent process made the two
87 ** be different, I don't see why it should be our job to
88 ** enforce the real UID. Also, why the heck do we call
89 ** the mkdir executable rather than the library function in
90 ** this case?? If we do want to call the mkdir executable,
91 ** we should at least be giving it -p (and change the single
92 ** chmod() call below) so it can successfully create nested
93 ** directories like the above code can.
94 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
96 switch (pid = vfork()) {
98 advise("fork", "unable to");
105 execl("/bin/mkdir", "mkdir", dir, NULL);
106 execl("/usr/bin/mkdir", "mkdir", dir, NULL);
107 fprintf(stderr, "unable to exec ");
112 if (pidXwait(pid, "mkdir"))
117 chmod(dir, folder_perms);
120 umask(saved_umask); /* put the user's umask back */
123 return 0; /* opposite of UNIX error return convention */