added -h flag to chgrp

This commit is contained in:
qorg11 2020-09-07 21:29:14 +02:00
parent b244eb3037
commit 8eed6cca17
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 22 additions and 5 deletions

View File

@ -7,18 +7,35 @@
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[1]);
struct group *group_data = getgrnam(argv[optind
]);
gid_t gid = group_data->gr_gid;
for(int i = 2; i<argc; i++)
for(int i = optind+1; i<argc; i++)
{
if(chown(argv[i],gid,getuid()) == -1)
fprintf(stderr,"error %i = %s\n",errno,strerror(errno));
if(follow_symlink)
{
if(lchown(argv[i],gid,getuid()) == -1)
fprintf(stderr,"Error: %i = %s\n",errno,strerror(errno));
}
else
if(chown(argv[i],gid,getuid()) == -1)
{
fprintf(stderr,"Error: %i = %s\n",errno,strerror(errno));
}
}
}