comparison toys/posix/chmod.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children aae54dd82927
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* chmod.c - Change file mode bits
2 *
3 * chmod.c - Change file mode bits
4 * 2 *
5 * Copyright 2012 Rob Landley <rob@landley.net> 3 * Copyright 2012 Rob Landley <rob@landley.net>
6 * 4 *
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/chmod.html 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/chmod.html
8 6
9 USE_CHMOD(NEWTOY(chmod, "<2?vR", TOYFLAG_BIN)) 7 USE_CHMOD(NEWTOY(chmod, "<2?vR", TOYFLAG_BIN))
10 8
11 config CHMOD 9 config CHMOD
12 bool "chmod" 10 bool "chmod"
13 default y 11 default y
14 help 12 help
15 usage: chmod [-R] MODE FILE... 13 usage: chmod [-R] MODE FILE...
16 14
17 Change mode of listed file[s] (recursively with -R). 15 Change mode of listed file[s] (recursively with -R).
18 16
19 MODE can be (comma-separated) stanzas: [ugoa][+-=][rwxstXugo] 17 MODE can be (comma-separated) stanzas: [ugoa][+-=][rwxstXugo]
20 18
21 Stanzas are applied in order: For each category (u = user, 19 Stanzas are applied in order: For each category (u = user,
22 g = group, o = other, a = all three, if none specified default is a), 20 g = group, o = other, a = all three, if none specified default is a),
23 set (+), clear (-), or copy (=), r = read, w = write, x = execute. 21 set (+), clear (-), or copy (=), r = read, w = write, x = execute.
24 s = u+s = suid, g+s = sgid, o+s = sticky. (+t is an alias for o+s). 22 s = u+s = suid, g+s = sgid, o+s = sticky. (+t is an alias for o+s).
25 suid/sgid: execute as the user/group who owns the file. 23 suid/sgid: execute as the user/group who owns the file.
26 sticky: can't delete files you don't own out of this directory 24 sticky: can't delete files you don't own out of this directory
27 X = x for directories or if any category already has x set. 25 X = x for directories or if any category already has x set.
28 26
29 Or MODE can be an octal value up to 7777 ug uuugggooo top + 27 Or MODE can be an octal value up to 7777 ug uuugggooo top +
30 bit 1 = o+x, bit 1<<8 = u+w, 1<<11 = g+1 sstrwxrwxrwx bottom 28 bit 1 = o+x, bit 1<<8 = u+w, 1<<11 = g+1 sstrwxrwxrwx bottom
31 29
32 Examples: 30 Examples:
33 chmod u+w file - allow owner of "file" to write to it. 31 chmod u+w file - allow owner of "file" to write to it.
34 chmod 744 file - user can read/write/execute, everyone else read only 32 chmod 744 file - user can read/write/execute, everyone else read only
35 */ 33 */
36 34
37 #define FOR_chmod 35 #define FOR_chmod
38 #include "toys.h" 36 #include "toys.h"
39 37
40 GLOBALS( 38 GLOBALS(
41 char *mode; 39 char *mode;
42 ) 40 )
43 41
44 int do_chmod(struct dirtree *try) 42 int do_chmod(struct dirtree *try)
45 { 43 {
46 mode_t mode; 44 mode_t mode;
47 45
48 if (!dirtree_notdotdot(try)) return 0; 46 if (!dirtree_notdotdot(try)) return 0;
49 47
50 mode = string_to_mode(TT.mode, try->st.st_mode); 48 mode = string_to_mode(TT.mode, try->st.st_mode);
51 if (toys.optflags & FLAG_v) { 49 if (toys.optflags & FLAG_v) {
52 char *s = dirtree_path(try, 0); 50 char *s = dirtree_path(try, 0);
53 printf("chmod '%s' to %04o\n", s, mode); 51 printf("chmod '%s' to %04o\n", s, mode);
54 free(s); 52 free(s);
55 } 53 }
56 wfchmodat(dirtree_parentfd(try), try->name, mode); 54 wfchmodat(dirtree_parentfd(try), try->name, mode);
57 55
58 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0; 56 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0;
59 } 57 }
60 58
61 void chmod_main(void) 59 void chmod_main(void)
62 { 60 {
63 TT.mode = *toys.optargs; 61 TT.mode = *toys.optargs;
64 char **file; 62 char **file;
65 63
66 for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chmod); 64 for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chmod);
67 } 65 }