Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / sbr / makedir.c
1 /* makedir.c - make a directory */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: makedir.c,v 1.13 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #if defined (BSD42) || defined (hpux)
7 /* Modified to try recursive create.  Really, this should be broken
8  * out into a subr so that SYS5 systems can do this too.  I don't 
9  * have a SYS5 machine around to test anymore, so someone else will
10  * have to send me the code.
11  */
12 #endif
13
14 #include "../h/mh.h"
15 #include <stdio.h>
16
17 #if defined (BSD42) || defined (hpux) || defined(SVR4) || \
18         defined(ncr) || defined (_AIX) || defined(AUX)
19 #include <errno.h>
20 #include <sys/param.h>
21 #include <sys/file.h>
22 #endif /* BSD42 ... */
23 #ifdef  SYS5DIR
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #endif  /* SYS5DIR */
27 #if defined(SVR4) || defined(ncr)
28 #include <unistd.h>
29 #endif
30
31 extern int  errno;
32         
33 makedir (dir)
34 register char *dir;
35 {
36     int     pid;
37     register char  *cp;
38 #if defined (BSD42)  || defined (hpux) || defined (SYS5DIR)
39     register char  *c;
40     char path[MAXPATHLEN];
41 #endif  /* BSD42 */
42
43     m_update ();
44     (void) fflush (stdout);
45
46 #if     defined (BSD42) ||  defined (hpux) || defined (SYS5DIR)
47     if (getuid () == geteuid ()) {
48             c = strcpy(path, dir);     
49
50             while ((c = index((c + 1), '/')) != NULL) { 
51                     *c = (char)0;
52                     if (access(path, X_OK)) {
53                             if (errno != ENOENT){
54                                     advise (dir, "unable to create directory");
55                                     return 0;
56                             }                       
57                             if (mkdir(path, 0775)) {
58                                     advise (dir, "unable to create directory");
59                                     return 0;
60                             }
61                     }
62                     *c = '/';
63             }
64  
65             if (mkdir (dir, 0755) == NOTOK) {
66                     advise (dir, "unable to create directory");
67                     return 0;
68             }
69     }
70     else
71 #endif  /* BSD42 or hpux or SYS5DIR */
72     switch (pid = vfork ()) {
73         case NOTOK: 
74             advise ("fork", "unable to");
75             return 0;
76
77         case OK: 
78             (void) setgid (getgid ());
79             (void) setuid (getuid ());
80
81             execl ("/bin/mkdir", "mkdir", dir, NULLCP);
82             execl ("/usr/bin/mkdir", "mkdir", dir, NULLCP);
83             fprintf (stderr, "unable to exec ");
84             perror ("mkdir");
85             _exit (-1);
86
87         default: 
88             if (pidXwait (pid, "mkdir"))
89                 return 0;
90             break;
91     }
92
93     if ((cp = m_find ("folder-protect")) == NULL)
94         cp = foldprot;
95     (void) chmod (dir, atooi (cp));
96     return 1;
97 }
98