Update a bunch of docs. Run a script to update my email addr.
[oweals/busybox.git] / coreutils / chgrp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chgrp implementation for busybox
4  *
5  * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 /* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
24 /* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
25 /* BB_AUDIT Note: gnu chgrp does not support -H, -L, or -P. */
26 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
27
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include "busybox.h"
31
32 /* Don't use lchown for libc5 or glibc older then 2.1.x */
33 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
34 #define lchown  chown
35 #endif
36
37 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
38 {
39         if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
40                 return (TRUE);
41         }
42         bb_perror_msg("%s", fileName);  /* Avoid multibyte problems. */
43         return (FALSE);
44 }
45
46 int chgrp_main(int argc, char **argv)
47 {
48         long gid;
49         int recursiveFlag;
50         int retval = EXIT_SUCCESS;
51         char *p;
52
53         recursiveFlag = bb_getopt_ulflags(argc, argv, "R");
54
55         if (argc - optind < 2) {
56                 bb_show_usage();
57         }
58
59         argv += optind;
60
61         /* Find the selected group */
62         gid = strtoul(*argv, &p, 10);   /* maybe it's already numeric */
63         if (*p || (p == *argv)) {               /* trailing chars or nonnumeric */
64                 gid = my_getgrnam(*argv);
65         }
66         ++argv;
67
68         /* Ok, ready to do the deed now */
69         do {
70                 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE, 
71                                                                 fileAction, fileAction, &gid)) {
72                         retval = EXIT_FAILURE;
73                 }
74         } while (*++argv);
75
76         return retval;
77 }
78
79 /*
80 Local Variables:
81 c-file-style: "linux"
82 c-basic-offset: 4
83 tab-width: 4
84 End:
85 */