diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-05 09:21:57 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-05 09:21:57 +0000 |
commit | 76140a77c9c47c41f244d90d4250d77b9504f7d4 (patch) | |
tree | 67c43b25beb904f111e8a96548bee1592f5010e2 /networking/libiproute/utils.c | |
parent | 021de3f0297af87bfc098bd364dffb332d6ace20 (diff) | |
download | busybox-76140a77c9c47c41f244d90d4250d77b9504f7d4.tar.gz |
networking/libiproute/*: code shrink by optimizing numeric conversions
and other misc stuff
function old new delta
print_tunnel 660 656 -4
format_host 5 - -5
get_unsigned 70 54 -16
get_u32 70 54 -16
do_iplink 1173 1151 -22
get_prefix 417 393 -24
print_rule 800 771 -29
print_addrinfo 1374 1342 -32
print_route 1745 1709 -36
iprule_modify 905 866 -39
iproute_modify 1105 1048 -57
get_integer 70 - -70
parse_args 1684 1440 -244
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 0/11 up/down: 0/-594) Total: -594 bytes
text data bss dec hex filename
817378 476 7892 825746 c9992 busybox_old
816784 476 7892 825152 c9740 busybox_unstripped
Diffstat (limited to 'networking/libiproute/utils.c')
-rw-r--r-- | networking/libiproute/utils.c | 121 |
1 files changed, 33 insertions, 88 deletions
diff --git a/networking/libiproute/utils.c b/networking/libiproute/utils.c index 6b9f6a7f5..c84d018eb 100644 --- a/networking/libiproute/utils.c +++ b/networking/libiproute/utils.c @@ -15,102 +15,46 @@ #include "utils.h" #include "inet_common.h" -int get_integer(int *val, char *arg, int base) -{ - long res; - char *ptr; - - if (!arg || !*arg) - return -1; - res = strtol(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) - return -1; - *val = res; - return 0; -} -//XXX: FIXME: use some libbb function instead -int get_unsigned(unsigned *val, char *arg, int base) -{ - unsigned long res; - char *ptr; - - if (!arg || !*arg) - return -1; - res = strtoul(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > UINT_MAX) - return -1; - *val = res; - return 0; -} - -int get_u32(uint32_t *val, char *arg, int base) +unsigned get_unsigned(char *arg, const char *errmsg) { unsigned long res; char *ptr; - if (!arg || !*arg) - return -1; - res = strtoul(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) - return -1; - *val = res; - return 0; + if (*arg) { + res = strtoul(arg, &ptr, 0); + if (!*ptr && res <= UINT_MAX) { + return res; + } + } + invarg(arg, errmsg); /* does not return */ } -int get_u16(uint16_t *val, char *arg, int base) +uint32_t get_u32(char *arg, const char *errmsg) { unsigned long res; char *ptr; - if (!arg || !*arg) - return -1; - res = strtoul(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > 0xFFFF) - return -1; - *val = res; - return 0; + if (*arg) { + res = strtoul(arg, &ptr, 0); + if (!*ptr && res <= 0xFFFFFFFFUL) { + return res; + } + } + invarg(arg, errmsg); /* does not return */ } -int get_u8(uint8_t *val, char *arg, int base) +uint16_t get_u16(char *arg, const char *errmsg) { unsigned long res; char *ptr; - if (!arg || !*arg) - return -1; - res = strtoul(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > 0xFF) - return -1; - *val = res; - return 0; -} - -int get_s16(int16_t *val, char *arg, int base) -{ - long res; - char *ptr; - - if (!arg || !*arg) - return -1; - res = strtol(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) - return -1; - *val = res; - return 0; -} - -int get_s8(int8_t *val, char *arg, int base) -{ - long res; - char *ptr; - - if (!arg || !*arg) - return -1; - res = strtol(arg, &ptr, base); - if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) - return -1; - *val = res; - return 0; + if (*arg) { + res = strtoul(arg, &ptr, 0); + if (!*ptr && res <= 0xFFFF) { + return res; + } + } + invarg(arg, errmsg); /* does not return */ } int get_addr_1(inet_prefix *addr, char *name, int family) @@ -161,8 +105,8 @@ static int get_prefix_1(inet_prefix *dst, char *arg, int family) || strcmp(arg, "any") == 0 ) { dst->family = family; - dst->bytelen = 0; - dst->bitlen = 0; + /*dst->bytelen = 0; - done by memset */ + /*dst->bitlen = 0;*/ return 0; } @@ -176,7 +120,8 @@ static int get_prefix_1(inet_prefix *dst, char *arg, int family) inet_prefix netmask_pfx; netmask_pfx.family = AF_UNSPEC; - if ((get_unsigned(&plen, slash + 1, 0) || plen > dst->bitlen) + plen = bb_strtou(slash + 1, NULL, 0); + if ((errno || plen > dst->bitlen) && (get_addr_1(&netmask_pfx, slash + 1, family))) err = -1; else if (netmask_pfx.family == AF_INET) { @@ -262,7 +207,7 @@ int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits) { uint32_t *a1 = a->data; uint32_t *a2 = b->data; - int words = bits >> 0x05; + int words = bits >> 5; bits &= 0x1f; @@ -286,7 +231,7 @@ int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits) return 0; } -const char *rt_addr_n2a(int af, int UNUSED_PARAM len, +const char *rt_addr_n2a(int af, void *addr, char *buf, int buflen) { switch (af) { @@ -298,9 +243,9 @@ const char *rt_addr_n2a(int af, int UNUSED_PARAM len, } } +#ifdef RESOLVE_HOSTNAMES const char *format_host(int af, int len, void *addr, char *buf, int buflen) { -#ifdef RESOLVE_HOSTNAMES if (resolve_hosts) { struct hostent *h_ent; @@ -323,6 +268,6 @@ const char *format_host(int af, int len, void *addr, char *buf, int buflen) } } } -#endif - return rt_addr_n2a(af, len, addr, buf, buflen); + return rt_addr_n2a(af, addr, buf, buflen); } +#endif |