*: optimize code size in strtoul calls

function                                             old     new   delta
bb_parse_mode                                        433     431      -2
rtnl_rtntype_a2n                                     202     198      -4
ParseField                                           511     498     -13
bb_init_module_24                                   4730    4675     -55
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-74)             Total: -74 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-09-23 17:17:53 +02:00
parent 8d338173a4
commit 1f27ab0d4b
8 changed files with 37 additions and 23 deletions

View File

@ -40,7 +40,7 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode)
mode_t new_mode; mode_t new_mode;
char op; char op;
if (((unsigned int)(*s - '0')) < 8) { if ((unsigned char)(*s - '0') < 8) {
unsigned long tmp; unsigned long tmp;
char *e; char *e;

View File

@ -816,7 +816,7 @@ static int bb__parsepwent(void *data, char *line)
i = 0; i = 0;
do { do {
p = ((char *) ((struct passwd *) data)) + pw_off[i]; p = (char *) data + pw_off[i];
if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
*((char **) p) = line; *((char **) p) = line;
@ -873,7 +873,7 @@ static int bb__parsegrent(void *data, char *line)
end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */ end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
i = 0; i = 0;
do { do {
p = ((char *) ((struct group *) data)) + gr_off[i]; p = (char *) data + gr_off[i];
if (i < 2) { if (i < 2) {
*((char **) p) = line; *((char **) p) = line;
@ -966,15 +966,15 @@ static const unsigned char sp_off[] ALIGN1 = {
offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
}; };
static int bb__parsespent(void *data, char * line) static int bb__parsespent(void *data, char *line)
{ {
char *endptr; char *endptr;
char *p; char *p;
int i; int i;
i = 0; i = 0;
do { while (1) {
p = ((char *) ((struct spwd *) data)) + sp_off[i]; p = (char *) data + sp_off[i];
if (i < 2) { if (i < 2) {
*((char **) p) = line; *((char **) p) = line;
line = strchr(line, ':'); line = strchr(line, ':');
@ -982,10 +982,10 @@ static int bb__parsespent(void *data, char * line)
break; break;
} }
} else { } else {
*((long *) p) = (long) strtoul(line, &endptr, 10); *((long *) p) = strtoul(line, &endptr, 10);
if (endptr == line) { if (endptr == line) {
*((long *) p) = ((i != 8) ? -1L : ((long)(~0UL))); *((long *) p) = (i != 8) ? -1L : (long)(~0UL);
} }
line = endptr; line = endptr;
@ -1003,9 +1003,9 @@ static int bb__parsespent(void *data, char * line)
} }
*line++ = 0; *line++ = '\0';
++i; ++i;
} while (1); }
return EINVAL; return EINVAL;
} }

View File

@ -320,11 +320,13 @@ static void ParseField(char *user, char *ary, int modvalue, int off,
skip = 1; skip = 1;
++ptr; ++ptr;
} else if (isdigit(*ptr)) { } else if (isdigit(*ptr)) {
char *endp;
if (n1 < 0) { if (n1 < 0) {
n1 = strtol(ptr, &ptr, 10) + off; n1 = strtol(ptr, &endp, 10) + off;
} else { } else {
n2 = strtol(ptr, &ptr, 10) + off; n2 = strtol(ptr, &endp, 10) + off;
} }
ptr = endp; /* gcc likes temp var for &endp */
skip = 1; skip = 1;
} else if (names) { } else if (names) {
int i; int i;
@ -361,7 +363,9 @@ static void ParseField(char *user, char *ary, int modvalue, int off,
n2 = n1; n2 = n1;
} }
if (*ptr == '/') { if (*ptr == '/') {
skip = strtol(ptr + 1, &ptr, 10); char *endp;
skip = strtol(ptr + 1, &endp, 10);
ptr = endp; /* gcc likes temp var for &endp */
} }
/* /*

View File

@ -2432,11 +2432,11 @@ new_process_module_arguments(struct obj_file *f, const char *options)
loc = contents + sym->value; loc = contents + sym->value;
if (*pinfo == 'c') { if (*pinfo == 'c') {
if (!isdigit(*(pinfo + 1))) { if (!isdigit(pinfo[1])) {
bb_error_msg_and_die("parameter type 'c' for %s must be followed by" bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
" the maximum size", param); " the maximum size", param);
} }
charssize = strtoul(pinfo + 1, (char **) NULL, 10); charssize = strtoul(pinfo + 1, NULL, 10);
} }
if (val == NULL) { if (val == NULL) {
@ -2449,6 +2449,8 @@ new_process_module_arguments(struct obj_file *f, const char *options)
n = 0; n = 0;
p = val; p = val;
while (*p != 0) { while (*p != 0) {
char *endp;
if (++n > max) if (++n > max)
bb_error_msg_and_die("too many values for %s (max %d)", param, max); bb_error_msg_and_die("too many values for %s (max %d)", param, max);
@ -2472,19 +2474,23 @@ new_process_module_arguments(struct obj_file *f, const char *options)
p += len; p += len;
break; break;
case 'b': case 'b':
*loc++ = strtoul(p, &p, 0); *loc++ = strtoul(p, &endp, 0);
p = endp; /* gcc likes temp var for &endp */
break; break;
case 'h': case 'h':
*(short *) loc = strtoul(p, &p, 0); *(short *) loc = strtoul(p, &endp, 0);
loc += tgt_sizeof_short; loc += tgt_sizeof_short;
p = endp;
break; break;
case 'i': case 'i':
*(int *) loc = strtoul(p, &p, 0); *(int *) loc = strtoul(p, &endp, 0);
loc += tgt_sizeof_int; loc += tgt_sizeof_int;
p = endp;
break; break;
case 'l': case 'l':
*(long *) loc = strtoul(p, &p, 0); *(long *) loc = strtoul(p, &endp, 0);
loc += tgt_sizeof_long; loc += tgt_sizeof_long;
p = endp;
break; break;
default: default:
bb_error_msg_and_die("unknown parameter type '%c' for %s", bb_error_msg_and_die("unknown parameter type '%c' for %s",

View File

@ -88,7 +88,7 @@ int rtnl_rtntype_a2n(int *id, char *arg)
res = RTN_THROW; res = RTN_THROW;
else { else {
res = strtoul(arg, &end, 0); res = strtoul(arg, &end, 0);
if (!end || end == arg || *end || res > 255) if (end == arg || *end || res > 255)
return -1; return -1;
} }
*id = res; *id = res;

View File

@ -22,6 +22,7 @@ unsigned get_unsigned(char *arg, const char *errmsg)
if (*arg) { if (*arg) {
res = strtoul(arg, &ptr, 0); res = strtoul(arg, &ptr, 0);
//FIXME: "" will be accepted too, is it correct?!
if (!*ptr && res <= UINT_MAX) { if (!*ptr && res <= UINT_MAX) {
return res; return res;
} }
@ -36,6 +37,7 @@ uint32_t get_u32(char *arg, const char *errmsg)
if (*arg) { if (*arg) {
res = strtoul(arg, &ptr, 0); res = strtoul(arg, &ptr, 0);
//FIXME: "" will be accepted too, is it correct?!
if (!*ptr && res <= 0xFFFFFFFFUL) { if (!*ptr && res <= 0xFFFFFFFFUL) {
return res; return res;
} }
@ -50,6 +52,7 @@ uint16_t get_u16(char *arg, const char *errmsg)
if (*arg) { if (*arg) {
res = strtoul(arg, &ptr, 0); res = strtoul(arg, &ptr, 0);
//FIXME: "" will be accepted too, is it correct?!
if (!*ptr && res <= 0xFFFF) { if (!*ptr && res <= 0xFFFF) {
return res; return res;
} }

View File

@ -89,7 +89,7 @@ static int get_qdisc_handle(__u32 *h, const char *str) {
if (p == str) if (p == str)
return 1; return 1;
maj <<= 16; maj <<= 16;
if (*p != ':' && *p!=0) if (*p != ':' && *p != '\0')
return 1; return 1;
ok: ok:
*h = maj; *h = maj;
@ -119,7 +119,8 @@ static int get_tc_classid(__u32 *h, const char *str) {
maj <<= 16; maj <<= 16;
str = p + 1; str = p + 1;
min = strtoul(str, &p, 16); min = strtoul(str, &p, 16);
if (*p != 0 || min >= (1<<16)) //FIXME: check for "" too?
if (*p != '\0' || min >= (1<<16))
return 1; return 1;
maj |= min; maj |= min;
} else if (*p != 0) } else if (*p != 0)

View File

@ -10078,7 +10078,7 @@ change_random(const char *value)
vrandom.flags &= ~VNOFUNC; vrandom.flags &= ~VNOFUNC;
} else { } else {
/* set/reset */ /* set/reset */
random_galois_LFSR = random_LCG = strtoul(value, (char **)NULL, 10); random_galois_LFSR = random_LCG = strtoul(value, NULL, 10);
} }
} }
#endif #endif