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.
23 #define VERSION "0.01"
25 int main(int argc
, char* argv
[])
28 fprintf(stderr
, "%s: missing operand\n"
29 "Run '%s --help' for usage.\n",
36 for (arg
= 1; arg
< argc
; arg
++)
38 if (strcmp(argv
[arg
], "--help") == 0) {
39 printf("Usage: %s [owner][:group] [file ...]\n", argv
[0]);
41 } else if (strcmp(argv
[arg
], "--version") == 0) {
42 printf("chown (mutos) v"VERSION
"\n");
50 for (size_t i
= 0; i
< strlen(argv
[arg
]); i
++)
52 if (argv
[arg
][i
] == ':') {
58 struct passwd
*user
= NULL
;
59 struct group
*group
= NULL
;
61 char* username
= NULL
;
62 char* groupname
= NULL
;
64 if (colon_index
== -1) {
65 user
= getpwnam(argv
[arg
]);
68 username
= strndup(argv
[arg
], colon_index
);
69 groupname
= strndup(argv
[arg
] + colon_index
+ 1, strlen(argv
[arg
] - colon_index
- 1));
71 user
= getpwnam(username
);
72 group
= getgrnam(groupname
);
76 fprintf(stderr
, "%s: invalid user: '%s'\n",
81 if (!group
&& groupname
) {
82 fprintf(stderr
, "%s: invalid group: '%s'\n",
87 arg
++; // go to next arg
90 fprintf(stderr
, "%s: No file specified\n", argv
[0]);
94 for ( ; arg
< argc
; arg
++)
96 int rc
= chown(argv
[arg
], user
->pw_uid
, group
? group
->gr_gid
: -1);
98 fprintf(stderr
, "%s: %s: %s\n", argv
[0], strerror(errno
), argv
[arg
]);