1 /* mkdir - make directories */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #include "bashtypes.h"
26 #include "posixstat.h"
30 #if defined (HAVE_UNISTD_H)
36 #include "bashgetopt.h"
43 #define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
45 extern int parse_symbolic_mode ();
47 static int make_path ();
49 static int original_umask
;
55 int opt
, pflag
, omode
, rval
, octal
, nmode
, parent_mode
, um
;
59 reset_internal_getopt ();
62 while ((opt
= internal_getopt(list
, "m:p")) != -1)
84 omode
= S_IRWXU
| S_IRWXG
| S_IRWXO
; /* a=rwx */
85 else if (ISOCTAL (*mode
)) /* octal number */
87 omode
= read_octal (mode
);
90 builtin_error ("invalid file mode: %s", mode
);
91 return (EXECUTION_FAILURE
);
97 /* initial bits are a=rwx; the mode argument modifies them */
98 omode
= parse_symbolic_mode (mode
, S_IRWXU
| S_IRWXG
| S_IRWXO
);
101 builtin_error ("invalid file mode: %s", mode
);
102 return (EXECUTION_FAILURE
);
107 /* Make the new mode */
108 original_umask
= umask (0);
109 umask (original_umask
);
111 nmode
= (S_IRWXU
| S_IRWXG
| S_IRWXO
) & ~original_umask
;
112 parent_mode
= nmode
| (S_IWRITE
|S_IEXEC
); /* u+wx */
114 /* Adjust new mode based on mode argument */
117 for (rval
= EXECUTION_SUCCESS
, l
= list
; l
; l
= l
->next
)
119 if (pflag
&& make_path (l
->word
->word
, nmode
, parent_mode
))
121 rval
= EXECUTION_FAILURE
;
124 else if (pflag
== 0 && mkdir (l
->word
->word
, nmode
) < 0)
126 builtin_error ("cannot create directory `%s': %s", l
->word
->word
, strerror (errno
));
127 rval
= EXECUTION_FAILURE
;
133 /* Make all the directories leading up to PATH, then create PATH. Note that
134 this changes the process's umask; make sure that all paths leading to a
135 return reset it to ORIGINAL_UMASK */
137 make_path (path
, nmode
, parent_mode
)
139 int nmode
, parent_mode
;
145 if (stat (path
, &sb
) == 0)
147 if (S_ISDIR (sb
.st_mode
) == 0)
149 builtin_error ("`%s': file exists but is not a directory", path
);
153 if (chmod (path
, nmode
))
155 builtin_error ("%s: %s", path
, strerror (errno
));
163 npath
= savestring (path
); /* So we can write to it. */
165 /* Check whether or not we need to do anything with intermediate dirs. */
167 /* Skip leading slashes. */
172 while (p
= strchr (p
, '/'))
175 if (stat (npath
, &sb
) != 0)
177 if (mkdir (npath
, parent_mode
))
179 builtin_error ("cannot create directory `%s': %s", npath
, strerror (errno
));
180 umask (original_umask
);
185 else if (S_ISDIR (sb
.st_mode
) == 0)
187 builtin_error ("`%s': file exists but is not a directory", npath
);
188 umask (original_umask
);
193 *p
++ = '/'; /* restore slash */
198 /* Create the final directory component. */
199 if (stat (npath
, &sb
) && mkdir (npath
, nmode
))
201 builtin_error ("cannot create directory `%s': %s", npath
, strerror (errno
));
202 umask (original_umask
);
207 umask (original_umask
);
212 char *mkdir_doc
[] = {
213 "Create directories.",
215 "Make directories. Create the directories named as arguments, in",
216 "the order specified, using mode rwxrwxrwx as modified by the current",
217 "umask (see `help umask'). The -m option causes the file permission",
218 "bits of the final directory to be MODE. The MODE argument may be",
219 "an octal number or a symbolic mode like that used by chmod(1). If",
220 "a symbolic mode is used, the operations are interpreted relative to",
221 "an initial mode of \"a=rwx\". The -p option causes any required",
222 "intermediate directories in PATH to be created. The directories",
223 "are created with permssion bits of rwxrwxrwx as modified by the current",
224 "umask, plus write and search permissions for the owner. mkdir",
225 "returns 0 if the directories are created successfully, and non-zero",
226 "if an error occurs.",
230 struct builtin mkdir_struct
= {
235 "mkdir [-p] [-m mode] directory [directory ...]",