Dan Pasanen | c6e3786 | 2014-10-02 14:08:59 -0500 | [diff] [blame] | 1 | /* umask.c, created from umask.def. */ |
| 2 | #line 22 "./umask.def" |
| 3 | |
| 4 | #line 41 "./umask.def" |
| 5 | |
| 6 | #include <config.h> |
| 7 | |
| 8 | #include "../bashtypes.h" |
| 9 | #include "filecntl.h" |
| 10 | #if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) |
| 11 | # include <sys/file.h> |
| 12 | #endif |
| 13 | |
| 14 | #if defined (HAVE_UNISTD_H) |
| 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <chartypes.h> |
| 20 | |
| 21 | #include "../bashintl.h" |
| 22 | |
| 23 | #include "../shell.h" |
| 24 | #include "posixstat.h" |
| 25 | #include "common.h" |
| 26 | #include "bashgetopt.h" |
| 27 | |
| 28 | /* **************************************************************** */ |
| 29 | /* */ |
| 30 | /* UMASK Builtin and Helpers */ |
| 31 | /* */ |
| 32 | /* **************************************************************** */ |
| 33 | |
| 34 | static void print_symbolic_umask __P((mode_t)); |
| 35 | static int symbolic_umask __P((WORD_LIST *)); |
| 36 | |
| 37 | /* Set or display the mask used by the system when creating files. Flag |
| 38 | of -S means display the umask in a symbolic mode. */ |
| 39 | int |
| 40 | umask_builtin (list) |
| 41 | WORD_LIST *list; |
| 42 | { |
| 43 | int print_symbolically, opt, umask_value, pflag; |
| 44 | mode_t umask_arg; |
| 45 | |
| 46 | print_symbolically = pflag = 0; |
| 47 | reset_internal_getopt (); |
| 48 | while ((opt = internal_getopt (list, "Sp")) != -1) |
| 49 | { |
| 50 | switch (opt) |
| 51 | { |
| 52 | case 'S': |
| 53 | print_symbolically++; |
| 54 | break; |
| 55 | case 'p': |
| 56 | pflag++; |
| 57 | break; |
| 58 | default: |
| 59 | builtin_usage (); |
| 60 | return (EX_USAGE); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | list = loptend; |
| 65 | |
| 66 | if (list) |
| 67 | { |
| 68 | if (DIGIT (*list->word->word)) |
| 69 | { |
| 70 | umask_value = read_octal (list->word->word); |
| 71 | |
| 72 | /* Note that other shells just let you set the umask to zero |
| 73 | by specifying a number out of range. This is a problem |
| 74 | with those shells. We don't change the umask if the input |
| 75 | is lousy. */ |
| 76 | if (umask_value == -1) |
| 77 | { |
| 78 | sh_erange (list->word->word, _("octal number")); |
| 79 | return (EXECUTION_FAILURE); |
| 80 | } |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | umask_value = symbolic_umask (list); |
| 85 | if (umask_value == -1) |
| 86 | return (EXECUTION_FAILURE); |
| 87 | } |
| 88 | umask_arg = (mode_t)umask_value; |
| 89 | umask (umask_arg); |
| 90 | if (print_symbolically) |
| 91 | print_symbolic_umask (umask_arg); |
| 92 | } |
| 93 | else /* Display the UMASK for this user. */ |
| 94 | { |
| 95 | umask_arg = umask (022); |
| 96 | umask (umask_arg); |
| 97 | |
| 98 | if (pflag) |
| 99 | printf ("umask%s ", (print_symbolically ? " -S" : "")); |
| 100 | if (print_symbolically) |
| 101 | print_symbolic_umask (umask_arg); |
| 102 | else |
| 103 | printf ("%04lo\n", (unsigned long)umask_arg); |
| 104 | } |
| 105 | |
| 106 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
| 107 | } |
| 108 | |
| 109 | /* Print the umask in a symbolic form. In the output, a letter is |
| 110 | printed if the corresponding bit is clear in the umask. */ |
| 111 | static void |
| 112 | print_symbolic_umask (um) |
| 113 | mode_t um; |
| 114 | { |
| 115 | char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */ |
| 116 | int i; |
| 117 | |
| 118 | i = 0; |
| 119 | if ((um & S_IRUSR) == 0) |
| 120 | ubits[i++] = 'r'; |
| 121 | if ((um & S_IWUSR) == 0) |
| 122 | ubits[i++] = 'w'; |
| 123 | if ((um & S_IXUSR) == 0) |
| 124 | ubits[i++] = 'x'; |
| 125 | ubits[i] = '\0'; |
| 126 | |
| 127 | i = 0; |
| 128 | if ((um & S_IRGRP) == 0) |
| 129 | gbits[i++] = 'r'; |
| 130 | if ((um & S_IWGRP) == 0) |
| 131 | gbits[i++] = 'w'; |
| 132 | if ((um & S_IXGRP) == 0) |
| 133 | gbits[i++] = 'x'; |
| 134 | gbits[i] = '\0'; |
| 135 | |
| 136 | i = 0; |
| 137 | if ((um & S_IROTH) == 0) |
| 138 | obits[i++] = 'r'; |
| 139 | if ((um & S_IWOTH) == 0) |
| 140 | obits[i++] = 'w'; |
| 141 | if ((um & S_IXOTH) == 0) |
| 142 | obits[i++] = 'x'; |
| 143 | obits[i] = '\0'; |
| 144 | |
| 145 | printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits); |
| 146 | } |
| 147 | |
| 148 | int |
| 149 | parse_symbolic_mode (mode, initial_bits) |
| 150 | char *mode; |
| 151 | int initial_bits; |
| 152 | { |
| 153 | int who, op, perm, bits, c; |
| 154 | char *s; |
| 155 | |
| 156 | for (s = mode, bits = initial_bits;;) |
| 157 | { |
| 158 | who = op = perm = 0; |
| 159 | |
| 160 | /* Parse the `who' portion of the symbolic mode clause. */ |
| 161 | while (member (*s, "agou")) |
| 162 | { |
| 163 | switch (c = *s++) |
| 164 | { |
| 165 | case 'u': |
| 166 | who |= S_IRWXU; |
| 167 | continue; |
| 168 | case 'g': |
| 169 | who |= S_IRWXG; |
| 170 | continue; |
| 171 | case 'o': |
| 172 | who |= S_IRWXO; |
| 173 | continue; |
| 174 | case 'a': |
| 175 | who |= S_IRWXU | S_IRWXG | S_IRWXO; |
| 176 | continue; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* The operation is now sitting in *s. */ |
| 183 | op = *s++; |
| 184 | switch (op) |
| 185 | { |
| 186 | case '+': |
| 187 | case '-': |
| 188 | case '=': |
| 189 | break; |
| 190 | default: |
| 191 | builtin_error (_("`%c': invalid symbolic mode operator"), op); |
| 192 | return (-1); |
| 193 | } |
| 194 | |
| 195 | /* Parse out the `perm' section of the symbolic mode clause. */ |
| 196 | while (member (*s, "rwx")) |
| 197 | { |
| 198 | c = *s++; |
| 199 | |
| 200 | switch (c) |
| 201 | { |
| 202 | case 'r': |
| 203 | perm |= S_IRUGO; |
| 204 | break; |
| 205 | case 'w': |
| 206 | perm |= S_IWUGO; |
| 207 | break; |
| 208 | case 'x': |
| 209 | perm |= S_IXUGO; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* Now perform the operation or return an error for a |
| 215 | bad permission string. */ |
| 216 | if (!*s || *s == ',') |
| 217 | { |
| 218 | if (who) |
| 219 | perm &= who; |
| 220 | |
| 221 | switch (op) |
| 222 | { |
| 223 | case '+': |
| 224 | bits |= perm; |
| 225 | break; |
| 226 | case '-': |
| 227 | bits &= ~perm; |
| 228 | break; |
| 229 | case '=': |
| 230 | if (who == 0) |
| 231 | who = S_IRWXU | S_IRWXG | S_IRWXO; |
| 232 | bits &= ~who; |
| 233 | bits |= perm; |
| 234 | break; |
| 235 | |
| 236 | /* No other values are possible. */ |
| 237 | } |
| 238 | |
| 239 | if (*s == '\0') |
| 240 | break; |
| 241 | else |
| 242 | s++; /* skip past ',' */ |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | builtin_error (_("`%c': invalid symbolic mode character"), *s); |
| 247 | return (-1); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return (bits); |
| 252 | } |
| 253 | |
| 254 | /* Set the umask from a symbolic mode string similar to that accepted |
| 255 | by chmod. If the -S argument is given, then print the umask in a |
| 256 | symbolic form. */ |
| 257 | static int |
| 258 | symbolic_umask (list) |
| 259 | WORD_LIST *list; |
| 260 | { |
| 261 | int um, bits; |
| 262 | |
| 263 | /* Get the initial umask. Don't change it yet. */ |
| 264 | um = umask (022); |
| 265 | umask (um); |
| 266 | |
| 267 | /* All work is done with the complement of the umask -- it's |
| 268 | more intuitive and easier to deal with. It is complemented |
| 269 | again before being returned. */ |
| 270 | bits = parse_symbolic_mode (list->word->word, ~um & 0777); |
| 271 | if (bits == -1) |
| 272 | return (-1); |
| 273 | |
| 274 | um = ~bits & 0777; |
| 275 | return (um); |
| 276 | } |