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, -L, and -P. */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
16 static struct bb_uidgid_t ugid = { -1, -1 };
18 static int (*chown_func)(const char *, uid_t, gid_t) = chown;
20 #define OPT_RECURSE (option_mask32 & 1)
21 #define OPT_NODEREF (option_mask32 & 2)
22 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
23 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
24 #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
25 #define OPT_STR ("Rh" USE_DESKTOP("vcf"))
28 * -H if a command line argument is a symbolic link to a directory, traverse it
29 * -L traverse every symbolic link to a directory encountered
30 * -P do not traverse any symbolic links (default)
33 static int fileAction(const char *fileName, struct stat *statbuf,
34 void ATTRIBUTE_UNUSED *junk, int depth)
37 // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
39 if (!chown_func(fileName,
40 (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid,
41 (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid)
44 || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid))
46 printf("changed ownership of '%s' to %u:%u\n",
47 fileName, ugid.uid, ugid.gid);
52 bb_perror_msg("%s", fileName); /* A filename can have % in it... */
56 int chown_main(int argc, char **argv)
59 int retval = EXIT_SUCCESS;
61 opt_complementary = "-2";
62 getopt32(argc, argv, OPT_STR);
65 if (OPT_NODEREF) chown_func = lchown;
67 /* First, check if there is a group name here */
68 groupName = strchr(*argv, '.'); /* deprecated? */
70 groupName = strchr(*argv, ':');
72 *groupName = ':'; /* replace '.' with ':' */
74 /* First, try parsing "user[:[group]]" */
75 if (!groupName) { /* "user" */
76 ugid.uid = get_ug_id(*argv, xuname2uid);
77 } else if (groupName == *argv) { /* ":group" */
78 ugid.gid = get_ug_id(groupName + 1, xgroup2gid);
80 if (!groupName[1]) /* "user:" */
82 if (!get_uidgid(&ugid, *argv, 1))
83 bb_error_msg_and_die("unknown user/group %s", *argv);
86 /* Ok, ready to do the deed now */
89 if (!recursive_action(*argv,
90 OPT_RECURSE, // recurse
91 FALSE, // follow links: TODO: -H/-L/-P
93 fileAction, // file action
94 fileAction, // dir action
98 retval = EXIT_FAILURE;