1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 char *
 ss_bytes(hsize_t nbytes,
          char *buf              /* Optional buffer to hold the results. If the user supplies the buffer then it should be
                                  * large enough to hold the result. On a 64-bit machine that would be at least 62 bytes. If
                                  * the caller passes the null pointer then one of six static buffers will be used (don't
                                  * make more than six calls to this function in a single printf() argument list). */
          )
 {
     SS_ENTER(ss_bytes, charP);
     static char buffers[6][64];
     static int ncalls=0;
     char tmp[32], join[2];
     size_t ng = nbytes >> 30;
     size_t nm = (nbytes >> 20) & 0x3ff;
     size_t nk = (nbytes >> 10) & 0x3ff;
     size_t nb = nbytes & 0x3ff;

     if (!buf)
         buf = buffers[ncalls++ % 4];

     /* The decimal part with commas */
     sprintf(tmp, "%llu", (unsigned long_long)nbytes);
     if (NULL==ss_insert_commas(tmp)) SS_ERROR(FAILED);
     strcpy(buf, tmp);

     /* The parenthesised part */
     join[0] = join[1] = '\0';
     if (nbytes>=1024) {
         strcat(buf, " (");
         if (ng) {
             sprintf(tmp, "%lu", (unsigned long)ng);
             if (NULL==ss_insert_commas(tmp)) SS_ERROR(FAILED);
             sprintf(buf+strlen(buf), "%sG", tmp);
             *join = '+';
         }
         if (nm) {
             sprintf(buf+strlen(buf), "%s%luM", join, (unsigned long)nm);
             *join = '+';
         }
         if (nk)
             sprintf(buf+strlen(buf), "%s%luk", join, (unsigned long)nk);
         if (nb)
             sprintf(buf+strlen(buf), "+%lu", (unsigned long)nb);
         strcat(buf, ")");
     }
 SS_CLEANUP:
     SS_LEAVE(buf);
 }