68e6b6327a921c98c288058753c812d79dd2b59f
[gnulib.git] / lib / mkdir.c
1 /* mkrmdir.c -- BSD compatible directory functions for System V
2    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #ifndef STDC_HEADERS
22 extern int errno;
23 #endif
24
25 #ifdef  STAT_MACROS_BROKEN
26 #ifdef S_ISDIR
27 #undef S_ISDIR
28 #endif
29 #endif  /* STAT_MACROS_BROKEN.  */
30
31 #if !defined(S_ISDIR) && defined(S_IFDIR)
32 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
33 #endif
34
35 /* mkdir and rmdir adapted from GNU tar.  */
36
37 /* Make directory DPATH, with permission mode DMODE.
38
39    Written by Robert Rother, Mariah Corporation, August 1985
40    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
41
42    Severely hacked over by John Gilmore to make a 4.2BSD compatible
43    subroutine.  11Mar86; hoptoad!gnu
44
45    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
46    subroutine didn't return EEXIST.  It does now.  */
47
48 int
49 mkdir (dpath, dmode)
50      char *dpath;
51      int dmode;
52 {
53   int cpid, status;
54   struct stat statbuf;
55
56   if (stat (dpath, &statbuf) == 0)
57     {
58       errno = EEXIST;           /* stat worked, so it already exists.  */
59       return -1;
60     }
61
62   /* If stat fails for a reason other than non-existence, return error.  */
63   if (errno != ENOENT)
64     return -1;
65
66   cpid = fork ();
67   switch (cpid)
68     {
69     case -1:                    /* Cannot fork.  */
70       return -1;                /* errno is set already.  */
71
72     case 0:                     /* Child process.  */
73       /* Cheap hack to set mode of new directory.  Since this child
74          process is going away anyway, we zap its umask.
75          This won't suffice to set SUID, SGID, etc. on this
76          directory, so the parent process calls chmod afterward.  */
77       status = umask (0);       /* Get current umask.  */
78       umask (status | (0777 & ~dmode)); /* Set for mkdir.  */
79       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
80       _exit (1);
81
82     default:                    /* Parent process.  */
83       while (wait (&status) != cpid) /* Wait for kid to finish.  */
84         /* Do nothing.  */ ;
85
86       if (status & 0xFFFF)
87         {
88           errno = EIO;          /* /bin/mkdir failed.  */
89           return -1;
90         }
91       return chmod (dpath, dmode);
92     }
93 }
94
95 /* Remove directory DPATH.
96    Return 0 if successful, -1 if not.  */
97
98 int
99 rmdir (dpath)
100      char *dpath;
101 {
102   int cpid, status;
103   struct stat statbuf;
104
105   if (stat (dpath, &statbuf) != 0)
106     return -1;                  /* stat set errno.  */
107
108   if (!S_ISDIR (statbuf.st_mode))
109     {
110       errno = ENOTDIR;
111       return -1;
112     }
113
114   cpid = fork ();
115   switch (cpid)
116     {
117     case -1:                    /* Cannot fork.  */
118       return -1;                /* errno is set already.  */
119
120     case 0:                     /* Child process.  */
121       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
122       _exit (1);
123
124     default:                    /* Parent process.  */
125       while (wait (&status) != cpid) /* Wait for kid to finish.  */
126         /* Do nothing.  */ ;
127
128       if (status & 0xFFFF)
129         {
130           errno = EIO;          /* /bin/rmdir failed.  */
131           return -1;
132         }
133       return 0;
134     }
135 }