remove unused defines in uip/pick.c
[mmh] / sbr / makedir.c
1 /*
2 ** makedir.c -- make a directory
3 **
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.
7 */
8
9 /*
10 ** Modified to try recursive create.
11 */
12
13 #include <h/mh.h>
14 #include <errno.h>
15 #include <sys/param.h>
16 #include <sys/file.h>
17 #include <sys/stat.h>
18
19 int
20 makedir(char *dir)
21 {
22         char path[PATH_MAX];
23         char *cp;
24         int had_an_error = 0;
25         mode_t folder_perms, saved_umask;
26         char* c;
27
28         context_save();  /* save the context file */
29         fflush(stdout);
30
31         if (!(cp = context_find("folder-protect")) || !*cp) {
32                 cp = foldprot;
33         }
34         folder_perms = strtoul(cp, NULL, 8);
35
36         /*
37         ** Folders have definite desired permissions that are set -- we
38         ** don't want to interact with the umask.  Clear it temporarily.
39         */
40         saved_umask = umask(0);
41
42         c = strncpy(path, dir, sizeof(path));
43
44         while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
45                 *c = '\0';
46                 /* Create an outer directory. */
47                 if (mkdir(path, folder_perms) == -1 &&
48                                 errno != EEXIST) {
49                         advise(dir, "unable to create directory");
50                         had_an_error = 1;
51                 }
52                 *c = '/';
53         }
54
55         /*
56         ** Create the innermost nested subdirectory of the
57         ** path we're being asked to create.
58         */
59         if (!had_an_error && mkdir(dir, folder_perms)==-1) {
60                 advise(dir, "unable to create directory");
61                 had_an_error = 1;
62         }
63         umask(saved_umask);  /* put the user's umask back */
64
65         return (had_an_error) ? 0 : 1;
66 }