chown, env: stop using statics
[oweals/busybox.git] / coreutils / chown.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chown implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 defects - none? */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
13
14 #include "busybox.h"
15
16 #define OPT_STR     ("Rh" USE_DESKTOP("vcfLHP"))
17 #define BIT_RECURSE 1
18 #define OPT_RECURSE (option_mask32 & 1)
19 #define OPT_NODEREF (option_mask32 & 2)
20 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
21 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
22 #define OPT_QUIET   (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
23 /* POSIX options
24  * -L traverse every symbolic link to a directory encountered
25  * -H if a command line argument is a symbolic link to a directory, traverse it
26  * -P do not traverse any symbolic links (default)
27  * We do not conform to the following:
28  * "Specifying more than one of -H, -L, and -P is not an error.
29  * The last option specified shall determine the behavior of the utility." */
30 /* -L */
31 #define BIT_TRAVERSE 0x20
32 #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
33 /* -H or -L */
34 #define BIT_TRAVERSE_TOP (0x20|0x40)
35 #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
36
37 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
38
39 static struct bb_uidgid_t ugid = { -1, -1 };
40
41 static int fileAction(const char *fileName, struct stat *statbuf,
42                 void *cf, int depth)
43 {
44         uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid;
45         gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid;
46
47         if (!((chown_fptr)cf)(fileName, u, g)) {
48                 if (OPT_VERBOSE
49                  || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
50                 ) {
51                         printf("changed ownership of '%s' to %u:%u\n",
52                                         fileName, (unsigned)u, (unsigned)g);
53                 }
54                 return TRUE;
55         }
56         if (!OPT_QUIET)
57                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
58         return FALSE;
59 }
60
61 int chown_main(int argc, char **argv);
62 int chown_main(int argc, char **argv)
63 {
64         int retval = EXIT_SUCCESS;
65         chown_fptr chown_func;
66
67         opt_complementary = "-2";
68         getopt32(argc, argv, OPT_STR);
69         argv += optind;
70
71         /* This matches coreutils behavior (almost - see below) */
72         chown_func = chown;
73         if (OPT_NODEREF
74             /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
75             USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
76         ) {
77                 chown_func = lchown;
78         }
79
80         parse_chown_usergroup_or_die(&ugid, argv[0]);
81
82         /* Ok, ready to do the deed now */
83         argv++;
84         do {
85                 char *arg = *argv;
86
87                 if (OPT_TRAVERSE_TOP) {
88                         /* resolves symlink (even recursive) */
89                         arg = xmalloc_realpath(arg);
90                         if (!arg)
91                                 continue;
92                 }
93
94                 if (!recursive_action(arg,
95                                 OPT_RECURSE,    // recurse
96                                 OPT_TRAVERSE,   // follow links if -L
97                                 FALSE,          // depth first
98                                 fileAction,     // file action
99                                 fileAction,     // dir action
100                                 chown_func,     // user data
101                                 0)              // depth
102                 ) {
103                         retval = EXIT_FAILURE;
104                 }
105
106                 if (OPT_TRAVERSE_TOP)
107                         free(arg);
108         } while (*++argv);
109
110         return retval;
111 }
112
113 /*
114 Testcase. Run in empty directory.
115
116 #!/bin/sh
117 t1="/tmp/busybox chown"
118 t2="/usr/bin/chown"
119 create() {
120     rm -rf $1; mkdir $1
121     (
122     cd $1 || exit 1
123     mkdir dir dir2
124     >up
125     >file
126     >dir/file
127     >dir2/file
128     ln -s dir linkdir
129     ln -s file linkfile
130     ln -s ../up dir/linkup
131     ln -s ../dir2 dir/linkupdir2
132     )
133     chown -R 0:0 $1
134 }
135 tst() {
136     create test1
137     create test2
138     (cd test1; $t1 $1)
139     (cd test2; $t2 $1)
140     (cd test1; ls -lnR) >out1
141     (cd test2; ls -lnR) >out2
142     echo "chown $1" >out.diff
143     if ! diff -u out1 out2 >>out.diff; then exit 1; fi
144     rm out.diff
145 }
146 tst_for_each() {
147     tst "$1 1:1 file"
148     tst "$1 1:1 dir"
149     tst "$1 1:1 linkdir"
150     tst "$1 1:1 linkfile"
151 }
152 echo "If script produced 'out.diff' file, then at least one testcase failed"
153 # These match coreutils 6.8:
154 tst_for_each ""
155 tst_for_each "-R"
156 tst_for_each "-RP"
157 tst_for_each "-RL"
158 tst_for_each "-RH"
159 tst_for_each "-h"
160 tst_for_each "-hR"
161 tst_for_each "-hRP"
162 # Below: with "chown linkdir" coreutils 6.8 will chown linkdir _target_,
163 # we lchown _the link_. I believe we are "more correct".
164 #tst_for_each "-hRL"
165 #tst_for_each "-hRH"
166
167 */