Updates from both Vladimir and Larry
[oweals/busybox.git] / chown.c
diff --git a/chown.c b/chown.c
index 43d62b1e07d14c694f459664fdba6e8b799ddd4f..a656072041b9b2bc58dfee138fb6ca69debaad64 100644 (file)
--- a/chown.c
+++ b/chown.c
 #define lchown chown
 #endif
 
-static long uid = -1;
-static long gid = -1;
+static long uid;
+static long gid;
+
+static int (*chown_func)() = chown;
 
 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
 {
-       if (lchown(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
+       if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
                return (TRUE);
        }
        perror(fileName);
@@ -48,20 +50,27 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
 int chown_main(int argc, char **argv)
 {
        int opt;
-       int recursiveFlag = FALSE;
+       int recursiveFlag = FALSE,
+               noderefFlag = FALSE;
        char *groupName=NULL;
        char *p=NULL;
 
        /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "R")) > 0) {
+       while ((opt = getopt(argc, argv, "Rh")) > 0) {
                switch (opt) {
                        case 'R':
                                recursiveFlag = TRUE;
+                               break;
+                       case 'h':
+                               noderefFlag = TRUE;
+                               break;
                        default:
                                show_usage();
                }
        }
 
+       if (noderefFlag) chown_func = lchown;
+
        if (argc > optind && argc > 2 && argv[optind]) {
                /* First, check if there is a group name here */
                groupName = strchr(argv[optind], '.');
@@ -85,7 +94,7 @@ int chown_main(int argc, char **argv)
        }
 
        /* Ok, ready to do the deed now */
-       while (optind++ < argc-1) {
+       while (++optind < argc) {
                if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 
                                        fileAction, fileAction, NULL) == FALSE) {
                        return EXIT_FAILURE;