summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-05 11:06:35 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-05 11:06:35 -0400
commit6ff4ec00482f299735276268853ff75311f75091 (patch)
treeb4e6ef7e6d7ba362b94abc396905e20d8917f2bc
parent7a01cc6e4e52a1fc1a70e211a7037963ac9abc98 (diff)
do recursion
-rw-r--r--chgrp.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/chgrp.c b/chgrp.c
index 80fdb7f..b5bd1ce 100644
--- a/chgrp.c
+++ b/chgrp.c
@@ -25,6 +25,7 @@
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <ftw.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -32,6 +33,11 @@
#include <sys/stat.h>
#include <unistd.h>
+#ifndef OPEN_MAX
+#define OPEN_MAX _POSIX_OPEN_MAX
+#endif
+
+static int changelink = 0;
static int newgid = 0;
static int retval = 0;
static enum { UNSET, COMMANDLINE, RECURSIVE, NONE } follow = UNSET;
@@ -64,10 +70,9 @@ static gid_t strtogid(const char *s)
int main(int argc, char **argv)
{
- int c = 0;
- int changelink = 0;
int recursive = 0;
+ int c;
while ((c = getopt(argc, argv, "hHLPR")) != -1) {
switch (c) {
case 'h':
@@ -101,14 +106,19 @@ int main(int argc, char **argv)
}
if (optind >= argc - 1) {
- fprintf(stderr, "chgrp: Group and at least one file are required\n");
+ fprintf(stderr, "chgrp: missing operand\n");
return 1;
}
newgid = strtogid(argv[optind++]);
+ /* TODO: handle -hHLP */
while (optind < argc) {
- chgrp(argv[optind++], NULL, 0, NULL);
+ if (recursive) {
+ nftw(argv[optind++], chgrp, OPEN_MAX, 0);
+ } else {
+ chgrp(argv[optind++], NULL, 0, NULL);
+ }
}
return retval;