usage.c: remove reference to busybox.h
[platform/upstream/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 "libbb.h"
15
16 /* This is a NOEXEC applet. Be very careful! */
17
18
19 #define OPT_STR     ("Rh" USE_DESKTOP("vcfLHP"))
20 #define BIT_RECURSE 1
21 #define OPT_RECURSE (option_mask32 & 1)
22 #define OPT_NODEREF (option_mask32 & 2)
23 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
24 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
25 #define OPT_QUIET   (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
26 /* POSIX options
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." */
33 /* -L */
34 #define BIT_TRAVERSE 0x20
35 #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
36 /* -H or -L */
37 #define BIT_TRAVERSE_TOP (0x20|0x40)
38 #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
39
40 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
41
42 static struct bb_uidgid_t ugid = { -1, -1 };
43
44 static int fileAction(const char *fileName, struct stat *statbuf,
45                 void *cf, int depth)
46 {
47         uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid;
48         gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid;
49
50         if (!((chown_fptr)cf)(fileName, u, g)) {
51                 if (OPT_VERBOSE
52                  || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
53                 ) {
54                         printf("changed ownership of '%s' to %u:%u\n",
55                                         fileName, (unsigned)u, (unsigned)g);
56                 }
57                 return TRUE;
58         }
59         if (!OPT_QUIET)
60                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
61         return FALSE;
62 }
63
64 int chown_main(int argc, char **argv);
65 int chown_main(int argc, char **argv)
66 {
67         int retval = EXIT_SUCCESS;
68         chown_fptr chown_func;
69
70         opt_complementary = "-2";
71         getopt32(argc, argv, OPT_STR);
72         argv += optind;
73
74         /* This matches coreutils behavior (almost - see below) */
75         chown_func = chown;
76         if (OPT_NODEREF
77             /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
78             USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
79         ) {
80                 chown_func = lchown;
81         }
82
83         parse_chown_usergroup_or_die(&ugid, argv[0]);
84
85         /* Ok, ready to do the deed now */
86         argv++;
87         do {
88                 char *arg = *argv;
89
90                 if (OPT_TRAVERSE_TOP) {
91                         /* resolves symlink (even recursive) */
92                         arg = xmalloc_realpath(arg);
93                         if (!arg)
94                                 continue;
95                 }
96
97                 if (!recursive_action(arg,
98                                 (OPT_RECURSE ? ACTION_RECURSE : 0) | /* recurse */
99                                 (OPT_TRAVERSE ? ACTION_FOLLOWLINKS : 0),/* follow links if -L */
100                                 fileAction,     /* file action */
101                                 fileAction,     /* dir action */
102                                 chown_func,     /* user data */
103                                 0)              /* depth */
104                 ) {
105                         retval = EXIT_FAILURE;
106                 }
107
108                 if (OPT_TRAVERSE_TOP)
109                         free(arg);
110         } while (*++argv);
111
112         return retval;
113 }
114
115 /*
116 Testcase. Run in empty directory.
117
118 #!/bin/sh
119 t1="/tmp/busybox chown"
120 t2="/usr/bin/chown"
121 create() {
122     rm -rf $1; mkdir $1
123     (
124     cd $1 || exit 1
125     mkdir dir dir2
126     >up
127     >file
128     >dir/file
129     >dir2/file
130     ln -s dir linkdir
131     ln -s file linkfile
132     ln -s ../up dir/linkup
133     ln -s ../dir2 dir/linkupdir2
134     )
135     chown -R 0:0 $1
136 }
137 tst() {
138     create test1
139     create test2
140     (cd test1; $t1 $1)
141     (cd test2; $t2 $1)
142     (cd test1; ls -lnR) >out1
143     (cd test2; ls -lnR) >out2
144     echo "chown $1" >out.diff
145     if ! diff -u out1 out2 >>out.diff; then exit 1; fi
146     rm out.diff
147 }
148 tst_for_each() {
149     tst "$1 1:1 file"
150     tst "$1 1:1 dir"
151     tst "$1 1:1 linkdir"
152     tst "$1 1:1 linkfile"
153 }
154 echo "If script produced 'out.diff' file, then at least one testcase failed"
155 # These match coreutils 6.8:
156 tst_for_each ""
157 tst_for_each "-R"
158 tst_for_each "-RP"
159 tst_for_each "-RL"
160 tst_for_each "-RH"
161 tst_for_each "-h"
162 tst_for_each "-hR"
163 tst_for_each "-hRP"
164 # Below: with "chown linkdir" coreutils 6.8 will chown linkdir _target_,
165 # we lchown _the link_. I believe we are "more correct".
166 #tst_for_each "-hRL"
167 #tst_for_each "-hRH"
168
169 */