summaryrefslogtreecommitdiffstats
path: root/toolbox/chown.c
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-02-10 15:44:07 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-02-10 15:44:07 -0800
commit13f797da7f190e9ea52f2f3d235210b8a4963b21 (patch)
treec821e95502ab1ab9096a11e7488e224e319a46b4 /toolbox/chown.c
parentdcf3ce247e13fe0a982ed2ada35cdee1f0ac626a (diff)
downloadsystem_core-13f797da7f190e9ea52f2f3d235210b8a4963b21.zip
system_core-13f797da7f190e9ea52f2f3d235210b8a4963b21.tar.gz
system_core-13f797da7f190e9ea52f2f3d235210b8a4963b21.tar.bz2
auto import from //branches/cupcake/...@130745
Diffstat (limited to 'toolbox/chown.c')
-rw-r--r--toolbox/chown.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/toolbox/chown.c b/toolbox/chown.c
new file mode 100644
index 0000000..13617db
--- /dev/null
+++ b/toolbox/chown.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <pwd.h>
+#include <grp.h>
+
+#include <unistd.h>
+#include <time.h>
+
+int chown_main(int argc, char **argv)
+{
+ int i;
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: chown <USER>[.GROUP] <FILE1> [FILE2] ...\n");
+ return 10;
+ }
+
+ // Copy argv[1] to 'user' so we can truncate it at the period
+ // if a group id specified.
+ char user[32];
+ char *group = NULL;
+ strncpy(user, argv[1], sizeof(user));
+ if ((group = strchr(user, '.')) != NULL) {
+ *group++ = '\0';
+ }
+
+ // Lookup uid (and gid if specified)
+ struct passwd *pw;
+ struct group *grp = NULL;
+ uid_t uid;
+ gid_t gid = -1; // passing -1 to chown preserves current group
+
+ pw = getpwnam(user);
+ if (pw == NULL) {
+ fprintf(stderr, "No such user '%s'\n", user);
+ return 10;
+ }
+ uid = pw->pw_uid;
+
+ if (group != NULL) {
+ grp = getgrnam(group);
+ if (grp == NULL) {
+ fprintf(stderr, "No such group '%s'\n", group);
+ return 10;
+ }
+ gid = grp->gr_gid;
+ }
+
+ for (i = 2; i < argc; i++) {
+ if (chown(argv[i], uid, gid) < 0) {
+ fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
+ return 10;
+ }
+ }
+
+ return 0;
+}
+