avoid using strok - eliminates use of hidden global variable

function                                             old     new   delta
udhcp_str2optset                                     616     650     +34
setpriv_main                                         950     975     +25
switch_root_main                                     688     706     +18
parse                                                958     970     +12
getopt_main                                          622     628      +6
parse_resolvconf                                     302     306      +4
mpstat_main                                         1139    1142      +3
static.p                                               4       -      -4
cdcmd                                                717     702     -15
strtok                                               148       -    -148
------------------------------------------------------------------------------
(add/remove: 0/3 grow/shrink: 7/1 up/down: 102/-167)          Total: -65 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-10-06 02:36:47 +02:00
parent 535a509846
commit 2496616b0a
11 changed files with 46 additions and 36 deletions

View File

@ -19,15 +19,17 @@ int FAST_FUNC get_linux_version_code(void)
{ {
struct utsname name; struct utsname name;
char *t; char *t;
int i, r; int r;
uname(&name); /* never fails */ uname(&name); /* never fails */
t = name.release; t = name.release - 1;
r = 0; r = 1;
for (i = 0; i < 3; i++) { do {
t = strtok(t, "."); r <<= 8;
r = r * 256 + (t ? atoi(t) : 0); if (t) {
t = NULL; r += atoi(++t);
} t = strchr(t, '.');
return r; }
} while (r < 0x1000000);
return r - 0x1000000;
} }

View File

