]>
git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/mkdir.c
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
, nmode
, parent_mode
;
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
);
96 /* initial bits are a=rwx; the mode argument modifies them */
97 omode
= parse_symbolic_mode (mode
, S_IRWXU
| S_IRWXG
| S_IRWXO
);
100 builtin_error ("invalid file mode: %s", mode
);
101 return (EXECUTION_FAILURE
);
105 /* Make the new mode */
106 original_umask
= umask (0);
107 umask (original_umask
);
109 nmode
= (S_IRWXU
| S_IRWXG
| S_IRWXO
) & ~original_umask
;
110 parent_mode
= nmode
| (S_IWUSR
|S_IXUSR
); /* u+wx */
112 /* Adjust new mode based on mode argument */
115 for (rval
= EXECUTION_SUCCESS
, l
= list
; l
; l
= l
->next
)
117 if (pflag
&& make_path (l
->word
->word
, nmode
, parent_mode
))
119 rval
= EXECUTION_FAILURE
;
122 else if (pflag
== 0 && mkdir (l
->word
->word
, nmode
) < 0)
124 builtin_error ("cannot create directory `%s': %s", l
->word
->word
, strerror (errno
));
125 rval
= EXECUTION_FAILURE
;
131 /* Make all the directories leading up to PATH, then create PATH. Note that
132 this changes the process's umask; make sure that all paths leading to a
133 return reset it to ORIGINAL_UMASK */
135 make_path (path
, nmode
, parent_mode
)
137 int nmode
, parent_mode
;
143 if (stat (path
, &sb
) == 0)
145 if (S_ISDIR (sb
.st_mode
) == 0)
147 builtin_error ("`%s': file exists but is not a directory", path
);
151 if (chmod (path
, nmode
))
153 builtin_error ("%s: %s", path
, strerror (errno
));
161 npath
= savestring (path
); /* So we can write to it. */
163 /* Check whether or not we need to do anything with intermediate dirs. */
165 /* Skip leading slashes. */
170 while (p
= strchr (p
, '/'))
173 if (stat (npath
, &sb
) != 0)
175 if (mkdir (npath
, parent_mode
))
177 builtin_error ("cannot create directory `%s': %s", npath
, strerror (errno
));
178 umask (original_umask
);
183 else if (S_ISDIR (sb
.st_mode
) == 0)
185 builtin_error ("`%s': file exists but is not a directory", npath
);
186 umask (original_umask
);
191 *p
++ = '/'; /* restore slash */
196 /* Create the final directory component. */
197 if (stat (npath
, &sb
) && mkdir (npath
, nmode
))
199 builtin_error ("cannot create directory `%s': %s", npath
, strerror (errno
));
200 umask (original_umask
);
205 umask (original_umask
);
210 char *mkdir_doc
[] = {
211 "Create directories.",
213 "Make directories. Create the directories named as arguments, in",
214 "the order specified, using mode rwxrwxrwx as modified by the current",
215 "umask (see `help umask'). The -m option causes the file permission",
216 "bits of the final directory to be MODE. The MODE argument may be",
217 "an octal number or a symbolic mode like that used by chmod(1). If",
218 "a symbolic mode is used, the operations are interpreted relative to",
219 "an initial mode of \"a=rwx\". The -p option causes any required",
220 "intermediate directories in PATH to be created. The directories",
221 "are created with permssion bits of rwxrwxrwx as modified by the current",
222 "umask, plus write and search permissions for the owner. mkdir",
223 "returns 0 if the directories are created successfully, and non-zero",
224 "if an error occurs.",
228 struct builtin mkdir_struct
= {
233 "mkdir [-p] [-m mode] directory [directory ...]",