Replaced atooi() with strtoul(..., 8). That's part of C89 and thus okay for us.
[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
18 int
19 makedir(char *dir)
20 {
21         char path[PATH_MAX];
22         char *cp;
23         int had_an_error = 0;
24         mode_t folder_perms, saved_umask;
25         register char* c;
26
27         context_save();  /* save the context file */
28         fflush(stdout);
29
30         if (!(cp = context_find("folder-protect")) || !*cp) {
31                 cp = foldprot;
32         }
33         folder_perms = strtoul(cp, NULL, 8);
34
35         /*
36         ** Folders have definite desired permissions that are set -- we
37         ** don't want to interact with the umask.  Clear it temporarily.
38         */
39         saved_umask = umask(0);
40
41         c = strncpy(path, dir, sizeof(path));
42
43         while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) {
44                 *c = '\0';
45                 /* Create an outer directory. */
46                 if (mkdir(path, folder_perms) == -1 &&
47                                 errno != EEXIST) {
48                         advise(dir, "unable to create directory");
49                         had_an_error = 1;
50                 }
51                 *c = '/';
52         }
53
54         /*
55         ** Create the innermost nested subdirectory of the
56         ** path we're being asked to create.
57         */
58         if (!had_an_error && mkdir(dir, folder_perms)==-1) {
59                 advise(dir, "unable to create directory");
60                 had_an_error = 1;
61         }
62         umask(saved_umask);  /* put the user's umask back */
63
64         return (had_an_error) ? 0 : 1;
65 }