blob: a611f92f1863961a61ec017311465954e59c3ed5 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include "internal.h"
2#include <pwd.h>
3#include <grp.h>
4#include <string.h>
5#include <stdio.h>
6
7const char chown_usage[] = "chown [-R] user-name file [file ...]\n"
8"\n\tThe group list is kept in the file /etc/groups.\n\n"
9"\t-R:\tRecursively change the mode of all files and directories\n"
10"\t\tunder the argument directory.";
11
12int
13parse_user_name(const char * s, struct FileInfo * i)
14{
15 struct passwd * p;
16 char * dot = strchr(s, '.');
17
18 if (! dot )
19 dot = strchr(s, ':');
20
21 if ( dot )
22 *dot = '\0';
23
24 if ( (p = getpwnam(s)) == 0 ) {
25 fprintf(stderr, "%s: no such user.\n", s);
26 return 1;
27 }
28 i->userID = p->pw_uid;
29
30 if ( dot ) {
31 struct group * g = getgrnam(++dot);
32 if ( g == 0 ) {
33 fprintf(stderr, "%s: no such group.\n", dot);
34 return 1;
35 }
36 i->groupID = g->gr_gid;
37 i->changeGroupID = 1;
38 }
39 return 0;
40}
41
42extern int
43chown_main(struct FileInfo * i, int argc, char * * argv)
44{
45 int status;
46
47 while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
48 i->recursive = 1;
49 argc--;
50 argv++;
51 }
52
53 if ( (status = parse_user_name(argv[1], i)) != 0 )
54 return status;
55
56 argv++;
57 argc--;
58
59 i->changeUserID = 1;
60 i->complainInPostProcess = 1;
61
62 return monadic_main(i, argc, argv);
63}