b00199d3fa76c490acea8a590d0d5a3d03d7d46d
[pspp] / lib / mkdir.c
1 /* mkdir.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 #include "safe-stat.h"
47
48 /* mkdir and rmdir adapted from GNU tar.  */
49
50 /* Make directory DPATH, with permission mode DMODE.
51
52    Written by Robert Rother, Mariah Corporation, August 1985
53    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
54
55    Severely hacked over by John Gilmore to make a 4.2BSD compatible
56    subroutine.  11Mar86; hoptoad!gnu
57
58    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
59    subroutine didn't return EEXIST.  It does now.  */
60
61 int
62 mkdir (dpath, dmode)
63      char *dpath;
64      int dmode;
65 {
66   int cpid, status;
67   struct stat statbuf;
68
69   if (SAFE_STAT (dpath, &statbuf) == 0)
70     {
71       errno = EEXIST;           /* stat worked, so it already exists.  */
72       return -1;
73     }
74
75   /* If stat fails for a reason other than non-existence, return error.  */
76   if (errno != ENOENT)
77     return -1;
78
79   cpid = fork ();
80   switch (cpid)
81     {
82     case -1:                    /* Cannot fork.  */
83       return -1;                /* errno is set already.  */
84
85     case 0:                     /* Child process.  */
86       /* Cheap hack to set mode of new directory.  Since this child
87          process is going away anyway, we zap its umask.
88          This won't suffice to set SUID, SGID, etc. on this
89          directory, so the parent process calls chmod afterward.  */
90       status = umask (0);       /* Get current umask.  */
91       umask (status | (0777 & ~dmode)); /* Set for mkdir.  */
92       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
93       _exit (1);
94
95     default:                    /* Parent process.  */
96       while (wait (&status) != cpid) /* Wait for kid to finish.  */
97         /* Do nothing.  */ ;
98
99       if (status & 0xFFFF)
100         {
101           errno = EIO;          /* /bin/mkdir failed.  */
102           return -1;
103         }
104       return chmod (dpath, dmode);
105     }
106 }
107
108 /* Remove directory DPATH.
109    Return 0 if successful, -1 if not.  */
110
111 int
112 rmdir (dpath)
113      char *dpath;
114 {
115   int cpid, status;
116   struct stat statbuf;
117
118   if (SAFE_STAT (dpath, &statbuf) != 0)
119     return -1;                  /* stat set errno.  */
120
121   if (!S_ISDIR (statbuf.st_mode))
122     {
123       errno = ENOTDIR;
124       return -1;
125     }
126
127   cpid = fork ();
128   switch (cpid)
129     {
130     case -1:                    /* Cannot fork.  */
131       return -1;                /* errno is set already.  */
132
133     case 0:                     /* Child process.  */
134       execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
135       _exit (1);
136
137     default:                    /* Parent process.  */
138       while (wait (&status) != cpid) /* Wait for kid to finish.  */
139         /* Do nothing.  */ ;
140
141       if (status & 0xFFFF)
142         {
143           errno = EIO;          /* /bin/rmdir failed.  */
144           return -1;
145         }
146       return 0;
147     }
148 }