114 lines
2.0 KiB
C
114 lines
2.0 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"
|
|
|
|
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
|
|
unsigned int r_flag;
|
|
unsigned int s_flag;
|
|
|
|
int change(const char *file, const struct group *grp) {
|
|
struct stat stat_path;
|
|
if (mu_get_stat("chgrp", file, &stat_path))
|
|
return 1;
|
|
|
|
if (lchown(file, stat_path.st_uid, grp->gr_gid)) {
|
|
if (!s_flag)
|
|
fprintf(stderr, "chgrp: %s: %s\n", file, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int chtree(const char *dst, const struct group *grp) {
|
|
int ret = change(dst, grp);
|
|
|
|
struct stat stat_path;
|
|
if (get_stat("chgrp", dst, &stat_path))
|
|
return 1;
|
|
|
|
if (!S_ISDIR(stat_path.st_mode) || !r_flag)
|
|
return ret;
|
|
|
|
DIR *dir = opendir(dst);
|
|
if (dir == NULL) {
|
|
if (!s_flag)
|
|
fprintf(stderr, "chgrp: %s: Can`t open directory\n", dst);
|
|
|
|
return 1;
|
|
}
|
|
|
|
struct dirent *ep;
|
|
while ((ep = readdir(dir)) != NULL) {
|
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
|
continue;
|
|
|
|
char *full_path = mu_make_path("chgrp", dst, ep->d_name);
|
|
if (full_path == NULL)
|
|
continue;
|
|
|
|
if (chtree(full_path, grp))
|
|
ret = 1;
|
|
|
|
free(full_path);
|
|
}
|
|
|
|
closedir(dir);
|
|
return ret;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
get_stat = mu_get_lstat;
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "RfH") != -1)) {
|
|
switch (opt) {
|
|
case 'R':
|
|
r_flag = 1;
|
|
break;
|
|
|
|
case 'f':
|
|
s_flag = 1;
|
|
break;
|
|
|
|
case 'H':
|
|
get_stat = mu_get_stat;
|
|
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;
|
|
}
|
|
|
|
struct group *grp = getgrnam(argv[optind]);
|
|
if (!grp) {
|
|
fprintf(stderr, "chgrp: unknow group\n");
|
|
return 1;
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
int ret = 0;
|
|
for (int i = 1; i < argc; i++)
|
|
if (chtree(argv[i], grp))
|
|
ret = 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
|