Mercurial > hg > toybox
annotate toys/posix/chgrp.c @ 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.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
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 * |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
3 * chown.c - Change ownership |
544
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 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/chown.html |
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
8 * See http://opengroup.org/onlinepubs/9699919799/utilities/chgrp.html |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
9 * |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
10 * TODO: group only one of [HLP] |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
11 |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
12 USE_CHGRP(NEWTOY(chgrp, "<2hPLHRfv", TOYFLAG_BIN)) |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
13 USE_CHGRP(OLDTOY(chown, chgrp, "<2hPLHRfv", TOYFLAG_BIN)) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
14 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
15 config CHGRP |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
16 bool "chgrp/chown" |
582
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
17 default y |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
18 help |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
19 usage: chown [-RHLP] [-fvh] [owner][:group] file... |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
20 usage: chgrp [-RHLP] [-fvh] group file... |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
21 |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
22 Change ownership of one or more files. |
544
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 -f suppress most error messages. |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
25 -h change symlinks instead of what they point to |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
26 -R recurse into subdirectories (implies -h). |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
27 -H with -R change target of symlink, follow command line symlinks |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
28 -L with -R change target of symlink, follow all symlinks |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
29 -P with -R change symlink, do not follow symlinks (default) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
30 -v verbose output. |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
31 */ |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
32 |
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
|
33 #define FOR_chgrp |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
34 #include "toys.h" |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
35 |
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
|
36 GLOBALS( |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
37 uid_t owner; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
38 gid_t group; |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
39 char *owner_name, *group_name; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
40 int symfollow; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
41 ) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
42 |
582
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
43 static int do_chgrp(struct dirtree *node) |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
44 { |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
45 int fd, 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
|
46 |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
47 // Depth first search |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
48 if (!dirtree_notdotdot(node)) return 0; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
49 if ((flags & FLAG_R) && node->data != -1 && S_ISDIR(node->st.st_mode)) |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
50 return DIRTREE_COMEAGAIN|((flags&FLAG_L) ? DIRTREE_SYMFOLLOW : 0); |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
51 |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
52 fd = dirtree_parentfd(node); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
53 ret = fchownat(fd, node->name, TT.owner, TT.group, |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
54 (flags&(FLAG_L|FLAG_H)) || !(flags&(FLAG_h|FLAG_R)) |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
55 ? 0 : AT_SYMLINK_NOFOLLOW); |
582
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
56 |
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
57 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
|
58 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
|
59 if (flags & FLAG_v) |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
60 xprintf("%s %s%s%s %s\n", toys.which->name, |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
61 TT.owner_name ? TT.owner_name : "", |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
62 toys.which->name[2]=='o' && TT.group_name ? ":" : "", |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
63 TT.group_name ? TT.group_name : "", path); |
582
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
64 if (ret == -1 && !(toys.optflags & FLAG_f)) |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
65 perror_msg("changing owner:group of '%s' to '%s:%s'", path, |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
66 TT.owner_name, 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
|
67 free(path); |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
68 } |
582
b88bc7dcdb48
Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents:
545
diff
changeset
|
69 toys.exitval |= ret; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
70 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
71 return 0; |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
72 } |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
73 |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
74 void chgrp_main(void) |
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
75 { |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
76 int ischown = toys.which->name[2] == 'o'; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
77 char **s, *own; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
78 |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
79 // Distinguish chown from chgrp |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
80 if (ischown) { |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
81 char *grp; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
82 struct passwd *p; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
83 |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
84 own = xstrdup(*toys.optargs); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
85 if ((grp = strchr(own, ':')) || (grp = strchr(own, '.'))) { |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
86 *(grp++) = 0; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
87 TT.group_name = grp; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
88 } |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
89 if (*own) { |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
90 TT.owner_name = own; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
91 p = getpwnam(own); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
92 // TODO: trailing garbage? |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
93 if (!p && isdigit(*own)) p=getpwuid(atoi(own)); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
94 if (!p) error_exit("no user '%s'", own); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
95 TT.owner = p->pw_uid; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
96 } |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
97 } else TT.group_name = *toys.optargs; |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
98 |
607
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
99 if (TT.group_name) { |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
100 struct group *g; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
101 g = getgrnam(TT.group_name); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
102 if (!g) g=getgrgid(atoi(TT.group_name)); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
103 if (!g) error_exit("no group '%s'", TT.group_name); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
104 TT.group = g->gr_gid; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
105 } |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
106 |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
107 for (s=toys.optargs+1; *s; s++) { |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
108 struct dirtree *new = dirtree_add_node(AT_FDCWD, *s, |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
109 toys.optflags&(FLAG_H|FLAG_L)); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
110 if (new) handle_callback(new, do_chgrp); |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
111 else toys.exitval = 1; |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
112 } |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
113 |
8bee9c27c219
Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents:
589
diff
changeset
|
114 if (CFG_TOYBOX_FREE) free(own); |
544
f11693d78764
New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff
changeset
|
115 } |