blob: 5064f2d6e8aa1ec2a907084ace821503af5a5945 [file] [log] [blame]
Eric Andersen9f0fedb2001-04-24 18:07:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Mini chgrp implementation for busybox
Eric Andersen9f0fedb2001-04-24 18:07:19 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen9f0fedb2001-04-24 18:07:19 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* 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
Eric Andersen9f0fedb2001-04-24 18:07:19 +000015#include <stdlib.h>
Eric Andersen9f0fedb2001-04-24 18:07:19 +000016#include <unistd.h>
17#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000018
Eric Andersen9f0fedb2001-04-24 18:07:19 +000019static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
20{
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
Eric Andersen9f0fedb2001-04-24 18:07:19 +000022 return (TRUE);
23 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
Eric Andersen9f0fedb2001-04-24 18:07:19 +000025 return (FALSE);
26}
27
28int chgrp_main(int argc, char **argv)
29{
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 long gid;
Manuel Novoa III 4eff1812003-03-19 09:42:02 +000031 int recursiveFlag;
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 int retval = EXIT_SUCCESS;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000033
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 recursiveFlag = bb_getopt_ulflags(argc, argv, "R");
35
36 if (argc - optind < 2) {
37 bb_show_usage();
Eric Andersen9f0fedb2001-04-24 18:07:19 +000038 }
39
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 argv += optind;
41
42 /* Find the selected group */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000043 gid = get_ug_id(*argv, bb_xgetgrnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 ++argv;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000045
46 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 do {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 fileAction, fileAction, &gid)) {
50 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000051 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000055}