* archival/bunzip2.c (bunzip2_main): Do not remove files if writing to standard
[oweals/busybox.git] / coreutils / chown.c
index e714b2d7a0aac851bf256360ea4ce54a927a906f..846e27c20c114ba2a52070f4926428ca0c1fb55e 100644 (file)
@@ -1,10 +1,9 @@
 /* vi: set sw=4 ts=4: */
 /*
- * Mini chown/chmod/chgrp implementation for busybox
+ * Mini chown implementation for busybox
  *
- *
- * Copyright (C) 1999,2000,2001 by Lineo, inc.
- * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
+ * Copyright (C) 1999,2000,2001 by Erik Andersen <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
 #define lchown chown
 #endif
 
-static long uid = -1;
-static long gid = -1;
+static long uid;
+static long gid;
+
+static int (*chown_func)(const char *, __uid_t, __gid_t) = 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 +49,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], '.');
@@ -86,8 +94,8 @@ int chown_main(int argc, char **argv)
 
        /* Ok, ready to do the deed now */
        while (++optind < argc) {
-               if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 
-                                       fileAction, fileAction, NULL) == FALSE) {
+               if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 
+                                       fileAction, fileAction, NULL)) {
                        return EXIT_FAILURE;
                }
        }