1 This file is umask.def, from which is created umask.c.
2 It implements the builtin "umask" in Bash.
4 Copyright (C) 1987, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION umask_builtin
26 $SHORT_DOC umask [-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 MODE begins with a digit, it is interpreted as an octal number,
31 otherwise it is a symbolic mode string like that accepted by chmod(1).
36 #include "../bashtypes.h"
37 #include "../filecntl.h"
40 #if defined (HAVE_UNISTD_H)
47 #include "../posixstat.h"
49 #include "bashgetopt.h"
51 /* **************************************************************** */
53 /* UMASK Builtin and Helpers */
55 /* **************************************************************** */
57 static void print_symbolic_umask ();
58 static int symbolic_umask ();
60 /* Set or display the mask used by the system when creating files. Flag
61 of -S means display the umask in a symbolic mode. */
66 int print_symbolically, opt, umask_value;
68 print_symbolically = 0;
69 reset_internal_getopt ();
70 while ((opt = internal_getopt (list, "S")) != -1)
87 if (digit (*list->word->word))
89 umask_value = read_octal (list->word->word);
91 /* Note that other shells just let you set the umask to zero
92 by specifying a number out of range. This is a problem
93 with those shells. We don't change the umask if the input
95 if (umask_value == -1)
97 builtin_error ("`%s' is not an octal number from 000 to 777",
99 return (EXECUTION_FAILURE);
104 umask_value = symbolic_umask (list);
105 if (umask_value == -1)
106 return (EXECUTION_FAILURE);
109 if (print_symbolically)
110 print_symbolic_umask (umask_value);
112 else /* Display the UMASK for this user. */
114 umask_value = umask (022);
117 if (print_symbolically)
118 print_symbolic_umask (umask_value);
120 printf ("%03o\n", umask_value);
124 return (EXECUTION_SUCCESS);
127 /* Print the umask in a symbolic form. In the output, a letter is
128 printed if the corresponding bit is clear in the umask. */
130 print_symbolic_umask (um)
133 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
137 if ((um & S_IRUSR) == 0)
139 if ((um & S_IWUSR) == 0)
141 if ((um & S_IXUSR) == 0)
146 if ((um & S_IRGRP) == 0)
148 if ((um & S_IWGRP) == 0)
150 if ((um & S_IXGRP) == 0)
155 if ((um & S_IROTH) == 0)
157 if ((um & S_IWOTH) == 0)
159 if ((um & S_IXOTH) == 0)
163 printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits);
166 /* Set the umask from a symbolic mode string similar to that accepted
167 by chmod. If the -S argument is given, then print the umask in a
170 symbolic_umask (list)
174 int who, op, perm, mask;
177 /* Get the initial umask. Don't change it yet. */
181 /* All work below is done with the complement of the umask -- it's
182 more intuitive and easier to deal with. It is complemented
183 again before being returned. */
186 s = list->word->word;
190 who = op = perm = mask = 0;
192 /* Parse the `who' portion of the symbolic mode clause. */
193 while (member (*s, "agou"))
207 who |= S_IRWXU | S_IRWXG | S_IRWXO;
214 /* The operation is now sitting in *s. */
223 builtin_error ("bad symbolic mode operator: %c", op);
227 /* Parse out the `perm' section of the symbolic mode clause. */
228 while (member (*s, "rwx"))
248 /* Now perform the operation or return an error for a
249 bad permission string. */
250 if (!*s || *s == ',')
271 builtin_error ("bad symbolic mode operator: %c", op);
281 s++; /* skip past ',' */
285 builtin_error ("bad character in symbolic mode: %c", *s);