changeset 1042:cbc467592b2e draft

Remove itoa/utoa, let libc do this with sprintf.
author Rob Landley <rob@landley.net>
date Tue, 03 Sep 2013 08:30:47 -0500
parents d3f9e55e350a
children acf7bb2b99e2
files lib/lib.c lib/lib.h toys/pending/netstat.c toys/pending/syslogd.c
diffstat 4 files changed, 11 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Tue Sep 03 08:16:42 2013 -0500
+++ b/lib/lib.c	Tue Sep 03 08:30:47 2013 -0500
@@ -177,62 +177,6 @@
   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)
--- a/lib/lib.h	Tue Sep 03 08:16:42 2013 -0500
+++ b/lib/lib.h	Tue Sep 03 08:30:47 2013 -0500
@@ -140,10 +140,6 @@
 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);
--- a/toys/pending/netstat.c	Tue Sep 03 08:16:42 2013 -0500
+++ b/toys/pending/netstat.c	Tue Sep 03 08:30:47 2013 -0500
@@ -173,7 +173,7 @@
  */
 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 @@
     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 @@
   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.
--- a/toys/pending/syslogd.c	Tue Sep 03 08:16:42 2013 -0500
+++ b/toys/pending/syslogd.c	Tue Sep 03 08:30:47 2013 -0500
@@ -84,11 +84,13 @@
 }
 
 //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 @@
 
   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;