Mercurial > hg > toybox
annotate toys/chmod.c @ 594:051dffe00b99
The linux from scratch build wants -v on chmod.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 10 Jun 2012 19:44:33 -0500 |
parents | 7becb497c3c4 |
children | a6a541b7fc34 |
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 * |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/chmod.html |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
8 * |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
9 |
594
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
10 USE_CHMOD(NEWTOY(chmod, "<2?vR", TOYFLAG_BIN)) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
11 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 |
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 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
|
19 |
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
20 MODE can be (comma-separated) stanzas: [ugoa][+-=][rwxstXugo] |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
21 |
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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 |
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 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
|
31 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
|
32 |
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 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
|
34 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
|
35 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
|
36 */ |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
37 |
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 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
40 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
|
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 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
44 #define TT this.chmod |
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 #define FLAG_R 1 |
594
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
47 #define FLAG_v 2 |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
48 |
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
|
49 int do_chmod(struct dirtree *try) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
50 { |
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
|
51 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
|
52 |
7becb497c3c4
Update chmod to work with new dirtree, and fix bugs in string_to_mode().
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
53 if (!dirtree_notdotdot(try)) return 0; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
54 |
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
|
55 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
|
56 if (toys.optflags & FLAG_v) { |
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
57 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
|
58 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
|
59 free(s); |
051dffe00b99
The linux from scratch build wants -v on chmod.
Rob Landley <rob@landley.net>
parents:
590
diff
changeset
|
60 } |
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
|
61 wfchmodat(try->parent ? try->parent->data : AT_FDCWD, try->name, mode); |
544
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 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0; |
544
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 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
66 void chmod_main(void) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
67 { |
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
|
68 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
|
69 char **file; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
70 |
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
|
71 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
|
72 } |