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, void* junk)
27 if (!chown_func(fileName,
28 (uid == -1) ? statbuf->st_uid : uid,
29 (gid == -1) ? statbuf->st_gid : gid)) {
32 bb_perror_msg("%s", fileName); /* A filename could have % in it... */
39 int chown_main(int argc, char **argv)
42 int retval = EXIT_SUCCESS;
45 flags = bb_getopt_ulflags(argc, argv, "Rh");
47 if (flags & FLAG_h) chown_func = lchown;
49 if (argc - optind < 2) {
55 /* First, check if there is a group name here */
56 if ((groupName = strchr(*argv, '.')) == NULL) {
57 groupName = strchr(*argv, ':');
60 /* Check for the username and groupname */
63 gid = get_ug_id(groupName, bb_xgetgrnam);
65 if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
68 /* Ok, ready to do the deed now */
70 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
71 fileAction, fileAction, NULL)) {
72 retval = EXIT_FAILURE;