@ -18,17 +18,20 @@
#if ENABLE_SELINUX #if ENABLE_SELINUX
static void check_selinux_update_passwd(const char *username) static void check_selinux_update_passwd(const char *username)
{ {
security_context_t context; security_context_t seuser;
char *seuser; char *p;
if (getuid() != (uid_t)0 || is_selinux_enabled() == 0) if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
return; /* No need to check */ return; /* No need to check */
if (getprevcon_raw(&context) < 0) if (getprevcon_raw(&seuser) < 0)
bb_simple_perror_msg_and_die("getprevcon failed"); bb_simple_perror_msg_and_die("getprevcon failed");
seuser = strtok(context, ":");
if (!seuser) p = strchr(seuser, ':');
bb_error_msg_and_die("invalid context '%s'", context); if (!p)
bb_error_msg_and_die("invalid context '%s'", seuser);
*p = '\0';
if (strcmp(seuser, username) != 0) { if (strcmp(seuser, username) != 0) {
security_class_t tclass; security_class_t tclass;
access_vector_t av; access_vector_t av;

View File

@ -115,6 +115,7 @@ static int parse(const char *boundary, char **argv)
/* Split to tokens */ /* Split to tokens */
{ {
char *s, *p; char *s, *p;
char *tokstate;
unsigned ntokens; unsigned ntokens;
const char *delims = ";=\" \t\n"; const char *delims = ";=\" \t\n";
@ -127,13 +128,13 @@ static int parse(const char *boundary, char **argv)
} }
dbg_error_msg("L:'%s'", p); dbg_error_msg("L:'%s'", p);
ntokens = 0; ntokens = 0;
s = strtok(s, delims); s = strtok_r(s, delims, &tokstate);
while (s) { while (s) {
tokens[ntokens] = s; tokens[ntokens] = s;
if (ntokens < ARRAY_SIZE(tokens) - 1) if (ntokens < ARRAY_SIZE(tokens) - 1)
ntokens++; ntokens++;
dbg_error_msg("L[%d]='%s'", ntokens, s); dbg_error_msg("L[%d]='%s'", ntokens, s);
s = strtok(NULL, delims); s = strtok_r(NULL, delims, &tokstate);
} }
tokens[ntokens] = NULL; tokens[ntokens] = NULL;
dbg_error_msg("EMPTYLINE, ntokens:%d", ntokens); dbg_error_msg("EMPTYLINE, ntokens:%d", ntokens);

View File

@ -703,12 +703,13 @@ static void parse_resolvconf(void)
while (fgets(line, sizeof(line), resolv)) { while (fgets(line, sizeof(line), resolv)) {
char *p, *arg; char *p, *arg;
char *tokstate;
p = strtok(line, " \t\n"); p = strtok_r(line, " \t\n", &tokstate);
if (!p) if (!p)
continue; continue;
dbg("resolv_key:'%s'\n", p); dbg("resolv_key:'%s'\n", p);
arg = strtok(NULL, "\n"); arg = strtok_r(NULL, "\n", &tokstate);
dbg("resolv_arg:'%s'\n", arg); dbg("resolv_arg:'%s'\n", arg);
if (!arg) if (!arg)
continue; continue;

View File

@ -526,7 +526,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
/* Cheat, the only *const* str possible is "" */ /* Cheat, the only *const* str possible is "" */
str = (char *) const_str; str = (char *) const_str;
opt = strtok(str, " \t=:"); opt = strtok_r(str, " \t=:", &str);
if (!opt) if (!opt)
return 0; return 0;
@ -550,10 +550,10 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
char *val; char *val;
if (optflag->flags == OPTION_BIN) { if (optflag->flags == OPTION_BIN) {
val = strtok(NULL, ""); /* do not split "'q w e'" */ val = strtok_r(NULL, "", &str); /* do not split "'q w e'" */
if (val) trim(val); if (val) trim(val);
} else } else
val = strtok(NULL, ", \t"); val = strtok_r(NULL, ", \t", &str);
if (!val) if (!val)
break; break;
@ -567,7 +567,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
break; break;
case OPTION_IP_PAIR: case OPTION_IP_PAIR:
retval = udhcp_str2nip(val, buffer); retval = udhcp_str2nip(val, buffer);
val = strtok(NULL, ", \t/-"); val = strtok_r(NULL, ", \t/-", &str);
if (!val) if (!val)
retval = 0; retval = 0;
if (retval) if (retval)
@ -631,7 +631,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
*slash = '\0'; *slash = '\0';
retval = udhcp_str2nip(val, buffer + 1); retval = udhcp_str2nip(val, buffer + 1);
buffer[0] = mask = bb_strtou(slash + 1, NULL, 10); buffer[0] = mask = bb_strtou(slash + 1, NULL, 10);
val = strtok(NULL, ", \t/-"); val = strtok_r(NULL, ", \t/-", &str);
if (!val || mask > 32 || errno) if (!val || mask > 32 || errno)
retval = 0; retval = 0;
if (retval) { if (retval) {

View File

@ -923,7 +923,7 @@ int mpstat_main(int argc UNUSED_PARAM, char **argv)
char *t; char *t;
G.p_option = 1; G.p_option = 1;
for (t = strtok(opt_set_cpu, ","); t; t = strtok(NULL, ",")) { for (t = strtok_r(opt_set_cpu, ",", &opt_set_cpu); t; t = strtok_r(NULL, ",", &opt_set_cpu)) {
if (strcmp(t, "ALL") == 0) { if (strcmp(t, "ALL") == 0) {
/* Select every CPU */ /* Select every CPU */
memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len); memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);

View File

@ -2770,7 +2770,7 @@ updatepwd(const char *dir)
lim++; lim++;
} }
} }
p = strtok(cdcomppath, "/"); p = strtok_r(cdcomppath, "/", &cdcomppath);
while (p) { while (p) {
switch (*p) { switch (*p) {
case '.': case '.':
@ -2789,7 +2789,7 @@ updatepwd(const char *dir)
new = stack_putstr(p, new); new = stack_putstr(p, new);
USTPUTC('/', new); USTPUTC('/', new);
} }
p = strtok(NULL, "/"); p = strtok_r(NULL, "/", &cdcomppath);
} }
if (new > lim) if (new > lim)
STUNPUTC(new); STUNPUTC(new);

View File

@ -289,12 +289,13 @@ static struct option *add_long_options(struct option *long_options, char *option
{ {
int long_nr = 0; int long_nr = 0;
int arg_opt, tlen; int arg_opt, tlen;
char *tokptr = strtok(options, ", \t\n"); char *tokptr;
if (long_options) if (long_options)
while (long_options[long_nr].name) while (long_options[long_nr].name)
long_nr++; long_nr++;
tokptr = strtok_r(options, ", \t\n", &options);
while (tokptr) { while (tokptr) {
arg_opt = no_argument; arg_opt = no_argument;
tlen = strlen(tokptr); tlen = strlen(tokptr);
@ -318,7 +319,7 @@ static struct option *add_long_options(struct option *long_options, char *option
long_nr++; long_nr++;
/*memset(&long_options[long_nr], 0, sizeof(long_options[0])); - xrealloc_vector did it */ /*memset(&long_options[long_nr], 0, sizeof(long_options[0])); - xrealloc_vector did it */
} }
tokptr = strtok(NULL, ", \t\n"); tokptr = strtok_r(NULL, ", \t\n", &options);
} }
return long_options; return long_options;
} }

View File

@ -1230,6 +1230,7 @@ static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *fi
* then data pointer is interpreted as a string. */ * then data pointer is interpreted as a string. */
struct nfs_mount_data data; struct nfs_mount_data data;
char *opt; char *opt;
char *tokstate;
struct hostent *hp; struct hostent *hp;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
struct sockaddr_in mount_server_addr; struct sockaddr_in mount_server_addr;
@ -1348,7 +1349,7 @@ static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *fi
nfsvers = 0; nfsvers = 0;
/* parse options */ /* parse options */
if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) { if (filteropts) for (opt = strtok_r(filteropts, ",", &tokstate); opt; opt = strtok_r(NULL, ",", &tokstate)) {
char *opteq = strchr(opt, '='); char *opteq = strchr(opt, '=');
if (opteq) { if (opteq) {
int val, idx; int val, idx;

View File

@ -144,10 +144,11 @@ static unsigned parse_cap(const char *cap)
static void set_inh_caps(char *capstring) static void set_inh_caps(char *capstring)
{ {
struct caps caps; struct caps caps;
char *string;
getcaps(&caps); getcaps(&caps);
capstring = strtok(capstring, ","); capstring = strtok_r(capstring, ",", &string);
while (capstring) { while (capstring) {
unsigned cap; unsigned cap;
@ -159,7 +160,7 @@ static void set_inh_caps(char *capstring)
caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap); caps.data[CAP_TO_INDEX(cap)].inheritable |= CAP_TO_MASK(cap);
else else
caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap); caps.data[CAP_TO_INDEX(cap)].inheritable &= ~CAP_TO_MASK(cap);
capstring = strtok(NULL, ","); capstring = strtok_r(NULL, ",", &string);
} }
if (capset(&caps.header, caps.data) != 0) if (capset(&caps.header, caps.data) != 0)
@ -170,7 +171,7 @@ static void set_ambient_caps(char *string)
{ {
char *cap; char *cap;
cap = strtok(string, ","); cap = strtok_r(string, ",", &string);
while (cap) { while (cap) {
unsigned idx; unsigned idx;
@ -182,7 +183,7 @@ static void set_ambient_caps(char *string)
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0) if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_LOWER, idx, 0, 0) < 0)
bb_simple_perror_msg("cap_ambient_lower"); bb_simple_perror_msg("cap_ambient_lower");
} }
cap = strtok(NULL, ","); cap = strtok_r(NULL, ",", &string);
} }
} }
#endif /* FEATURE_SETPRIV_CAPABILITIES */ #endif /* FEATURE_SETPRIV_CAPABILITIES */

View File

@ -164,7 +164,7 @@ static void drop_capabilities(char *string)
{ {
char *cap; char *cap;
cap = strtok(string, ","); cap = strtok_r(string, ",", &string);
while (cap) { while (cap) {
unsigned cap_idx; unsigned cap_idx;
@ -174,7 +174,7 @@ static void drop_capabilities(char *string)
drop_bounding_set(cap_idx); drop_bounding_set(cap_idx);
drop_capset(cap_idx); drop_capset(cap_idx);
bb_error_msg("dropped capability: %s", cap); bb_error_msg("dropped capability: %s", cap);
cap = strtok(NULL, ","); cap = strtok_r(NULL, ",", &string);
} }
} }
#endif #endif