home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / top / utils.c < prev   
C/C++ Source or Header  |  1992-02-01  |  2KB  |  122 lines

  1. /*
  2.  *  Top - a top users display for Berkeley Unix
  3.  *
  4.  *  This file contains various handy utilities used by top.
  5.  */
  6.  
  7. #include "top.h"
  8.  
  9. atoiwi(str)
  10.  
  11. char *str;
  12.  
  13. {
  14.     register int len;
  15.  
  16.     len = strlen(str);
  17.     if (len != 0)
  18.     {
  19.     if (strncmp(str, "infinity", len) == 0 ||
  20.         strncmp(str, "all",      len) == 0 ||
  21.         strncmp(str, "maximum",  len) == 0)
  22.     {
  23.         return(Infinity);
  24.     }
  25.     else if (str[0] == '-')
  26.     {
  27.         return(Invalid);
  28.     }
  29.     else
  30.     {
  31.         return(atoi(str));
  32.     }
  33.     }
  34.     return(0);
  35. }
  36.  
  37. /*
  38.  *  itoa - convert integer (decimal) to ascii string for positive numbers
  39.  *         only (we don't bother with negative numbers since we know we
  40.  *       don't use them).
  41.  */
  42.  
  43. static char buffer[16];        /* shared by the next two routines */
  44.                 /*
  45.                  * How do we know that 16 will suffice?
  46.                  * Because the biggest number that we will
  47.                  * ever convert will be 2^32-1, which is 10
  48.                  * digits.
  49.                  */
  50.  
  51. char *itoa(val)
  52.  
  53. register int val;
  54.  
  55. {
  56.     register char *ptr;
  57.  
  58.     ptr = buffer + sizeof(buffer);
  59.     *--ptr = '\0';
  60.     if (val == 0)
  61.     {
  62.     *--ptr = '0';
  63.     }
  64.     else while (val != 0)
  65.     {
  66.     *--ptr = (val % 10) + '0';
  67.     val /= 10;
  68.     }
  69.     return(ptr);
  70. }
  71.  
  72. /*
  73.  *  itoa7(val) - like itoa, except the number is right justified in a 7
  74.  *    character field.  This code is a duplication of itoa instead of
  75.  *    a front end to a more general routine for efficiency.
  76.  */
  77.  
  78. char *itoa7(val)
  79.  
  80. register int val;
  81.  
  82. {
  83.     register char *ptr;
  84.  
  85.     ptr = buffer + sizeof(buffer);
  86.     *--ptr = '\0';
  87.     if (val == 0)
  88.     {
  89.     *--ptr = '0';
  90.     }
  91.     else while (val != 0)
  92.     {
  93.     *--ptr = (val % 10) + '0';
  94.     val /= 10;
  95.     }
  96.     while (ptr > buffer + sizeof(buffer) - 7)
  97.     {
  98.     *--ptr = ' ';
  99.     }
  100.     return(ptr);
  101. }
  102.  
  103. /*
  104.  *  digits(val) - return number of decimal digits in val.  Only works for
  105.  *    positive numbers.  If val <= 0 then digits(val) == 0.
  106.  */
  107.  
  108. digits(val)
  109.  
  110. int val;
  111.  
  112. {
  113.     register int cnt = 0;
  114.  
  115.     while (val > 0)
  116.     {
  117.     cnt++;
  118.     val /= 10;
  119.     }
  120.     return(cnt);
  121. }
  122.