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