aboutsummaryrefslogtreecommitdiff
path: root/coreutils/chown.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-22 11:37:23 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-22 11:37:23 +0000
commit08d120e6e1242498b8e5ca92870a362bb1c64c85 (patch)
treed5289cc2ddb9bd36b27dad7b52a7e54680d8f3b4 /coreutils/chown.c
parent9e3a540b1beb135ff446731209cb35a78854ee88 (diff)
downloadbusybox-w32-08d120e6e1242498b8e5ca92870a362bb1c64c85.tar.gz
busybox-w32-08d120e6e1242498b8e5ca92870a362bb1c64c85.tar.bz2
busybox-w32-08d120e6e1242498b8e5ca92870a362bb1c64c85.zip
chown: do not use static data
function old new delta chown_main 160 182 +22 fileAction 750 744 -6 ugid 8 - -8
Diffstat (limited to 'coreutils/chown.c')
-rw-r--r--coreutils/chown.c50
1 files changed, 30 insertions, 20 deletions
diff --git a/coreutils/chown.c b/coreutils/chown.c
index b3d974ae4..345249261 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -18,11 +18,11 @@
18 18
19#define OPT_STR ("Rh" USE_DESKTOP("vcfLHP")) 19#define OPT_STR ("Rh" USE_DESKTOP("vcfLHP"))
20#define BIT_RECURSE 1 20#define BIT_RECURSE 1
21#define OPT_RECURSE (option_mask32 & 1) 21#define OPT_RECURSE (opt & 1)
22#define OPT_NODEREF (option_mask32 & 2) 22#define OPT_NODEREF (opt & 2)
23#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0)) 23#define OPT_VERBOSE (USE_DESKTOP(opt & 0x04) SKIP_DESKTOP(0))
24#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0)) 24#define OPT_CHANGED (USE_DESKTOP(opt & 0x08) SKIP_DESKTOP(0))
25#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0)) 25#define OPT_QUIET (USE_DESKTOP(opt & 0x10) SKIP_DESKTOP(0))
26/* POSIX options 26/* POSIX options
27 * -L traverse every symbolic link to a directory encountered 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 28 * -H if a command line argument is a symbolic link to a directory, traverse it
@@ -32,22 +32,27 @@
32 * The last option specified shall determine the behavior of the utility." */ 32 * The last option specified shall determine the behavior of the utility." */
33/* -L */ 33/* -L */
34#define BIT_TRAVERSE 0x20 34#define BIT_TRAVERSE 0x20
35#define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0)) 35#define OPT_TRAVERSE (USE_DESKTOP(opt & BIT_TRAVERSE) SKIP_DESKTOP(0))
36/* -H or -L */ 36/* -H or -L */
37#define BIT_TRAVERSE_TOP (0x20|0x40) 37#define BIT_TRAVERSE_TOP (0x20|0x40)
38#define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0)) 38#define OPT_TRAVERSE_TOP (USE_DESKTOP(opt & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
39 39
40typedef int (*chown_fptr)(const char *, uid_t, gid_t); 40typedef int (*chown_fptr)(const char *, uid_t, gid_t);
41 41
42static struct bb_uidgid_t ugid = { -1, -1 }; 42struct param_t {
43 struct bb_uidgid_t ugid;
44 chown_fptr chown_func;
45};
43 46
44static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, 47static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
45 void *cf, int depth UNUSED_PARAM) 48 void *vparam, int depth UNUSED_PARAM)
46{ 49{
47 uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid; 50#define param (*(struct param_t*)vparam)
48 gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid; 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;
49 54
50 if (!((chown_fptr)cf)(fileName, u, g)) { 55 if (param.chown_func(fileName, u, g) == 0) {
51 if (OPT_VERBOSE 56 if (OPT_VERBOSE
52 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g)) 57 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
53 ) { 58 ) {
@@ -59,25 +64,30 @@ static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
59 if (!OPT_QUIET) 64 if (!OPT_QUIET)
60 bb_simple_perror_msg(fileName); /* A filename can have % in it... */ 65 bb_simple_perror_msg(fileName); /* A filename can have % in it... */
61 return FALSE; 66 return FALSE;
67#undef opt
68#undef param
62} 69}
63 70
64int chown_main(int argc UNUSED_PARAM, char **argv) 71int chown_main(int argc UNUSED_PARAM, char **argv)
65{ 72{
66 int retval = EXIT_SUCCESS; 73 int retval = EXIT_SUCCESS;
67 int flags; 74 int opt, flags;
68 chown_fptr chown_func; 75 struct param_t param;
76
77 param.ugid.uid = -1;
78 param.ugid.gid = -1;
79 param.chown_func = chown;
69 80
70 opt_complementary = "-2"; 81 opt_complementary = "-2";
71 getopt32(argv, OPT_STR); 82 opt = getopt32(argv, OPT_STR);
72 argv += optind; 83 argv += optind;
73 84
74 /* This matches coreutils behavior (almost - see below) */ 85 /* This matches coreutils behavior (almost - see below) */
75 chown_func = chown;
76 if (OPT_NODEREF 86 if (OPT_NODEREF
77 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */ 87 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
78 USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE) 88 USE_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
79 ) { 89 ) {
80 chown_func = lchown; 90 param.chown_func = lchown;
81 } 91 }
82 92
83 flags = ACTION_DEPTHFIRST; /* match coreutils order */ 93 flags = ACTION_DEPTHFIRST; /* match coreutils order */
@@ -88,7 +98,7 @@ int chown_main(int argc UNUSED_PARAM, char **argv)
88 if (OPT_TRAVERSE) 98 if (OPT_TRAVERSE)
89 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */ 99 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
90 100
91 parse_chown_usergroup_or_die(&ugid, argv[0]); 101 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
92 102
93 /* Ok, ready to do the deed now */ 103 /* Ok, ready to do the deed now */
94 argv++; 104 argv++;
@@ -97,7 +107,7 @@ int chown_main(int argc UNUSED_PARAM, char **argv)
97 flags, /* flags */ 107 flags, /* flags */
98 fileAction, /* file action */ 108 fileAction, /* file action */
99 fileAction, /* dir action */ 109 fileAction, /* dir action */
100 chown_func, /* user data */ 110 &param, /* user data */
101 0) /* depth */ 111 0) /* depth */
102 ) { 112 ) {
103 retval = EXIT_FAILURE; 113 retval = EXIT_FAILURE;