1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int c;
int follow_symlink;
while((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
case 'h':
follow_symlink = 1;
break;
}
}
if(argc == 1 || argc == 2) {
fprintf(stderr, "usage: chgrp group file...\n");
return 1;
}
struct group *group_data = getgrnam(argv[optind]);
gid_t gid = group_data->gr_gid;
for(int i = optind + 1; i < argc; i++) {
if(follow_symlink) {
if(lchown(argv[i], gid, getuid()) == -1) {
fprintf(stderr, "chgrp: %s: %s\n", argv[i], strerror(errno));
return 1;
}
} else if(chown(argv[i], gid, getuid()) == -1) {
fprintf(stderr, "chgrp: %s: %s\n", argv[i], strerror(errno));
return 1;
}
}
return 0;
}
|