Mercurial > hg > toybox
annotate toys/posix/chmod.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
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 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
37 #define FOR_chmod |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
38 #include "toys.h" |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
39 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
40 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
|
41 char *mode; |
544
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 |
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
|
44 int do_chmod(struct dirtree *try) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
45 { |
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
|
46 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
|
47 |
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 if (!dirtree_notdotdot(try)) return 0; |
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 = 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
|
51 if (toys.optflags & FLAG_v) { |
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
52 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
|
53 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
|
54 free(s); |
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
55 } |
601 | 56 wfchmodat(dirtree_parentfd(try), try->name, mode); |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
57 |
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
|
58 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
59 } |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
60 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
61 void chmod_main(void) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
62 { |
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
|
63 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
|
64 char **file; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
65 |
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
|
66 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
|
67 } |