blob: 92efee61522fce8507fc4d38d786b72f2f1f2266 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <dirent.h>
6#include <errno.h>
7#include <pwd.h>
8#include <grp.h>
9
10#include <unistd.h>
11#include <time.h>
12
13int chown_main(int argc, char **argv)
14{
15 int i;
16
17 if (argc < 3) {
Jeff Sharkey474cea82012-03-02 15:11:53 -080018 fprintf(stderr, "Usage: chown <USER>[:GROUP] <FILE1> [FILE2] ...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019 return 10;
20 }
21
22 // Copy argv[1] to 'user' so we can truncate it at the period
23 // if a group id specified.
24 char user[32];
25 char *group = NULL;
26 strncpy(user, argv[1], sizeof(user));
Jeff Sharkey474cea82012-03-02 15:11:53 -080027 if ((group = strchr(user, ':')) != NULL) {
28 *group++ = '\0';
29 } else if ((group = strchr(user, '.')) != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030 *group++ = '\0';
31 }
32
33 // Lookup uid (and gid if specified)
34 struct passwd *pw;
35 struct group *grp = NULL;
36 uid_t uid;
37 gid_t gid = -1; // passing -1 to chown preserves current group
38
39 pw = getpwnam(user);
Andrew Stadler715c8892010-02-12 13:46:58 -080040 if (pw != NULL) {
41 uid = pw->pw_uid;
42 } else {
43 char* endptr;
44 uid = (int) strtoul(user, &endptr, 0);
45 if (endptr == user) { // no conversion
46 fprintf(stderr, "No such user '%s'\n", user);
47 return 10;
48 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51 if (group != NULL) {
52 grp = getgrnam(group);
Andrew Stadler715c8892010-02-12 13:46:58 -080053 if (grp != NULL) {
54 gid = grp->gr_gid;
55 } else {
56 char* endptr;
57 gid = (int) strtoul(group, &endptr, 0);
58 if (endptr == group) { // no conversion
59 fprintf(stderr, "No such group '%s'\n", group);
60 return 10;
61 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 }
64
65 for (i = 2; i < argc; i++) {
66 if (chown(argv[i], uid, gid) < 0) {
Johan Norberg69ab6de2011-05-24 16:13:21 +020067 fprintf(stderr, "Unable to chown %s: %s\n", argv[i], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 return 10;
69 }
70 }
71
72 return 0;
73}