chgrp.c (1052B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <errno.h> 3 #include <grp.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <sys/stat.h> 8 #include <sys/types.h> 9 #include <unistd.h> 10 11 #include "util.h" 12 13 static int gid; 14 static int status; 15 static int rflag; 16 static struct stat st; 17 18 static void 19 usage(void) 20 { 21 eprintf("usage: chgrp [-R] groupname file...\n"); 22 } 23 24 static void 25 chgrp(const char *path) 26 { 27 if (chown(path, st.st_uid, gid) < 0) { 28 weprintf("chown %s:", path); 29 status = 1; 30 } 31 if (rflag) 32 recurse(path, chgrp); 33 } 34 35 int 36 main(int argc, char *argv[]) 37 { 38 struct group *gr; 39 40 ARGBEGIN { 41 case 'R': 42 rflag = 1; 43 break; 44 default: 45 usage(); 46 } ARGEND; 47 48 if (argc < 2) 49 usage(); 50 51 errno = 0; 52 gr = getgrnam(argv[0]); 53 if (!gr) { 54 if (errno) 55 eprintf("getgrnam %s:", argv[0]); 56 else 57 eprintf("getgrnam %s: no such group\n", argv[0]); 58 } 59 gid = gr->gr_gid; 60 61 while (*++argv) { 62 if (stat(*argv, &st) < 0) { 63 weprintf("stat %s:", *argv); 64 status = 1; 65 continue; 66 } 67 chgrp(*argv); 68 } 69 return status; 70 }