accidentally applied wrong (old) patch, fixing up...
[oweals/busybox.git] / miscutils / nmeter.c
index e83de38ec68404aa0f16c85263477fe4472eea6f..1fa35b2935a07446ad16a5a4eba10bfef8e839b2 100644 (file)
@@ -16,7 +16,6 @@
 #include <time.h>
 
 typedef unsigned long long ullong;
-typedef unsigned long ulong;
 
 enum { proc_file_size = 4096 };
 
@@ -88,12 +87,7 @@ static void put_question_marks(int count)
 
 static int readfile_z(char *buf, int sz, const char* fname)
 {
-       int fd;
-       fd = xopen(fname, O_RDONLY);
-       // We are not checking for short reads (valid only because
-       // we are reading /proc files)
-       sz = read(fd, buf, sz-1);
-       close(fd);
+       sz = open_read_close(fname, buf, sz-1);
        if (sz < 0) {
                buf[0] = '\0';
                return 1;
@@ -110,7 +104,7 @@ static const char* get_file(proc_file *pf)
                // but allows us to allocate only once (at first sample)
                // per proc file, and reuse buffer for each sample
                if (!pf->file)
-                       pf->file = (char*)xmalloc(proc_file_size);
+                       pf->file = xmalloc(proc_file_size);
                readfile_z(pf->file, proc_file_size, pf->name);
        }
        return pf->file;
@@ -219,42 +213,8 @@ static int rdval_diskstats(const char* p, ullong *vec)
 
 static void scale(ullong ul)
 {
-       char *fmt;
        char buf[5];
-       char c;
-       unsigned v,idx = 0;
-       ul *= 10;
-       if (ul > 9999*10) { // do not scale if 9999 or less
-               while (ul >= 10000) {
-                       ul /= 1024;
-                       idx++;
-               }
-       }
-       v = ul; // ullong divisions are expensive, avoid them
-
-       fmt = " 123456789";
-       if (!idx) {             // 9999 or less: use 1234 format
-               c = buf[0] = " 123456789"[v/10000];
-               if (c!=' ') fmt = "0123456789";
-               c = buf[1] = fmt[v/1000%10];
-               if (c!=' ') fmt = "0123456789";
-               buf[2] = fmt[v/100%10];
-               buf[3] = "0123456789"[v/10%10];
-       } else {
-               if (v>=10*10) { // scaled value is >=10: use 123M format
-                       c = buf[0] = " 123456789"[v/1000];
-                       if (c!=' ') fmt = "0123456789";
-                       buf[1] = fmt[v/100%10];
-                       buf[2] = "0123456789"[v/10%10];
-               } else {        // scaled value is <10: use 1.2M format
-                       buf[0] = "0123456789"[v/10];
-                       buf[1] = '.';
-                       buf[2] = "0123456789"[v%10];
-               }
-               // see http://en.wikipedia.org/wiki/Tera
-               buf[3] = " kMGTPEZY"[idx];
-       }
-       buf[4] = '\0';
+       smart_ulltoa5(ul, buf);
        put(buf);
 }
 
@@ -634,7 +594,7 @@ static void collect_mem(mem_stat *s)
        }
 
        m_free += m_bufs + m_cached + m_slab;
-       switch(s->opt) {
+       switch (s->opt) {
        case 'f':
                scale(m_free << 10); break;
        default:
@@ -776,15 +736,12 @@ int nmeter_main(int argc, char* argv[])
        s_stat *last = NULL;
        s_stat *s;
        char *cur, *prev;
-       int fd;
 
        if (argc != 2)
                bb_show_usage();
 
-       fd = xopen("/proc/version", O_RDONLY);
-       if (read(fd, buf, sizeof(buf)) > 0)
-               is26 = (strstr(buf, "Linux version 2.4.")==NULL);
-       close(fd);
+       if (open_read_close("/proc/version", buf, sizeof(buf)) > 0)
+               is26 = (strstr(buf, " 2.4.")==NULL);
 
        // Can use argv[1] directly, but this will mess up
        // parameters as seen by e.g. ps. Making a copy...