getopt_ulflags -> getopt32.
[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-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
11 /* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
12 /* BB_AUDIT Note: gnu chgrp does not support -H, -L, or -P. */
13 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include "busybox.h"
18
19 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
20 {
21         if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
22                 return (TRUE);
23         }
24         bb_perror_msg("%s", fileName);  /* Avoid multibyte problems. */
25         return (FALSE);
26 }
27
28 int chgrp_main(int argc, char **argv)
29 {
30         long gid;
31         int recursiveFlag;
32         int retval = EXIT_SUCCESS;
33
34         recursiveFlag = getopt32(argc, argv, "R");
35
36         if (argc - optind < 2) {
37                 bb_show_usage();
38         }
39
40         argv += optind;
41
42         /* Find the selected group */
43         gid = get_ug_id(*argv, bb_xgetgrnam);
44         ++argv;
45
46         /* Ok, ready to do the deed now */
47         do {
48                 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
49                                                                 fileAction, fileAction, &gid)) {
50                         retval = EXIT_FAILURE;
51                 }
52         } while (*++argv);
53
54         return retval;
55 }