wc: Added wc
[mutos-utils.git] / chgrp.c
blob5e3e3e5baa3e47d6528c8d1e1bfc46d94d9c7348
1 /*
2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
15 #include <stdio.h>
16 #include <grp.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <string.h>
21 #define VERSION "0.01"
23 int main(int argc, char* argv[])
25 if (argc == 1) {
26 fprintf(stderr, "%s: missing operand\n"
27 "Run '%s --help' for usage.\n",
28 argv[0], argv[0]);
29 return 1;
32 // flag parsing
33 int arg = 0;
34 for (arg = 1; arg < argc; arg++)
36 if (strcmp(argv[arg], "--help") == 0) {
37 printf("Usage: %s [group] [file ...]\n", argv[0]);
38 return 0;
39 } else if (strcmp(argv[arg], "--version") == 0) {
40 printf("chgrp (mutos) v"VERSION"\n");
41 return 0;
42 } else {
43 break;
47 struct group *group = getgrnam(argv[arg]);
49 if (!group) {
50 fprintf(stderr, "%s: invalid group: '%s'\n", argv[0], argv[arg]);
51 return 1;
54 arg++; // go to next arg
56 if (argc - arg < 1) {
57 fprintf(stderr, "%s: No file specified\n", argv[0]);
58 return 1;
61 for ( ; arg < argc; arg++)
63 int rc = chown(argv[arg], -1, group->gr_gid);
64 if (rc == -1) {
65 fprintf(stderr, "%s: %s: %s\n", argv[0], strerror(errno), argv[arg]);
66 return 1;
70 return 0;