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");
82 /* Ummm, why do we want to avoid creating directories with the effective
83 user ID? None of the nmh tools are installed such that the effective
84 should be different from the real, and if some parent process made
85 the two be different, I don't see why it should be our job to enforce
86 the real UID. Also, why the heck do we call the mkdir executable
87 rather than the library function in this case?? If we do want to
88 call the mkdir executable, we should at least be giving it -p (and
89 change the single chmod() call below) so it can successfully create
90 nested directories like the above code can.
92 -- Dan Harkless <dan-nmh@dilvish.speed.net> */
93 switch (pid = vfork()) {
95 advise ("fork", "unable to");
102 execl ("/bin/mkdir", "mkdir", dir, NULL);
103 execl ("/usr/bin/mkdir", "mkdir", dir, NULL);
104 fprintf (stderr, "unable to exec ");
109 if (pidXwait(pid, "mkdir"))
114 chmod (dir, folder_perms);
117 umask(saved_umask); /* put the user's umask back */
120 return 0; /* opposite of UNIX error return convention */