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 - none? */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
16 /* This is a NOEXEC applet. Be very careful! */
19 #define OPT_STR ("Rh" IF_DESKTOP("vcfLHP"))
21 #define OPT_RECURSE (opt & 1)
22 #define OPT_NODEREF (opt & 2)
23 #define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
24 #define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
25 #define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
27 * -L traverse every symbolic link to a directory encountered
28 * -H if a command line argument is a symbolic link to a directory, traverse it
29 * -P do not traverse any symbolic links (default)
30 * We do not conform to the following:
31 * "Specifying more than one of -H, -L, and -P is not an error.
32 * The last option specified shall determine the behavior of the utility." */
34 #define BIT_TRAVERSE 0x20
35 #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
37 #define BIT_TRAVERSE_TOP (0x20|0x40)
38 #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
40 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
43 struct bb_uidgid_t ugid;
44 chown_fptr chown_func;
47 static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
48 void *vparam, int depth UNUSED_PARAM)
50 #define param (*(struct param_t*)vparam)
51 #define opt option_mask32
52 uid_t u = (param.ugid.uid == (uid_t)-1) ? statbuf->st_uid : param.ugid.uid;
53 gid_t g = (param.ugid.gid == (gid_t)-1) ? statbuf->st_gid : param.ugid.gid;
55 if (param.chown_func(fileName, u, g) == 0) {
57 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
59 printf("changed ownership of '%s' to %u:%u\n",
60 fileName, (unsigned)u, (unsigned)g);
65 bb_simple_perror_msg(fileName); /* A filename can have % in it... */
71 int chown_main(int argc UNUSED_PARAM, char **argv)
73 int retval = EXIT_SUCCESS;
79 param.chown_func = chown;
81 opt_complementary = "-2";
82 opt = getopt32(argv, OPT_STR);
85 /* This matches coreutils behavior (almost - see below) */
87 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
88 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
90 param.chown_func = lchown;
93 flags = ACTION_DEPTHFIRST; /* match coreutils order */
95 flags |= ACTION_RECURSE;
97 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
99 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
101 parse_chown_usergroup_or_die(¶m.ugid, argv[0]);
103 /* Ok, ready to do the deed now */
106 if (!recursive_action(*argv,
108 fileAction, /* file action */
109 fileAction, /* dir action */
110 ¶m, /* user data */
113 retval = EXIT_FAILURE;
121 Testcase. Run in empty directory.
124 t1="/tmp/busybox chown"
137 ln -s ../up dir/linkup
138 ln -s ../dir2 dir/linkupdir2
145 echo "[$1]" >>test1.out
146 echo "[$1]" >>test2.out
147 (cd test1; $t1 $1) >>test1.out 2>&1
148 (cd test2; $t2 $1) >>test2.out 2>&1
149 (cd test1; ls -lnR) >out1
150 (cd test2; ls -lnR) >out2
151 echo "chown $1" >out.diff
152 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
159 tst "$1 1:1 linkfile"
161 echo "If script produced 'out.diff' file, then at least one testcase failed"
164 # These match coreutils 6.8:
175 # Fix `name' in coreutils output
176 sed 's/`/'"'"'/g' -i test2.out
177 # Compare us with coreutils output
178 diff -u test1.out test2.out