Make smart_ulltoa return pointer to end (allows for code shink in callers)
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 6 Sep 2013 10:53:14 +0000 (12:53 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 6 Sep 2013 10:53:14 +0000 (12:53 +0200)
function                                             old     new   delta
smart_ulltoa5                                        405     408      +3
smart_ulltoa4                                        273     276      +3
list_table                                          1113    1114      +1
scale                                                 36      34      -2
put_lu                                                55      53      -2
ulltoa6_and_space                                     19      14      -5
powertop_main                                       1470    1461      -9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/human_readable.c
procps/nmeter.c
procps/powertop.c
procps/ps.c
procps/top.c
util-linux/fdisk_gpt.c

index 6b95dc0ecd54498d916e2b05c6778e8fe216c940..1315e5f8fc63c8e36b720b2568705e7c0aa24604 100644 (file)
@@ -815,8 +815,8 @@ char *itoa(int n) FAST_FUNC;
 char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) FAST_FUNC;
 char *itoa_to_buf(int n, char *buf, unsigned buflen) FAST_FUNC;
 /* Intelligent formatters of bignums */
-void smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale) FAST_FUNC;
-void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
+char *smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale) FAST_FUNC;
+char *smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
 /* If block_size == 0, display size without fractional part,
  * else display (size * block_size) with one decimal digit.
  * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
index 8b22b0cb55c5cc954ecf095ba1a4146548b8fca8..0b2eb777e2375c7f939ddcbba5eab7f589fc76a1 100644 (file)
@@ -94,7 +94,7 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long val,
 
 /* Convert unsigned long long value into compact 5-char representation.
  * String is not terminated (buf[5] is untouched) */
-void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
+char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
 {
        const char *fmt;
        char c;
@@ -145,12 +145,13 @@ void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *sca
                buf[3] = "0123456789"[v];
                buf[4] = scale[idx]; /* typically scale = " kmgt..." */
        }
+       return buf + 5;
 }
 
 /* Convert unsigned long long value into compact 4-char
  * representation. Examples: "1234", "1.2k", " 27M", "123T"
  * String is not terminated (buf[4] is untouched) */
-void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
+char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
 {
        const char *fmt;
        char c;
@@ -194,4 +195,5 @@ void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *sca
                buf[2] = "0123456789"[v];
                buf[3] = scale[idx]; /* typically scale = " kmgt..." */
        }
+       return buf + 4;
 }
index 6a3b3274382feee6d67b8e0dc7ba07816f1c6fe5..5d5b83b8d5078bb586c56b2918b9dc1361bed744 100644 (file)
@@ -333,8 +333,7 @@ static void scale(ullong ul)
        char buf[5];
 
        /* see http://en.wikipedia.org/wiki/Tera */
-       smart_ulltoa4(ul, buf, " kmgtpezy");
-       buf[4] = '\0';
+       smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0';
        put(buf);
 }
 
index 71988a29557255898ac09479b9467e9c4afad675..e3c29d1c3ebde70aacb68b426039accdbf163626 100644 (file)
@@ -627,7 +627,6 @@ static void show_timerstats(void)
                int i, n = 0;
                char strbuf6[6];
 
-               strbuf6[5] = '\0';
                puts("\nTop causes for wakeups:");
                for (i = 0; i < G.lines_cnt; i++) {
                        if ((G.lines[i].count > 0 /*|| G.lines[i].disk_count > 0*/)
@@ -639,7 +638,7 @@ static void show_timerstats(void)
                                /*char c = ' ';
                                if (G.lines[i].disk_count)
                                        c = 'D';*/
-                               smart_ulltoa5(G.lines[i].count, strbuf6, " KMGTPEZY");
+                               smart_ulltoa5(G.lines[i].count, strbuf6, " KMGTPEZY")[0] = '\0';
                                printf(/*" %5.1f%% (%s)%c  %s\n"*/
                                        " %5.1f%% (%s)   %s\n",
                                        G.lines[i].count * 100.0 / G.lines_cumulative_count,
index 89cadad007d18f7f1645d72e1b9f2664974e2ab5..c65fa012a2f2c0f5721d221c89f011ad8b39d928 100644 (file)
@@ -299,8 +299,7 @@ static void put_lu(char *buf, int size, unsigned long u)
        char buf4[5];
 
        /* see http://en.wikipedia.org/wiki/Tera */
-       smart_ulltoa4(u, buf4, " mgtpezy");
-       buf4[4] = '\0';
+       smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
        sprintf(buf, "%.*s", size, buf4);
 }
 
@@ -740,8 +739,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #endif
                {
                        char buf6[6];
-                       smart_ulltoa5(p->vsz, buf6, " mgtpezy");
-                       buf6[5] = '\0';
+                       smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
 #if ENABLE_FEATURE_PS_LONG
                        if (opts & OPT_l) {
                                char bufr[6], stime_str[6];
@@ -752,8 +750,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
                                time_t start = now - elapsed;
                                struct tm *tm = localtime(&start);
 
-                               smart_ulltoa5(p->rss, bufr, " mgtpezy");
-                               bufr[5] = '\0';
+                               smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
 
                                if (p->tty_major == 136)
                                        /* It should be pts/N, not ptsN, but N > 9
index abee69806e7501465058fd479e42ed4ed2fc0543..a13a41cd8c1f7e7caafe5653c6f3671b6797f8cb 100644 (file)
@@ -847,8 +847,7 @@ static void display_topmem_header(int scr_width, int *lines_rem_p)
 static void ulltoa6_and_space(unsigned long long ul, char buf[6])
 {
        /* see http://en.wikipedia.org/wiki/Tera */
-       smart_ulltoa5(ul, buf, " mgtpezy");
-       buf[5] = ' ';
+       smart_ulltoa5(ul, buf, " mgtpezy")[0] = '\0';
 }
 
 static NOINLINE void display_topmem_process_list(int lines_rem, int scr_width)
index f45f68af0396e2e6fec18b4098b9499ebb697000..5786d5f7d753d6e1e414e84a58cca306dcf5b5aa 100644 (file)
@@ -93,9 +93,7 @@ gpt_list_table(int xtra UNUSED_PARAM)
        int i;
        char numstr6[6];
 
-       numstr6[5] = '\0';
-
-       smart_ulltoa5(total_number_of_sectors * sector_size, numstr6, " KMGTPEZY");
+       smart_ulltoa5(total_number_of_sectors * sector_size, numstr6, " KMGTPEZY")[0] = '\0';
        printf("Disk %s: %llu sectors, %s\n", disk_device,
                (unsigned long long)total_number_of_sectors,
                numstr6);
@@ -113,7 +111,7 @@ gpt_list_table(int xtra UNUSED_PARAM)
                gpt_partition *p = gpt_part(i);
                if (p->lba_start) {
                        smart_ulltoa5((1 + SWAP_LE64(p->lba_end) - SWAP_LE64(p->lba_start)) * sector_size,
-                               numstr6, " KMGTPEZY");
+                               numstr6, " KMGTPEZY")[0] = '\0';
                        printf("%4u %15llu %15llu %11s   %04x  ",
                                i + 1,
                                (unsigned long long)SWAP_LE64(p->lba_start),