(mkdir): Use pid_t instead of int; check status
[gnulib.git] / lib / mkdir.c
1 /* BSD compatible make directory function for System V
2    Copyright (C) 1988, 1990, 1998 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #ifndef errno
26 extern int errno;
27 #endif
28
29 #if STAT_MACROS_BROKEN
30 # undef S_ISDIR
31 #endif
32
33 #if !defined(S_ISDIR) && defined(S_IFDIR)
34 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
35 #endif
36
37 /* mkdir adapted from GNU tar.  */
38
39 /* Make directory DPATH, with permission mode DMODE.
40
41    Written by Robert Rother, Mariah Corporation, August 1985
42    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
43
44    Severely hacked over by John Gilmore to make a 4.2BSD compatible
45    subroutine.  11Mar86; hoptoad!gnu
46
47    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
48    subroutine didn't return EEXIST.  It does now.  */
49
50 int
51 mkdir (const char *dpath, int dmode)
52 {
53   pid_t cpid;
54   int status;
55   struct stat statbuf;
56
57   if (stat (dpath, &statbuf) == 0)
58     {
59       errno = EEXIST;           /* stat worked, so it already exists.  */
60       return -1;
61     }
62
63   /* If stat fails for a reason other than non-existence, return error.  */
64   if (errno != ENOENT)
65     return -1;
66
67   cpid = fork ();
68   switch (cpid)
69     {
70     case -1:                    /* Cannot fork.  */
71       return -1;                /* errno is already set.  */
72
73     case 0:                     /* Child process.  */
74       /* Cheap hack to set mode of new directory.  Since this child
75          process is going away anyway, we zap its umask.
76          This won't suffice to set SUID, SGID, etc. on this
77          directory, so the parent process calls chmod afterward.  */
78       status = umask (0);       /* Get current umask.  */
79       umask (status | (0777 & ~dmode)); /* Set for mkdir.  */
80       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
81       _exit (1);
82
83     default:                    /* Parent process.  */
84       /* Wait for kid to finish.  */
85       while (wait (&status) != cpid)
86         /* Do nothing.  */ ;
87
88       if (status)
89         {
90           /* /bin/mkdir failed.  */
91           errno = EIO;
92           return -1;
93         }
94       return chmod (dpath, dmode);
95     }
96 }