From 6ff4ec00482f299735276268853ff75311f75091 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 5 Aug 2019 11:06:35 -0400 Subject: do recursion --- chgrp.c | 18 ++++++++++++++---- 1 file 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 #include +#include #include #include #include @@ -32,6 +33,11 @@ #include #include +#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; -- cgit v1.2.1