The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #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 | |
| 13 | int chown_main(int argc, char **argv) |
| 14 | { |
| 15 | int i; |
| 16 | |
| 17 | if (argc < 3) { |
Jeff Sharkey | 474cea8 | 2012-03-02 15:11:53 -0800 | [diff] [blame] | 18 | fprintf(stderr, "Usage: chown <USER>[:GROUP] <FILE1> [FILE2] ...\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | 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 Sharkey | 474cea8 | 2012-03-02 15:11:53 -0800 | [diff] [blame] | 27 | if ((group = strchr(user, ':')) != NULL) { |
| 28 | *group++ = '\0'; |
| 29 | } else if ((group = strchr(user, '.')) != NULL) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | *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 Stadler | 715c889 | 2010-02-12 13:46:58 -0800 | [diff] [blame] | 40 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
| 51 | if (group != NULL) { |
| 52 | grp = getgrnam(group); |
Andrew Stadler | 715c889 | 2010-02-12 13:46:58 -0800 | [diff] [blame] | 53 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | for (i = 2; i < argc; i++) { |
| 66 | if (chown(argv[i], uid, gid) < 0) { |
Johan Norberg | 69ab6de | 2011-05-24 16:13:21 +0200 | [diff] [blame] | 67 | fprintf(stderr, "Unable to chown %s: %s\n", argv[i], strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | return 10; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |