From c6218dbae579de0cd20f5a7f1e9877673e28225d Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Wed, 20 Dec 2000 22:52:58 +0000 Subject: A number of updates from Manuel Novoa III. Things look good... --- libc/inet/addr.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'libc/inet') diff --git a/libc/inet/addr.c b/libc/inet/addr.c index 142363ccc..a05a4288d 100644 --- a/libc/inet/addr.c +++ b/libc/inet/addr.c @@ -3,6 +3,15 @@ * under the GNU Library General Public License. */ +/* + * Manuel Novoa III Dec 2000 + * + * Converted to use my new (un)signed long (long) to string routines, which + * are smaller than the previous functions and don't require static buffers. + * In the process, removed the reference to strcat and cut object size of + * inet_ntoa in half (from 190 bytes down to 94). + */ + #include #include #include @@ -63,22 +72,35 @@ const char *cp; #ifdef L_inet_ntoa -extern char *itoa(int); +#include + +#if (ULONG_MAX >> 32) +/* We're set up for 32 bit unsigned longs */ +#error need to check size allocation for static buffer 'buf' +#endif + +extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase); char *inet_ntoa(in) struct in_addr in; { - static char buf[18]; - unsigned long addr = ntohl(in.s_addr); + static char buf[16]; /* max 12 digits + 3 '.'s + 1 nul */ - strcpy(buf, itoa((addr >> 24) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa((addr >> 16) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa((addr >> 8) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa(addr & 0xff)); + unsigned long addr = ntohl(in.s_addr); + int i; + char *p, *q; + + q = 0; + p = buf + sizeof(buf) - 1; + for (i=0 ; i < 4 ; i++ ) { + p = __ultostr(p, addr & 0xff, 10, 0 ) - 1; + addr >>= 8; + if (q) { + *q = '.'; + } + q = p; + } - return buf; + return p+1; } #endif -- cgit v1.2.3