3 * makedir.c -- make a directory
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.
11 * Modified to try recursive create.
16 #include <sys/param.h>
23 char* folder_perms_ASCII;
25 mode_t folder_perms, saved_umask;
29 context_save(); /* save the context file */
32 if (!(folder_perms_ASCII = context_find ("folder-protect")))
33 folder_perms_ASCII = foldprot; /* defaults to "700" */
35 /* Because mh-profile.man documents "Folder-Protect:" as an octal constant,
36 and we don't want to force the user to remember to include a leading
37 zero, we call atooi(folder_perms_ASCII) here rather than
38 strtoul(folder_perms_ASCII, NULL, 0). Therefore, if anyone ever tries to
39 specify a mode in say, hex, they'll get garbage. (I guess nmh uses its
40 atooi() function rather than calling strtoul() with a radix of 8 because
41 some ancient platforms are missing that functionality. */
42 folder_perms = atooi(folder_perms_ASCII);
44 /* Folders have definite desired permissions that are set -- we don't want
45 to interact with the umask. Clear it temporarily. */
46 saved_umask = umask(0);
48 if (getuid () == geteuid ()) {
49 c = strncpy(path, dir, sizeof(path));
51 while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
53 if (access(path, X_OK)) {
55 advise (dir, "unable to create directory");
58 /* Create an outer directory. */
59 if (mkdir(path, folder_perms)) {
60 advise (dir, "unable to create directory");
68 /* Create the innermost nested subdirectory of the path we're being
70 if (mkdir (dir, folder_perms) == -1) {
71 advise (dir, "unable to create directory");
77 /* Ummm, why do we want to avoid creating directories with the effective
78 user ID? None of the nmh tools are installed such that the effective
79 should be different from the real, and if some parent process made
80 the two be different, I don't see why it should be our job to enforce
81 the real UID. Also, why the heck do we call the mkdir executable
82 rather than the library function in this case?? If we do want to
83 call the mkdir executable, we should at least be giving it -p (and
84 change the single chmod() call below) so it can successfully create
85 nested directories like the above code can.
87 -- Dan Harkless <dan-nmh@dilvish.speed.net> */
88 switch (pid = vfork()) {
90 advise ("fork", "unable to");
97 execl ("/bin/mkdir", "mkdir", dir, NULL);
98 execl ("/usr/bin/mkdir", "mkdir", dir, NULL);
99 fprintf (stderr, "unable to exec ");
104 if (pidXwait(pid, "mkdir"))
109 chmod (dir, folder_perms);
112 umask(saved_umask); /* put the user's umask back */
115 return 0; /* opposite of UNIX error return convention */