- patch from Denis Vlasenko to add and use bb_xopen3()
[oweals/busybox.git] / coreutils / chgrp.c
index 83bb194635da11a22f64d958a7af2cb772a475c8..70ac672c2b50801d8a6f553d5422c49daef33b30 100644 (file)
@@ -1,10 +1,8 @@
 /* vi: set sw=4 ts=4: */
 /*
- * Mini chown/chmod/chgrp implementation for busybox
+ * Mini chgrp implementation for busybox
  *
- *
- * Copyright (C) 1999,2000,2001 by Lineo, inc.
- * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.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>
+/* 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 chgrp does not support -H, -L, or -P. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
+
 #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 */
+/* Don't use lchown glibc older then 2.1.x */
 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
 #define lchown chown
 #endif
 
-
-static long gid = -1;
-
 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
 {
-       if (lchown(fileName, statbuf->st_uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
+       if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
                return (TRUE);
        }
-       perror(fileName);
+       bb_perror_msg("%s", fileName);  /* Avoid multibyte problems. */
        return (FALSE);
 }
 
 int chgrp_main(int argc, char **argv)
 {
-       int opt;
-       int recursiveFlag = FALSE;
-       char *p=NULL;
+       long gid;
+       int recursiveFlag;
+       int retval = EXIT_SUCCESS;
 
-       /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "R")) > 0) {
-               switch (opt) {
-                       case 'R':
-                               recursiveFlag = TRUE;
-                       default:
-                               show_usage();
-               }
-       }
+       recursiveFlag = bb_getopt_ulflags(argc, argv, "R");
 
-       if (argc > optind && argc > 2 && argv[optind]) {
-               /* Find the selected group */
-               gid = strtoul(argv[optind], &p, 10);    /* maybe it's already numeric */
-               if (argv[optind] == p)
-                       gid = my_getgrnam(argv[optind]);
-       } else {
-               error_msg_and_die(too_few_args);
+       if (argc - optind < 2) {
+               bb_show_usage();
        }
 
+       argv += optind;
+
+       /* Find the selected group */
+       gid = get_ug_id(*argv, bb_xgetgrnam);
+       ++argv;
+
        /* Ok, ready to do the deed now */
-       while (optind++ < argc-1) {
-               if (recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 
-                                       fileAction, fileAction, NULL) == FALSE) {
-                       return EXIT_FAILURE;
+       do {
+               if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
+                                                               fileAction, fileAction, &gid)) {
+                       retval = EXIT_FAILURE;
                }
-       }
-       return EXIT_SUCCESS;
+       } while (*++argv);
 
+       return retval;
 }
 
 /*