use bb_xbind/bb_xlisten
[oweals/busybox.git] / coreutils / chown.c
index a611f92f1863961a61ec017311465954e59c3ed5..bb379ac205949898ecbc71acb81dff87e50750fa 100644 (file)
@@ -1,63 +1,78 @@
-#include "internal.h"
-#include <pwd.h>
-#include <grp.h>
-#include <string.h>
-#include <stdio.h>
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini chown implementation for busybox
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
 
-const char     chown_usage[] = "chown [-R] user-name file [file ...]\n"
-"\n\tThe group list is kept in the file /etc/groups.\n\n"
-"\t-R:\tRecursively change the mode of all files and directories\n"
-"\t\tunder the argument directory.";
+/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
+/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
+/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
 
-int
-parse_user_name(const char * s, struct FileInfo * i)
-{
-       struct  passwd *        p;
-       char *                          dot = strchr(s, '.');
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include "busybox.h"
 
-       if (! dot )
-               dot = strchr(s, ':');
+static uid_t uid = -1;
+static gid_t gid = -1;
 
-       if ( dot )
-               *dot = '\0';
+static int (*chown_func)(const char *, uid_t, gid_t) = chown;
 
-       if ( (p = getpwnam(s)) == 0 ) {
-               fprintf(stderr, "%s: no such user.\n", s);
-               return 1;
-       }
-       i->userID = p->pw_uid;
-
-       if ( dot ) {
-               struct group *  g = getgrnam(++dot);
-               if ( g == 0 ) {
-                       fprintf(stderr, "%s: no such group.\n", dot);
-                       return 1;
-               }
-               i->groupID = g->gr_gid;
-               i->changeGroupID = 1;
+static int fileAction(const char *fileName, struct stat *statbuf,
+               void ATTRIBUTE_UNUSED *junk)
+{
+       if (!chown_func(fileName,
+                               (uid == (uid_t)-1) ? statbuf->st_uid : uid,
+                               (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
+               return TRUE;
        }
-       return 0;
+       bb_perror_msg("%s", fileName);  /* A filename could have % in it... */
+       return FALSE;
 }
 
-extern int
-chown_main(struct FileInfo * i, int argc, char * * argv)
+#define FLAG_R 1
+#define FLAG_h 2
+
+int chown_main(int argc, char **argv)
 {
-       int                                     status;
+       int flags;
+       int retval = EXIT_SUCCESS;
+       char *groupName;
+
+       flags = bb_getopt_ulflags(argc, argv, "Rh");
 
-       while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
-               i->recursive = 1;
-               argc--;
-               argv++;
+       if (flags & FLAG_h) chown_func = lchown;
+
+       if (argc - optind < 2) {
+               bb_show_usage();
        }
 
-       if ( (status = parse_user_name(argv[1], i)) != 0 )
-               return status;
+       argv += optind;
 
-       argv++;
-       argc--;
+       /* First, check if there is a group name here */
+       if ((groupName = strchr(*argv, '.')) == NULL) {
+               groupName = strchr(*argv, ':');
+       }
 
-       i->changeUserID = 1;
-       i->complainInPostProcess = 1;
+       /* Check for the username and groupname */
+       if (groupName) {
+               *groupName++ = '\0';
+               gid = get_ug_id(groupName, bb_xgetgrnam);
+       }
+       if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
+       ++argv;
+
+       /* Ok, ready to do the deed now */
+       do {
+               if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
+                                                               fileAction, fileAction, NULL)) {
+                       retval = EXIT_FAILURE;
+               }
+       } while (*++argv);
 
-       return monadic_main(i, argc, argv);
+       return retval;
 }