These messages can be .rodata, so make them even more const.
[oweals/busybox.git] / chown.c
diff --git a/chown.c b/chown.c
index fc0c2424fd0dc369a141a4ea0870c4987eb49c8c..0114033789078b7779ad4e299c5b1aaf061d582f 100644 (file)
--- a/chown.c
+++ b/chown.c
@@ -1,7 +1,10 @@
+/* vi: set sw=4 ts=4: */
 /*
- * Mini chown/chgrp implementation for busybox
+ * Mini chown/chmod/chgrp implementation for busybox
  *
- * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
+ *
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
+ * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 #include <stdio.h>
-#include <grp.h>
-#include <pwd.h>
-#include "internal.h"
-
-
-static int uid=-1;
-static int gid=0;
-static int chownApp;
-static char* invocationName=NULL;
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "busybox.h"
 
+/* Don't use lchown for libc5 or glibc older then 2.1.x */
+#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
+#define lchown chown
+#endif
 
-const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
-    "Change the group membership of each FILE to GROUP.\n"
-    "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
-const char chown_usage[] = "[OPTION]...  OWNER[.[GROUP] FILE...\n"
-    "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
-    "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
+static long uid;
+static long gid;
 
-
-
-static int fileAction(const char *fileName)
+static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
 {
-    struct stat statBuf;
-    if ((stat(fileName, &statBuf) < 0) || 
-           (chown(fileName, 
-                  ((chownApp==TRUE)? uid: statBuf.st_uid), 
-                  gid) < 0)) { 
+       if (lchown(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
+               return (TRUE);
+       }
        perror(fileName);
-       return( TRUE);
-    }
-    return( FALSE);
+       return (FALSE);
 }
 
 int chown_main(int argc, char **argv)
 {
-    struct group *grp;
-    struct passwd *pwd;
-    int recursiveFlag=FALSE;
-    char *groupName;
+       int opt;
+       int recursiveFlag = FALSE;
+       char *groupName=NULL;
+       char *p=NULL;
 
+       /* do normal option parsing */
+       while ((opt = getopt(argc, argv, "R")) > 0) {
+               switch (opt) {
+                       case 'R':
+                               recursiveFlag = TRUE;
+                               break;
+                       default:
+                               show_usage();
+               }
+       }
 
-    chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE;
-
-    if (argc < 2) {
-       fprintf(stderr, "Usage: %s %s", *argv, 
-               (chownApp==TRUE)? chown_usage : chgrp_usage);
-       return( FALSE);
-    }
-    invocationName=*argv;
-    argc--;
-    argv++;
-
-    /* Parse options */
-    while (**argv == '-') {
-       while (*++(*argv)) switch (**argv) {
-           case 'R':
-               recursiveFlag = TRUE;
-               break;
-           default:
-               fprintf(stderr, "Unknown option: %c\n", **argv);
-               return( FALSE);
+       if (argc > optind && argc > 2 && argv[optind]) {
+               /* First, check if there is a group name here */
+               groupName = strchr(argv[optind], '.');
+               if (groupName == NULL)
+                       groupName = strchr(argv[optind], ':');
+               if (groupName) {
+                       *groupName++ = '\0';
+                       gid = strtoul(groupName, &p, 10);
+                       if (groupName == p)
+                               gid = my_getgrnam(groupName);
+               } else {
+                       gid = -1;
+               }
+               /* Now check for the username */
+               uid = strtoul(argv[optind], &p, 10);    /* Is is numeric? */
+               if (argv[optind] == p) {
+                       uid = my_getpwnam(argv[optind]);
+               }
+       } else {
+               error_msg_and_die(too_few_args);
        }
-       argc--;
-       argv++;
-    }
-    
-    /* Find the selected group */
-    groupName = strchr(*argv, '.');
-    if ( chownApp==TRUE && groupName )
-       *groupName++ = '\0';
-    else
-       groupName = *argv;
-    grp = getgrnam(groupName);
-    if (grp == NULL) {
-       fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
-       return( FALSE);
-    }
-    gid = grp->gr_gid;
 
-    /* Find the selected user (if appropriate)  */
-    if (chownApp==TRUE) {
-       pwd = getpwnam(*argv);
-       if (pwd == NULL) {
-           fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
-           return( FALSE);
+       /* Ok, ready to do the deed now */
+       while (++optind < argc) {
+               if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 
+                                       fileAction, fileAction, NULL) == FALSE) {
+                       return EXIT_FAILURE;
+               }
        }
-       uid = pwd->pw_uid;
-    }
+       return EXIT_SUCCESS;
 
-    /* Ok, ready to do the deed now */
-    if (argc <= 1) {
-       fprintf(stderr, "%s: too few arguments", invocationName);
-       return( FALSE);
-    }
-    while (argc-- > 1) {
-       argv++;
-       if (recursiveFlag==TRUE) 
-           recursiveAction( *argv, TRUE, fileAction, fileAction);
-       else
-           fileAction( *argv);
-    }
-    return(TRUE);
 }
+
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/