1 This file is umask.def, from which is created umask.c.
2 It implements the builtin "umask" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION umask_builtin
25 $SHORT_DOC umask [-p] [-S] [mode]
26 Display or set file mode mask.
28 Sets the user file-creation mask to MODE. If MODE is omitted, prints
29 the current value of the mask.
31 If MODE begins with a digit, it is interpreted as an octal number;
32 otherwise it is a symbolic mode string like that accepted by chmod(1).
35 -p if MODE is omitted, output in a form that may be reused as input
36 -S makes the output symbolic; otherwise an octal number is output
39 Returns success unless MODE is invalid or an invalid option is given.
44 #include "../bashtypes.h"
46 #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
47 # include <sys/file.h>
50 #if defined (HAVE_UNISTD_H)
55 #include <chartypes.h>
57 #include "../bashintl.h"
60 #include "posixstat.h"
62 #include "bashgetopt.h"
64 /* **************************************************************** */
66 /* UMASK Builtin and Helpers */
68 /* **************************************************************** */
70 static void print_symbolic_umask __P((mode_t));
71 static int symbolic_umask __P((WORD_LIST *));
73 /* Set or display the mask used by the system when creating files. Flag
74 of -S means display the umask in a symbolic mode. */
79 int print_symbolically, opt, umask_value, pflag;
82 print_symbolically = pflag = 0;
83 reset_internal_getopt ();
84 while ((opt = internal_getopt (list, "Sp")) != -1)
104 if (DIGIT (*list->word->word))
106 umask_value = read_octal (list->word->word);
108 /* Note that other shells just let you set the umask to zero
109 by specifying a number out of range. This is a problem
110 with those shells. We don't change the umask if the input
112 if (umask_value == -1)
114 sh_erange (list->word->word, _("octal number"));
115 return (EXECUTION_FAILURE);
120 umask_value = symbolic_umask (list);
121 if (umask_value == -1)
122 return (EXECUTION_FAILURE);
124 umask_arg = (mode_t)umask_value;
126 if (print_symbolically)
127 print_symbolic_umask (umask_arg);
129 else /* Display the UMASK for this user. */
131 umask_arg = umask (022);
135 printf ("umask%s ", (print_symbolically ? " -S" : ""));
136 if (print_symbolically)
137 print_symbolic_umask (umask_arg);
139 printf ("%04lo\n", (unsigned long)umask_arg);
142 return (sh_chkwrite (EXECUTION_SUCCESS));
145 /* Print the umask in a symbolic form. In the output, a letter is
146 printed if the corresponding bit is clear in the umask. */
148 print_symbolic_umask (um)
151 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
155 if ((um & S_IRUSR) == 0)
157 if ((um & S_IWUSR) == 0)
159 if ((um & S_IXUSR) == 0)
164 if ((um & S_IRGRP) == 0)
166 if ((um & S_IWGRP) == 0)
168 if ((um & S_IXGRP) == 0)
173 if ((um & S_IROTH) == 0)
175 if ((um & S_IWOTH) == 0)
177 if ((um & S_IXOTH) == 0)
181 printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits);
185 parse_symbolic_mode (mode, initial_bits)
189 int who, op, perm, bits, c;
192 for (s = mode, bits = initial_bits;;)
196 /* Parse the `who' portion of the symbolic mode clause. */
197 while (member (*s, "agou"))
211 who |= S_IRWXU | S_IRWXG | S_IRWXO;
218 /* The operation is now sitting in *s. */
227 builtin_error (_("`%c': invalid symbolic mode operator"), op);
231 /* Parse out the `perm' section of the symbolic mode clause. */
232 while (member (*s, "rwx"))
250 /* Now perform the operation or return an error for a
251 bad permission string. */
252 if (!*s || *s == ',')
267 who = S_IRWXU | S_IRWXG | S_IRWXO;
272 /* No other values are possible. */
278 s++; /* skip past ',' */
282 builtin_error (_("`%c': invalid symbolic mode character"), *s);
290 /* Set the umask from a symbolic mode string similar to that accepted
291 by chmod. If the -S argument is given, then print the umask in a
294 symbolic_umask (list)
299 /* Get the initial umask. Don't change it yet. */
303 /* All work is done with the complement of the umask -- it's
304 more intuitive and easier to deal with. It is complemented
305 again before being returned. */
306 bits = parse_symbolic_mode (list->word->word, ~um & 0777);