annotate toys/chmod.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 4a91ede70548
children 7becb497c3c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 *
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
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
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
10 USE_CHMOD(NEWTOY(chmod, "<2Rfv", TOYFLAG_BIN))
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
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
13 bool "chmod"
545
4a91ede70548 Default new ch* commands to n until enough is implemented not to break aboriginal build.
Rob Landley <rob@landley.net>
parents: 544
diff changeset
14 default n
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
15 help
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
16 usage: chmod [-R] [-f] [-v] mode file...
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
17 Change mode bits of one or more files.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
18
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
19 -R recurse into subdirectories.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
20 -f suppress most error messages.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
21 -v verbose output.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
22 */
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
23
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
24 #include "toys.h"
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
25
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
26 #define FLAG_R 4
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
27 #define FLAG_f 2
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
28 #define FLAG_v 1
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
29
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
30 DEFINE_GLOBALS(
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
31 long mode;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
32 )
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 #define TT this.chmod
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 static int do_chmod(const char *path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
37 int ret = chmod(path, TT.mode);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
38 if (toys.optflags & FLAG_v)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
39 xprintf("chmod(%04o, %s)\n", TT.mode, path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
40 if (ret == -1 && !(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
41 perror_msg("changing perms of '%s' to %04o", path, TT.mode);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
42 toys.exitval |= ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
43 return ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
44 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
45
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
46 // Copied from toys/cp.c:cp_node()
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
47 int chmod_node(char *path, struct dirtree *node)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
48 {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
49 char *s = path + strlen(path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
50 struct dirtree *n = node;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
51
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
52 for ( ; ; n = n->parent) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
53 while (s!=path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
54 if (*(--s) == '/') break;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
55 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
56 if (!n) break;
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 if (s != path) s++;
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 do_chmod(s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
61
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
62 return 0;
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 {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
67 char **s;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
68 TT.mode = strtoul(*toys.optargs, NULL, 8);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
69
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
70 if (toys.optflags & FLAG_R) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
71 // Recurse into subdirectories
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
72 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
73 struct stat sb;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
74 if (stat(*s, &sb) == -1) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
75 if (!(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
76 perror_msg("%s", *s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
77 continue;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
78 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
79 do_chmod(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
80 if (S_ISDIR(sb.st_mode)) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
81 strncpy(toybuf, *s, sizeof(toybuf) - 1);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
82 toybuf[sizeof(toybuf) - 1] = 0;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
83 dirtree_read(toybuf, NULL, chmod_node);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
84 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
85 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
86 } else {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
87 // Do not recurse
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
88 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
89 do_chmod(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
90 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
91 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
92 }