Initial revision
[platform/upstream/busybox.git] / chgrp.c
1 /*
2  * Mini chgrp implementation for busybox
3  *
4  * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22 #include "internal.h"
23 #include <grp.h>
24 #include <stdio.h>
25
26 const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n"
27     "Change the group membership of each FILE to GROUP.\n"
28     "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
29
30 int chgrp_main(int argc, char **argv)
31 {
32     const char *cp;
33     int gid;
34     struct group *grp;
35     struct stat statBuf;
36
37     if (argc < 2) {
38         fprintf(stderr, "Usage: %s %s", *argv, chgrp_usage);
39         return 1;
40     }
41     argc--;
42     argv++;
43
44     cp = argv[1];
45     if (isDecimal(*cp)) {
46         gid = 0;
47         while (isDecimal(*cp))
48             gid = gid * 10 + (*cp++ - '0');
49         if (*cp) {
50             fprintf(stderr, "Bad gid value\n");
51             return -1;
52         }
53     } else {
54         grp = getgrnam(cp);
55         if (grp == NULL) {
56             fprintf(stderr, "Unknown group name\n");
57             return -1;
58         }
59         gid = grp->gr_gid;
60     }
61     argc--;
62     argv++;
63     while (argc-- > 1) {
64         argv++;
65         if ((stat(*argv, &statBuf) < 0) ||
66             (chown(*argv, statBuf.st_uid, gid) < 0)) {
67             perror(*argv);
68         }
69     }
70     return 1;
71 }
72
73
74
75
76
77
78
79
80
81 #if 0
82 int
83 recursive(const char *fileName, BOOL followLinks, const char *pattern,
84           int (*fileAction) (const char *fileName,
85                              const struct stat * statbuf),
86           int (*dirAction) (const char *fileName,
87                             const struct stat * statbuf))
88
89 #endif