1 /* vi: set sw=4 ts=4: */
3 * Mini chown implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 chown does not support -H, -L, or -P. */
13 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
20 static uid_t uid = -1;
21 static gid_t gid = -1;
23 static int (*chown_func)(const char *, uid_t, gid_t) = chown;
25 static int fileAction(const char *fileName, struct stat *statbuf,
26 void ATTRIBUTE_UNUSED *junk)
28 if (!chown_func(fileName,
29 (uid == (uid_t)-1) ? statbuf->st_uid : uid,
30 (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
33 bb_perror_msg("%s", fileName); /* A filename could have % in it... */
40 int chown_main(int argc, char **argv)
43 int retval = EXIT_SUCCESS;
46 flags = bb_getopt_ulflags(argc, argv, "Rh");
48 if (flags & FLAG_h) chown_func = lchown;
50 if (argc - optind < 2) {
56 /* First, check if there is a group name here */
57 if ((groupName = strchr(*argv, '.')) == NULL) {
58 groupName = strchr(*argv, ':');
61 /* Check for the username and groupname */
64 gid = get_ug_id(groupName, bb_xgetgrnam);
66 if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
69 /* Ok, ready to do the deed now */
71 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
72 fileAction, fileAction, NULL)) {
73 retval = EXIT_FAILURE;