annotate toys/chgrp.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 b88bc7dcdb48
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 * chgrp.c - Change group ownership
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/chgrp.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 * TODO: Add support for -h
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
10 * TODO: Add support for -H
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
11 * TODO: Add support for -L
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
12 * TODO: Add support for -P
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
13
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
14 USE_CHGRP(NEWTOY(chgrp, "<2Rfv", TOYFLAG_BIN))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
15
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
16 config CHGRP
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
17 bool "chgrp"
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
18 default n
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
19 help
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
20 usage: chgrp [-R] [-f] [-v] group file...
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
21 Change group ownership of one or more files.
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 -R recurse into subdirectories.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
24 -f suppress most error messages.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
25 -v verbose output.
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
26 */
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
27
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
28 #include "toys.h"
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 FLAG_R 4
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
31 #define FLAG_f 2
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
32 #define FLAG_v 1
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_GLOBALS(
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
35 gid_t group;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
36 char *group_name;
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
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
39 #define TT this.chgrp
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 static int do_chgrp(const char *path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
42 int ret = chown(path, -1, TT.group);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
43 if (toys.optflags & FLAG_v)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
44 xprintf("chgrp(%s, %s)\n", TT.group_name, path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
45 if (ret == -1 && !(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
46 perror_msg("changing group of '%s' to '%s'", path, TT.group_name);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
47 toys.exitval |= ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
48 return ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
49 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
50
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
51 // Copied from toys/cp.c:cp_node()
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
52 int chgrp_node(char *path, struct dirtree *node)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
53 {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
54 char *s = path + strlen(path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
55 struct dirtree *n = node;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
56
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
57 for ( ; ; n = n->parent) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
58 while (s!=path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
59 if (*(--s) == '/') break;
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 if (!n) break;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
62 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
63 if (s != path) s++;
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 do_chgrp(s);
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 return 0;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
68 }
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 void chgrp_main(void)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
71 {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
72 char **s;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
73 struct group *group;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
74
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
75 TT.group_name = *toys.optargs;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
76 group = getgrnam(TT.group_name);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
77 if (!group) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
78 error_msg("invalid group '%s'", TT.group_name);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
79 toys.exitval = 1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
80 return;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
81 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
82 TT.group = group->gr_gid;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
83
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
84 if (toys.optflags & FLAG_R) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
85 // Recurse into subdirectories
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
86 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
87 struct stat sb;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
88 if (stat(*s, &sb) == -1) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
89 if (!(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
90 perror_msg("stat '%s'", *s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
91 continue;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
92 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
93 do_chgrp(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
94 if (S_ISDIR(sb.st_mode)) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
95 strncpy(toybuf, *s, sizeof(toybuf) - 1);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
96 toybuf[sizeof(toybuf) - 1] = 0;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
97 dirtree_read(toybuf, NULL, chgrp_node);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
98 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
99 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
100 } else {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
101 // Do not recurse
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
102 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
103 do_chgrp(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
104 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
105 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
106 }