+/* vi: set sw=4 ts=4: */
/*
- * Mini 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 "internal.h"
-#include <grp.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "busybox.h"
-const char chgrp_usage[] = "chgrp [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";
+/* Don't use lchown for libc5 or glibc older then 2.1.x */
+#if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
+#define lchown chown
+#endif
-int chgrp_main(int argc, char **argv)
-{
- const char *cp;
- int gid;
- struct group *grp;
- struct stat statBuf;
- if (argc < 2) {
- fprintf(stderr, "Usage: %s %s", *argv, chgrp_usage);
- return 1;
- }
- argc--;
- argv++;
+static long gid;
- cp = argv[1];
- if (isDecimal(*cp)) {
- gid = 0;
- while (isDecimal(*cp))
- gid = gid * 10 + (*cp++ - '0');
- if (*cp) {
- fprintf(stderr, "Bad gid value\n");
- return -1;
- }
- } else {
- grp = getgrnam(cp);
- if (grp == NULL) {
- fprintf(stderr, "Unknown group name\n");
- return -1;
- }
- gid = grp->gr_gid;
- }
- argc--;
- argv++;
- while (argc-- > 1) {
- argv++;
- if ((stat(*argv, &statBuf) < 0) ||
- (chown(*argv, statBuf.st_uid, gid) < 0)) {
- perror(*argv);
+static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
+{
+ if (lchown(fileName, statbuf->st_uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
+ return (TRUE);
}
- }
- return 1;
+ perror(fileName);
+ return (FALSE);
}
+int chgrp_main(int argc, char **argv)
+{
+ int opt;
+ int recursiveFlag = FALSE;
+ char *p=NULL;
+ /* do normal option parsing */
+ while ((opt = getopt(argc, argv, "R")) > 0) {
+ switch (opt) {
+ case 'R':
+ recursiveFlag = TRUE;
+ break;
+ default:
+ show_usage();
+ }
+ }
+ 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);
+ }
+ /* 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;
+ }
+ }
+ return EXIT_SUCCESS;
+}
-
-
-
-#if 0
-int
-recursive(const char *fileName, BOOL followLinks, const char *pattern,
- int (*fileAction) (const char *fileName,
- const struct stat * statbuf),
- int (*dirAction) (const char *fileName,
- const struct stat * statbuf))
-
-#endif
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/