Mercurial > hg > toybox
annotate toys/posix/chmod.c @ 1776:7bf68329eb3b draft default tip
Repository switched to git at https://github.com/landley/toybox
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 09 Apr 2015 02:28:32 -0500 |
parents | aae54dd82927 |
children |
rev | line source |
---|---|
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* chmod.c - Change file mode bits |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
2 * |
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
|
3 * Copyright 2012 Rob Landley <rob@landley.net> |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
4 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
5 * 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
|
6 |
865
aae54dd82927
Add -f to chmod (just turns off -v).
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
7 USE_CHMOD(NEWTOY(chmod, "<2?vRf[-vf]", TOYFLAG_BIN)) |
544
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 config CHMOD |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
10 bool "chmod" |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
11 default y |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
12 help |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
13 usage: chmod [-R] MODE FILE... |
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
|
14 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
15 Change mode of listed file[s] (recursively with -R). |
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
|
16 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
17 MODE can be (comma-separated) stanzas: [ugoa][+-=][rwxstXugo] |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
18 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
19 Stanzas are applied in order: For each category (u = user, |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
20 g = group, o = other, a = all three, if none specified default is a), |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
21 set (+), clear (-), or copy (=), r = read, w = write, x = execute. |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
22 s = u+s = suid, g+s = sgid, o+s = sticky. (+t is an alias for o+s). |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
23 suid/sgid: execute as the user/group who owns the file. |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
24 sticky: can't delete files you don't own out of this directory |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
25 X = x for directories or if any category already has x set. |
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
|
26 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
27 Or MODE can be an octal value up to 7777 ug uuugggooo top + |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
28 bit 1 = o+x, bit 1<<8 = u+w, 1<<11 = g+1 sstrwxrwxrwx bottom |
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
|
29 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
30 Examples: |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
31 chmod u+w file - allow owner of "file" to write to it. |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
32 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
|
33 */ |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
34 |
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
|
35 #define FOR_chmod |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
36 #include "toys.h" |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
37 |
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
|
38 GLOBALS( |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
39 char *mode; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
40 ) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
41 |
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
|
42 int do_chmod(struct dirtree *try) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
43 { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
44 mode_t mode; |
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 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
46 if (!dirtree_notdotdot(try)) return 0; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
47 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
48 mode = string_to_mode(TT.mode, try->st.st_mode); |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
49 if (toys.optflags & FLAG_v) { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
50 char *s = dirtree_path(try, 0); |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
51 printf("chmod '%s' to %04o\n", s, mode); |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
52 free(s); |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
53 } |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
54 wfchmodat(dirtree_parentfd(try), try->name, mode); |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
55 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
56 return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
57 } |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
58 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
59 void chmod_main(void) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
60 { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
61 TT.mode = *toys.optargs; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
62 char **file; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
63 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
64 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
|
65 } |