1 This file is umask.def, from which is created umask.c.
2 It implements the builtin "umask" in Bash.
4 Copyright (C) 1987-2004 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION umask_builtin
26 $SHORT_DOC umask [-p] [-S] [mode]
27 The user file-creation mask is set to MODE. If MODE is omitted, or if
28 `-S' is supplied, the current value of the mask is printed. The `-S'
29 option makes the output symbolic; otherwise an octal number is output.
30 If `-p' is supplied, and MODE is omitted, the output is in a form
31 that may be used as input. If MODE begins with a digit, it is
32 interpreted as an octal number, otherwise it is a symbolic mode string
33 like that accepted by chmod(1).
38 #include "../bashtypes.h"
40 #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
41 # include <sys/file.h>
44 #if defined (HAVE_UNISTD_H)
49 #include <chartypes.h>
51 #include "../bashintl.h"
54 #include "posixstat.h"
56 #include "bashgetopt.h"
62 /* **************************************************************** */
64 /* UMASK Builtin and Helpers */
66 /* **************************************************************** */
68 static void print_symbolic_umask __P((mode_t));
69 static int symbolic_umask __P((WORD_LIST *));
71 /* Set or display the mask used by the system when creating files. Flag
72 of -S means display the umask in a symbolic mode. */
77 int print_symbolically, opt, umask_value, pflag;
80 print_symbolically = pflag = 0;
81 reset_internal_getopt ();
82 while ((opt = internal_getopt (list, "Sp")) != -1)
102 if (DIGIT (*list->word->word))
104 umask_value = read_octal (list->word->word);
106 /* Note that other shells just let you set the umask to zero
107 by specifying a number out of range. This is a problem
108 with those shells. We don't change the umask if the input
110 if (umask_value == -1)
112 sh_erange (list->word->word, _("octal number"));
113 return (EXECUTION_FAILURE);
118 umask_value = symbolic_umask (list);
119 if (umask_value == -1)
120 return (EXECUTION_FAILURE);
122 umask_arg = (mode_t)umask_value;
124 if (print_symbolically)
125 print_symbolic_umask (umask_arg);
127 else /* Display the UMASK for this user. */
129 umask_arg = umask (022);
133 printf ("umask%s ", (print_symbolically ? " -S" : ""));
134 if (print_symbolically)
135 print_symbolic_umask (umask_arg);
137 printf ("%04lo\n", (unsigned long)umask_arg);
141 return (EXECUTION_SUCCESS);
144 /* Print the umask in a symbolic form. In the output, a letter is
145 printed if the corresponding bit is clear in the umask. */
147 print_symbolic_umask (um)
150 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
154 if ((um & S_IRUSR) == 0)
156 if ((um & S_IWUSR) == 0)
158 if ((um & S_IXUSR) == 0)
163 if ((um & S_IRGRP) == 0)
165 if ((um & S_IWGRP) == 0)
167 if ((um & S_IXGRP) == 0)
172 if ((um & S_IROTH) == 0)
174 if ((um & S_IWOTH) == 0)
176 if ((um & S_IXOTH) == 0)
180 printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits);
184 parse_symbolic_mode (mode, initial_bits)
188 int who, op, perm, bits, c;
191 for (s = mode, bits = initial_bits;;)
195 /* Parse the `who' portion of the symbolic mode clause. */
196 while (member (*s, "agou"))
210 who |= S_IRWXU | S_IRWXG | S_IRWXO;
217 /* The operation is now sitting in *s. */
226 builtin_error (_("`%c': invalid symbolic mode operator"), op);
230 /* Parse out the `perm' section of the symbolic mode clause. */
231 while (member (*s, "rwx"))
249 /* Now perform the operation or return an error for a
250 bad permission string. */
251 if (!*s || *s == ',')
266 who = S_IRWXU | S_IRWXG | S_IRWXO;
271 /* No other values are possible. */
277 s++; /* skip past ',' */
281 builtin_error (_("`%c': invalid symbolic mode character"), *s);
289 /* Set the umask from a symbolic mode string similar to that accepted
290 by chmod. If the -S argument is given, then print the umask in a
293 symbolic_umask (list)
298 /* Get the initial umask. Don't change it yet. */
302 /* All work is done with the complement of the umask -- it's
303 more intuitive and easier to deal with. It is complemented
304 again before being returned. */
305 bits = parse_symbolic_mode (list->word->word, ~um & 0777);