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