From f538f420deffc242742ce2d0661a39fa9a3b5399 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 3 Sep 2013 08:30:47 -0500 Subject: [PATCH] Remove itoa/utoa, let libc do this with sprintf. --- lib/lib.c | 56 -------------------------------------------------- lib/lib.h | 4 ---- toys/pending/netstat.c | 6 +++--- toys/pending/syslogd.c | 12 +++++++---- 4 files changed, 11 insertions(+), 67 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 5867092..86e99ac 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -177,62 +177,6 @@ struct string_list *find_in_path(char *path, char *filename) return rlist; } -// Convert unsigned int to ascii, writing into supplied buffer. A truncated -// result contains the first few digits of the result ala strncpy, and is -// always null terminated (unless buflen is 0). -void utoa_to_buf(unsigned n, char *buf, unsigned buflen) -{ - int i, out = 0; - - if (buflen) { - for (i=1000000000; i; i/=10) { - int res = n/i; - - if ((res || out || i == 1) && --buflen>0) { - out++; - n -= res*i; - *buf++ = '0' + res; - } - } - *buf = 0; - } -} - -// Convert signed integer to ascii, using utoa_to_buf() -void itoa_to_buf(int n, char *buf, unsigned buflen) -{ - if (buflen && n<0) { - n = -n; - *buf++ = '-'; - buflen--; - } - utoa_to_buf((unsigned)n, buf, buflen); -} - -// This static buffer is used by both utoa() and itoa(), calling either one a -// second time will overwrite the previous results. -// -// The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. -// Note that int is always 32 bits on any remotely unix-like system, see -// http://www.unix.org/whitepapers/64bit.html for details. - -static char itoa_buf[12]; - -// Convert unsigned integer to ascii, returning a static buffer. -char *utoa(unsigned n) -{ - utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); - - return itoa_buf; -} - -char *itoa(int n) -{ - itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); - - return itoa_buf; -} - // atol() with the kilo/mega/giga/tera/peta/exa extensions. // (zetta and yotta don't fit in 64 bits.) long atolx(char *numstr) diff --git a/lib/lib.h b/lib/lib.h index 2294a5b..14e14e9 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -140,10 +140,6 @@ void msleep(long miliseconds); int64_t peek(void *ptr, int size); void poke(void *ptr, uint64_t val, int size); struct string_list *find_in_path(char *path, char *filename); -void utoa_to_buf(unsigned n, char *buf, unsigned buflen); -void itoa_to_buf(int n, char *buf, unsigned buflen); -char *utoa(unsigned n); -char *itoa(int n); long atolx(char *c); int numlen(long l); int stridx(char *haystack, char needle); diff --git a/toys/pending/netstat.c b/toys/pending/netstat.c index cbe245e..88e5bd3 100644 --- a/toys/pending/netstat.c +++ b/toys/pending/netstat.c @@ -173,7 +173,7 @@ static const char *get_pid_name(unsigned long inode) */ static void display_data(unsigned rport, char *label, unsigned rxq, unsigned txq, char *lip, char *rip, unsigned state, unsigned long inode) { - char *ss_state = "UNKNOWN"; + char *ss_state = "UNKNOWN", buf[12]; char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1", "FIN_WAIT2", "TIME_WAIT", "CLOSE", "CLOSE_WAIT", "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"}; if (!strcmp(label, "tcp")) { @@ -185,7 +185,7 @@ static void display_data(unsigned rport, char *label, unsigned rxq, unsigned txq if (state == 1) ss_state = state_label[state]; else if (state == 7) ss_state = ""; } - else if (!strcmp(label, "raw")) ss_state = itoa(state); + else if (!strcmp(label, "raw")) sprintf(ss_state = buf, "%u", state); if ( (toys.optflags & FLAG_W) && (toys.optflags & FLAG_p)) xprintf("%3s %6d %6d %-51s %-51s %-12s%s\n", label, rxq, txq, lip, rip, ss_state, get_pid_name(inode)); @@ -215,7 +215,7 @@ static char *get_servname(int port, char *label) if (!lport) return xmsprintf("%s", "*"); struct servent *ser = getservbyport(lport, label); if (ser) return xmsprintf("%s", ser->s_name); - return xmsprintf("%s", itoa(ntohs(lport))); + return xmsprintf("%u", (unsigned)ntohs(lport)); } /* * used to convert address into text format. diff --git a/toys/pending/syslogd.c b/toys/pending/syslogd.c index 3138919..c2cc34a 100644 --- a/toys/pending/syslogd.c +++ b/toys/pending/syslogd.c @@ -84,11 +84,13 @@ int logger_lookup(int where, char *key) } //search the given name and return its value -static char *dec(int val, CODE *clist) +static char *dec(int val, CODE *clist, char *buf) { for (; clist->c_name; clist++) if (val == clist->c_val) return clist->c_name; - return itoa(val); + sprintf(buf, "%u", val); + + return buf; } /* @@ -340,8 +342,10 @@ static void logmsg(char *msg, int len) if (toys.optflags & FLAG_K) len = sprintf(toybuf, "<%d> %s\n", pri, msg); else { - facstr = dec(pri & LOG_FACMASK, facilitynames); - lvlstr = dec(LOG_PRI(pri), prioritynames); + char facbuf[12], pribuf[12]; + + facstr = dec(pri & LOG_FACMASK, facilitynames, facbuf); + lvlstr = dec(LOG_PRI(pri), prioritynames, pribuf); p = "local"; if (!uname(&uts)) p = uts.nodename; -- 2.7.4