79 lines
1.4 KiB
C
79 lines
1.4 KiB
C
#include <grp.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
#include "make_path.h"
|
|
#include "get_stat.h"
|
|
#include "recurse.h"
|
|
|
|
unsigned int r_flag;
|
|
unsigned int f_flag;
|
|
unsigned int H_flag;
|
|
|
|
int change(const char *path, void *p) {
|
|
struct group *grp = (struct group *)p;
|
|
|
|
struct stat stat_path;
|
|
if (mu_get_stat("chgrp", path, &stat_path))
|
|
return 1;
|
|
|
|
if (lchown(path, stat_path.st_uid, grp->gr_gid)) {
|
|
if (!f_flag)
|
|
fprintf(stderr, "chgrp: %s: %s\n", path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "RfH")) != -1) {
|
|
switch (opt) {
|
|
case 'R':
|
|
r_flag = 1;
|
|
break;
|
|
|
|
case 'f':
|
|
f_flag = 1;
|
|
break;
|
|
|
|
case 'H':
|
|
H_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
printf("chgrp [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R Recursive] [-f Silent]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (argv[optind] == NULL) {
|
|
fprintf(stderr, "chgrp: missing operand\n");
|
|
return 1;
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
struct group *grp = getgrnam(argv[0]);
|
|
if (!grp) {
|
|
fprintf(stderr, "chgrp: unknow group\n");
|
|
return 1;
|
|
}
|
|
|
|
int ret = 0;
|
|
for (int i = 1; i < argc; i++)
|
|
if (mu_recurse((f_flag) ? NULL : "chgrp", H_flag, argv[i], (void *)grp, change, change))
|
|
ret = 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
|