Rename SAFE_STAT to safe_stat.
[gnulib.git] / lib / mkdir.c
1 /* mkdir.c -- BSD compatible make directory function 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 #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 #ifdef 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 #include "safe-stat.h"
38
39 /* mkdir adapted from GNU tar.  */
40
41 /* Make directory DPATH, with permission mode DMODE.
42
43    Written by Robert Rother, Mariah Corporation, August 1985
44    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
45
46    Severely hacked over by John Gilmore to make a 4.2BSD compatible
47    subroutine.  11Mar86; hoptoad!gnu
48
49    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
50    subroutine didn't return EEXIST.  It does now.  */
51
52 int
53 mkdir (dpath, dmode)
54      char *dpath;
55      int dmode;
56 {
57   int cpid, status;
58   struct stat statbuf;
59
60   if (safe_stat (dpath, &statbuf) == 0)
61     {
62       errno = EEXIST;           /* stat worked, so it already exists.  */
63       return -1;
64     }
65
66   /* If stat fails for a reason other than non-existence, return error.  */
67   if (errno != ENOENT)
68     return -1;
69
70   cpid = fork ();
71   switch (cpid)
72     {
73     case -1:                    /* Cannot fork.  */
74       return -1;                /* errno is already set.  */
75
76     case 0:                     /* Child process.  */
77       /* Cheap hack to set mode of new directory.  Since this child
78          process is going away anyway, we zap its umask.
79          This won't suffice to set SUID, SGID, etc. on this
80          directory, so the parent process calls chmod afterward.  */
81       status = umask (0);       /* Get current umask.  */
82       umask (status | (0777 & ~dmode)); /* Set for mkdir.  */
83       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
84       _exit (1);
85
86     default:                    /* Parent process.  */
87       /* Wait for kid to finish.  */
88       while (wait (&status) != cpid)
89         /* Do nothing.  */ ;
90
91       if (status & 0xFFFF)
92         {
93           /* /bin/mkdir failed.  */
94           errno = EIO;
95           return -1;
96         }
97       return chmod (dpath, dmode);
98     }
99 }