Mercurial > hg > toybox
comparison toys/posix/chmod.c @ 653:2986aa63a021
Move commands into "posix", "lsb", and "other" menus/directories.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 14:25:22 -0500 |
parents | toys/chmod.c@a6a541b7fc34 |
children | 6df4ccc0acbe |
comparison
equal
deleted
inserted
replaced
652:2d7c56913fda | 653:2986aa63a021 |
---|---|
1 /* vi: set sw=4 ts=4: | |
2 * | |
3 * chmod.c - Change file mode bits | |
4 * | |
5 * Copyright 2012 Rob Landley <rob@landley.net> | |
6 * | |
7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/chmod.html | |
8 * | |
9 | |
10 USE_CHMOD(NEWTOY(chmod, "<2?vR", TOYFLAG_BIN)) | |
11 | |
12 config CHMOD | |
13 bool "chmod" | |
14 default y | |
15 help | |
16 usage: chmod [-R] MODE FILE... | |
17 | |
18 Change mode of listed file[s] (recursively with -R). | |
19 | |
20 MODE can be (comma-separated) stanzas: [ugoa][+-=][rwxstXugo] | |
21 | |
22 Stanzas are applied in order: For each category (u = user, | |
23 g = group, o = other, a = all three, if none specified default is a), | |
24 set (+), clear (-), or copy (=), r = read, w = write, x = execute. | |
25 s = u+s = suid, g+s = sgid, o+s = sticky. (+t is an alias for o+s). | |
26 suid/sgid: execute as the user/group who owns the file. | |
27 sticky: can't delete files you don't own out of this directory | |
28 X = x for directories or if any category already has x set. | |
29 | |
30 Or MODE can be an octal value up to 7777 ug uuugggooo top + | |
31 bit 1 = o+x, bit 1<<8 = u+w, 1<<11 = g+1 sstrwxrwxrwx bottom | |
32 | |
33 Examples: | |
34 chmod u+w file - allow owner of "file" to write to it. | |
35 chmod 744 file - user can read/write/execute, everyone else read only | |
36 */ | |
37 | |
38 #include "toys.h" | |
39 | |
40 DEFINE_GLOBALS( | |
41 char *mode; | |
42 ) | |
43 | |
44 #define TT this.chmod | |
45 | |
46 #define FLAG_R 1 | |
47 #define FLAG_v 2 | |
48 | |
49 int do_chmod(struct dirtree *try) | |
50 { | |
51 mode_t mode; | |
52 | |
53 if (!dirtree_notdotdot(try)) return 0; | |
54 | |
55 mode = string_to_mode(TT.mode, try->st.st_mode); | |
56 if (toys.optflags & FLAG_v) { | |
57 char *s = dirtree_path(try, 0); | |
58 printf("chmod '%s' to %04o\n", s, mode); | |
59 free(s); | |
60 } | |
61 wfchmodat(dirtree_parentfd(try), try->name, mode); | |
62 | |
63 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0; | |
64 } | |
65 | |
66 void chmod_main(void) | |
67 { | |
68 TT.mode = *toys.optargs; | |
69 char **file; | |
70 | |
71 for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chmod); | |
72 } |