annotate toys/chown.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
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 * chown.c - Change 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/chown.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_CHOWN(NEWTOY(chown, "<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 CHOWN
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
17 bool "chown"
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: chown [-R] [-f] [-v] group file...
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
21 Change 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 uid_t owner;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
36 gid_t group;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
37 char *owner_name;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
38 char *group_name;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
39 )
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 #define TT this.chown
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
42
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
43 static int do_chown(const char *path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
44 int ret = chown(path, TT.owner, TT.group);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
45 if (toys.optflags & FLAG_v)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
46 xprintf("chown(%s:%s, %s)\n", TT.owner_name, TT.group_name, path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
47 if (ret == -1 && !(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
48 perror_msg("changing owner of '%s' to '%s:%s'", path,
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
49 TT.owner_name, TT.group_name);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
50 toys.exitval |= ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
51 return ret;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
52 }
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 // Copied from toys/cp.c:cp_node()
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
55 int chown_node(char *path, struct dirtree *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 char *s = path + strlen(path);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
58 struct dirtree *n = node;
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 for ( ; ; n = n->parent) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
61 while (s!=path) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
62 if (*(--s) == '/') break;
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 if (!n) break;
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 if (s != path) s++;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
67
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
68 do_chown(s);
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 return 0;
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
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
73 void chown_main(void)
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 char **s;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
76 char *owner = NULL, *group;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
77 char *param1 = *toys.optargs;
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 TT.owner = -1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
80 TT.group = -1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
81 TT.owner_name = "";
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
82 TT.group_name = "";
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 group = strchr(param1, ':');
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
85 if (!group)
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
86 group = strchr(param1, '.');
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
87
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
88 if (group) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
89 group++;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
90 struct group *g = getgrnam(group);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
91 if (!g) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
92 error_msg("invalid group '%s'", group);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
93 toys.exitval = 1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
94 return;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
95 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
96 TT.group = g->gr_gid;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
97 TT.group_name = group;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
98 owner = param1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
99 owner[group - owner - 1] = '\0';
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 owner = param1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
102 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
103
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
104 if (owner && owner[0]) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
105 struct passwd *p = getpwnam(owner);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
106 if (!p) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
107 error_msg("invalid owner '%s'", owner);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
108 toys.exitval = 1;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
109 return;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
110 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
111 TT.owner = p->pw_uid;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
112 TT.owner_name = owner;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
113 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
114
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
115 if (toys.optflags & FLAG_R) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
116 // Recurse into subdirectories
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
117 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
118 struct stat sb;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
119 if (stat(*s, &sb) == -1) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
120 if (!(toys.optflags & FLAG_f))
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
121 perror_msg("%s", *s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
122 continue;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
123 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
124 do_chown(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
125 if (S_ISDIR(sb.st_mode)) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
126 strncpy(toybuf, *s, sizeof(toybuf) - 1);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
127 toybuf[sizeof(toybuf) - 1] = 0;
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
128 dirtree_read(toybuf, NULL, chown_node);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
129 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
130 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
131 } else {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
132 // Do not recurse
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
133 for (s=toys.optargs + 1; *s; s++) {
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
134 do_chown(*s);
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
135 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
136 }
f11693d78764 New toys - chmod, chown, and chgrp.
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
137 }