changeset 1040:7dfad5f21984

Switch ls from utoa to sprintf.
author Rob Landley <rob@landley.net>
date Mon, 02 Sep 2013 22:20:29 -0500
parents 75f5e63d79c3
children d3f9e55e350a
files toys/posix/ls.c
diffstat 1 files changed, 17 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/ls.c	Mon Sep 02 18:48:59 2013 -0500
+++ b/toys/posix/ls.c	Mon Sep 02 22:20:29 2013 -0500
@@ -58,9 +58,7 @@
 
   unsigned screen_width;
   int nl_title;
-
-  // group and user can make overlapping use of the utoa() buf, so move it
-  char uid_buf[12];
+  char uid_buf[12], gid_buf[12];
 )
 
 void dlist_to_dirtree(struct dirtree *parent)
@@ -92,14 +90,17 @@
 static char *getusername(uid_t uid)
 {
   struct passwd *pw = getpwuid(uid);
-  utoa_to_buf(uid, TT.uid_buf, 12);
+
+  sprintf(TT.uid_buf, "%u", (unsigned)uid);
   return pw ? pw->pw_name : TT.uid_buf;
 }
 
 static char *getgroupname(gid_t gid)
 {
   struct group *gr = getgrgid(gid);
-  return gr ? gr->gr_name : utoa(gid);
+
+  sprintf(TT.gid_buf, "%u", (unsigned)gid);
+  return gr ? gr->gr_name : TT.gid_buf;
 }
 
 // Figure out size of printable entry fields for display indent/wrap
@@ -117,8 +118,10 @@
   if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
     unsigned fn = flags & FLAG_n;
     len[2] = numlen(st->st_nlink);
-    len[3] = strlen(fn ? utoa(st->st_uid) : getusername(st->st_uid));
-    len[4] = strlen(fn ? utoa(st->st_gid) : getgroupname(st->st_gid));
+    len[3] = fn ? snprintf(0, 0, "%u", (unsigned)st->st_uid)
+                : strlen(getusername(st->st_uid));
+    len[4] = fn ? snprintf(0, 0, "%u", (unsigned)st->st_gid)
+                : strlen(getgroupname(st->st_gid));
     if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
       // cheating slightly here: assuming minor is always 3 digits to avoid
       // tracking another column
@@ -356,22 +359,18 @@
 
       mode_to_string(mode, perm);
 
-      tm = localtime(&(st->st_mtime));
-      strftime(thyme, sizeof(thyme), "%F %H:%M", tm);
-
       if (flags&FLAG_o) grp = grpad = toybuf+256;
       else {
-        grp = (flags&FLAG_n) ? utoa(st->st_gid) : getgroupname(st->st_gid);
+        sprintf(thyme, "%u", (unsigned)st->st_gid);
+        grp = (flags&FLAG_n) ? thyme : getgroupname(st->st_gid);
         grpad = toybuf+256-(totals[4]-len[4]);
       }
 
       if (flags&FLAG_g) usr = upad = toybuf+256;
       else {
         upad = toybuf+255-(totals[3]-len[3]);
-        if (flags&FLAG_n) {
-          usr = TT.uid_buf;
-          utoa_to_buf(st->st_uid, TT.uid_buf, 12);
-        } else usr = getusername(st->st_uid);
+        if (flags&FLAG_n) sprintf(usr = TT.uid_buf, "%u", (unsigned)st->st_uid);
+        else usr = getusername(st->st_uid);
       }
 
       // Coerce the st types into something we know we can print.
@@ -381,6 +380,9 @@
       if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
         printf("% *d,% 4d", totals[5]-4, major(st->st_rdev),minor(st->st_rdev));
       else printf("% *"PRId64, totals[5]+1, (int64_t)st->st_size);
+
+      tm = localtime(&(st->st_mtime));
+      strftime(thyme, sizeof(thyme), "%F %H:%M", tm);
       xprintf(" %s ", thyme);
     }