util-linux/lsusb.c: print manufacturer/product strings if available
[busybox-git.git] / coreutils / chown.c
blob528a2a05abfb3b6f2d6018bd7d88def5bfae9bae
1 /* vi: set sw=4 ts=4: */
2 /*
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 source tree.
8 */
9 //config:config CHOWN
10 //config: bool "chown (7.6 kb)"
11 //config: default y
12 //config: help
13 //config: chown is used to change the user and/or group ownership
14 //config: of files.
15 //config:
16 //config:config FEATURE_CHOWN_LONG_OPTIONS
17 //config: bool "Enable long options"
18 //config: default y
19 //config: depends on CHOWN && LONG_OPTS
21 //applet:IF_CHOWN(APPLET_NOEXEC(chown, chown, BB_DIR_BIN, BB_SUID_DROP, chown))
23 //kbuild:lib-$(CONFIG_CHOWN) += chown.o
25 /* BB_AUDIT SUSv3 defects - none? */
26 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
28 //usage:#define chown_trivial_usage
29 //usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
30 //usage:#define chown_full_usage "\n\n"
31 //usage: "Change the owner and/or group of FILEs to USER and/or GRP"
32 //usage: "\n"
33 //usage: "\n -h Affect symlinks instead of symlink targets"
34 //usage: IF_DESKTOP(
35 //usage: "\n -L Traverse all symlinks to directories"
36 //usage: "\n -H Traverse symlinks on command line only"
37 //usage: "\n -P Don't traverse symlinks (default)"
38 //usage: )
39 //next 4 options are the same for chmod/chown/chgrp:
40 //usage: "\n -R Recurse"
41 //usage: IF_DESKTOP(
42 //usage: "\n -c List changed files"
43 //usage: "\n -v Verbose"
44 //usage: "\n -f Hide errors"
45 //usage: )
46 //usage:
47 //usage:#define chown_example_usage
48 //usage: "$ ls -l /tmp/foo\n"
49 //usage: "-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n"
50 //usage: "$ chown root /tmp/foo\n"
51 //usage: "$ ls -l /tmp/foo\n"
52 //usage: "-r--r--r-- 1 root andersen 0 Apr 12 18:25 /tmp/foo\n"
53 //usage: "$ chown root.root /tmp/foo\n"
54 //usage: "ls -l /tmp/foo\n"
55 //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
57 #include "libbb.h"
59 /* This is a NOEXEC applet. Be very careful! */
62 #define OPT_STR "Rh" IF_DESKTOP("vcfLHP")
63 #define BIT_RECURSE 1
64 #define OPT_RECURSE (opt & 1)
65 #define OPT_NODEREF (opt & 2)
66 #define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
67 #define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
68 #define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
69 /* POSIX options
70 * -L traverse every symbolic link to a directory encountered
71 * -H if a command line argument is a symbolic link to a directory, traverse it
72 * -P do not traverse any symbolic links (default)
73 * We do not conform to the following:
74 * "Specifying more than one of -H, -L, and -P is not an error.
75 * The last option specified shall determine the behavior of the utility." */
76 /* -L */
77 #define BIT_TRAVERSE 0x20
78 #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
79 /* -H or -L */
80 #define BIT_TRAVERSE_TOP (0x20|0x40)
81 #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
83 #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
84 static const char chown_longopts[] ALIGN1 =
85 "recursive\0" No_argument "R"
86 "dereference\0" No_argument "\xff"
87 "no-dereference\0" No_argument "h"
88 # if ENABLE_DESKTOP
89 "changes\0" No_argument "c"
90 "silent\0" No_argument "f"
91 "quiet\0" No_argument "f"
92 "verbose\0" No_argument "v"
93 # endif
95 #endif
97 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
99 struct param_t {
100 struct bb_uidgid_t ugid;
101 chown_fptr chown_func;
104 static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
105 const char *fileName, struct stat *statbuf)
107 #define param (*(struct param_t*)state->userData)
108 #define opt option_mask32
109 uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
110 gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
112 if (param.chown_func(fileName, u, g) == 0) {
113 if (OPT_VERBOSE
114 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
116 printf("changed ownership of '%s' to %u:%u\n",
117 fileName, (unsigned)u, (unsigned)g);
119 return TRUE;
121 if (!OPT_QUIET)
122 bb_simple_perror_msg(fileName);
123 return FALSE;
124 #undef opt
125 #undef param
128 int chown_main(int argc UNUSED_PARAM, char **argv)
130 int retval = EXIT_SUCCESS;
131 int opt, flags;
132 struct param_t param;
134 #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
135 opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts);
136 #else
137 opt = getopt32(argv, "^" OPT_STR "\0" "-2");
138 #endif
139 argv += optind;
141 /* This matches coreutils behavior (almost - see below) */
142 param.chown_func = chown;
143 if (OPT_NODEREF
144 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
145 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
147 param.chown_func = lchown;
150 flags = ACTION_DEPTHFIRST; /* match coreutils order */
151 if (OPT_RECURSE)
152 flags |= ACTION_RECURSE;
153 if (OPT_TRAVERSE_TOP)
154 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
155 if (OPT_TRAVERSE)
156 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
158 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
160 /* Ok, ready to do the deed now */
161 while (*++argv) {
162 if (!recursive_action(*argv,
163 flags, /* flags */
164 fileAction, /* file action */
165 fileAction, /* dir action */
166 &param) /* user data */
168 retval = EXIT_FAILURE;
172 return retval;
176 Testcase. Run in empty directory.
178 #!/bin/sh
179 t1="/tmp/busybox chown"
180 t2="/usr/bin/chown"
181 create() {
182 rm -rf $1; mkdir $1
184 cd $1 || exit 1
185 mkdir dir dir2
187 >file
188 >dir/file
189 >dir2/file
190 ln -s dir linkdir
191 ln -s file linkfile
192 ln -s ../up dir/linkup
193 ln -s ../dir2 dir/linkupdir2
195 chown -R 0:0 $1
197 tst() {
198 create test1
199 create test2
200 echo "[$1]" >>test1.out
201 echo "[$1]" >>test2.out
202 (cd test1; $t1 $1) >>test1.out 2>&1
203 (cd test2; $t2 $1) >>test2.out 2>&1
204 (cd test1; ls -lnR) >out1
205 (cd test2; ls -lnR) >out2
206 echo "chown $1" >out.diff
207 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
208 rm out.diff
210 tst_for_each() {
211 tst "$1 1:1 file"
212 tst "$1 1:1 dir"
213 tst "$1 1:1 linkdir"
214 tst "$1 1:1 linkfile"
216 echo "If script produced 'out.diff' file, then at least one testcase failed"
217 >test1.out
218 >test2.out
219 # These match coreutils 6.8:
220 tst_for_each "-v"
221 tst_for_each "-vR"
222 tst_for_each "-vRP"
223 tst_for_each "-vRL"
224 tst_for_each "-vRH"
225 tst_for_each "-vh"
226 tst_for_each "-vhR"
227 tst_for_each "-vhRP"
228 tst_for_each "-vhRL"
229 tst_for_each "-vhRH"
230 # Fix `name' in coreutils output
231 sed 's/`/'"'"'/g' -i test2.out
232 # Compare us with coreutils output
233 diff -u test1.out test2.out