28c89326538865a413290d058eaaec44ef3ad3bc
[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 #ifdef HAVE_CONFIG_H
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22    (which it would do because it found this file in $srcdir).  */
23 #include <config.h>
24 #else
25 #include "config.h"
26 #endif
27 #endif
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #ifndef STDC_HEADERS
33 extern int errno;
34 #endif
35
36 #ifdef  STAT_MACROS_BROKEN
37 #ifdef S_ISDIR
38 #undef S_ISDIR
39 #endif
40 #endif  /* STAT_MACROS_BROKEN.  */
41
42 #if !defined(S_ISDIR) && defined(S_IFDIR)
43 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
44 #endif
45
46 /* mkdir and rmdir adapted from GNU tar.  */
47
48 /* Make directory DPATH, with permission mode DMODE.
49
50    Written by Robert Rother, Mariah Corporation, August 1985
51    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
52
53    Severely hacked over by John Gilmore to make a 4.2BSD compatible
54    subroutine.  11Mar86; hoptoad!gnu
55
56    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
57    subroutine didn't return EEXIST.  It does now.  */
58
59 int
60 mkdir (dpath, dmode)
61      char *dpath;
62      int dmode;
63 {
64   int cpid, status;
65   struct stat statbuf;
66
67   if (stat (dpath, &statbuf) == 0)
68     {
69       errno = EEXIST;           /* stat worked, so it already exists.  */
70       return -1;
71     }
72
73   /* If stat fails for a reason other than non-existence, return error.  */
74   if (errno != ENOENT)
75     return -1;
76
77   cpid = fork ();
78   switch (cpid)
79     {
80     case -1:                    /* Cannot fork.  */
81       return -1;                /* errno is set already.  */
82
83     case 0:                     /* Child process.  */
84       /* Cheap hack to set mode of new directory.  Since this child
85          process is going away anyway, we zap its umask.
86          This won't suffice to set SUID, SGID, etc. on this
87          directory, so the parent process calls chmod afterward.  */
88       status = umask (0);       /* Get current umask.  */
89       umask (status | (0777 & ~dmode)); /* Set for mkdir.  */
90       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
91       _exit (1);
92
93     default:                    /* Parent process.  */
94       while (wait (&status) != cpid) /* Wait for kid to finish.  */
95         /* Do nothing.  */ ;
96
97       if (status & 0xFFFF)
98         {
99           errno = EIO;          /* /bin/mkdir failed.  */
100           return -1;
101         }
102       return chmod (dpath, dmode);
103     }
104 }
105
106 /* Remove directory DPATH.
107    Return 0 if successful, -1 if not.  */
108
109 int
110 rmdir (dpath)
111      char *dpath;
112 {
113   int cpid, status;
114   struct stat statbuf;
115
116   if (stat (dpath, &statbuf) != 0)
117     return -1;                  /* stat set errno.  */
118
119   if (!S_ISDIR (statbuf.st_mode))
120     {
121       errno = ENOTDIR;
122       return -1;
123     }
124
125   cpid = fork ();
126   switch (cpid)
127     {
128     case -1:                    /* Cannot fork.  */
129       return -1;                /* errno is set already.  */
130
131     case 0:                     /* Child process.  */
132       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
133       _exit (1);
134
135     default:                    /* Parent process.  */
136       while (wait (&status) != cpid) /* Wait for kid to finish.  */
137         /* Do nothing.  */ ;
138
139       if (status & 0xFFFF)
140         {
141           errno = EIO;          /* /bin/rmdir failed.  */
142           return -1;
143         }
144       return 0;
145     }
146 }