3 * makedir.c -- make a directory
9 * Modified to try recursive create.
14 #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 "0700" */
35 /* Call strtoul() with radix=0, which means it'll determine the base by
36 looking at the first digit. That way it'll know "0700" is an octal
37 constant (and if someone gets wacky and changes the representation to hex
39 folder_perms = strtoul(folder_perms_ASCII, NULL, 0);
41 /* Folders have definite desired permissions that are set -- we don't want
42 to interact with the umask. Clear it temporarily. */
43 saved_umask = umask(0);
45 if (getuid () == geteuid ()) {
46 c = strncpy(path, dir, sizeof(path));
48 while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
50 if (access(path, X_OK)) {
52 advise (dir, "unable to create directory");
55 /* Create an outer directory. */
56 if (mkdir(path, folder_perms)) {
57 advise (dir, "unable to create directory");
65 /* Create the innermost nested subdirectory of the path we're being
67 if (mkdir (dir, folder_perms) == -1) {
68 advise (dir, "unable to create directory");
74 /* Ummm, why do we want to avoid creating directories with the effective
75 user ID? None of the nmh tools are installed such that the effective
76 should be different from the real, and if some parent process made
77 the two be different, I don't see why it should be our job to enforce
78 the real UID. Also, why the heck do we call the mkdir executable
79 rather than the library function in this case?? If we do want to
80 call the mkdir executable, we should at least be giving it -p (and
81 change the single chmod() call below) so it can successfully create
82 nested directories like the above code can.
84 -- Dan Harkless <dan-nmh@dilvish.speed.net> */
85 switch (pid = vfork()) {
87 advise ("fork", "unable to");
94 execl ("/bin/mkdir", "mkdir", dir, NULL);
95 execl ("/usr/bin/mkdir", "mkdir", dir, NULL);
96 fprintf (stderr, "unable to exec ");
101 if (pidXwait(pid, "mkdir"))
106 chmod (dir, folder_perms);
109 umask(saved_umask); /* put the user's umask back */
112 return 0; /* opposite of UNIX error return convention */