annotate toys/chgrp.c @ 588:9c2277b92b86

Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
author Rob Landley <rob@landley.net>
date Fri, 01 Jun 2012 20:04:39 -0500
parents b88bc7dcdb48
children 7013fd450ff4
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"
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
18 default y
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
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
41 static int do_chgrp(struct dirtree *node)
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
42 {
588
9c2277b92b86 Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
Rob Landley <rob@landley.net>
parents: 582
diff changeset
43 int ret, flags = toys.optflags;
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
44
588
9c2277b92b86 Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
Rob Landley <rob@landley.net>
parents: 582
diff changeset
45 ret = dirtree_comeagain(node, flags & FLAG_R);
9c2277b92b86 Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
Rob Landley <rob@landley.net>
parents: 582
diff changeset
46 if (!ret || ret == DIRTREE_COMEAGAIN) return ret;
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
47
588
9c2277b92b86 Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
Rob Landley <rob@landley.net>
parents: 582
diff changeset
48 if (node->extra != -1) ret = fchown(node->extra, -1, TT.group);
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
49
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
50 if (ret || (flags & FLAG_v)) {
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
51 char *path = dirtree_path(node, 0);
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
52 if (flags & FLAG_v)
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
53 xprintf("chgrp(%s, %s)\n", TT.group_name, path);
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
54 if (ret == -1 && !(toys.optflags & FLAG_f))
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
55 perror_msg("changing group of '%s' to '%s'", path, TT.group_name);
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
56 free(path);
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
57 }
588
9c2277b92b86 Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
Rob Landley <rob@landley.net>
parents: 582
diff changeset
58 close(node->extra);
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
59 toys.exitval |= ret;
544
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 return 0;
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
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
64 void chgrp_main(void)
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 char **s;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
67 struct group *group;
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 TT.group_name = *toys.optargs;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
70 group = getgrnam(TT.group_name);
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
71 if (!group) error_exit("no group '%s'", TT.group_name);
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
72 TT.group = group->gr_gid;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
73
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 545
diff changeset
74 for (s=toys.optargs+1; *s; s++) dirtree_read(*s, do_chgrp);
544
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
75 }