home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / PS / PROCPS-0.000 / PROCPS-0 / procps-0.97 / free.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-25  |  2.8 KB  |  94 lines

  1. /* free.c - a /proc implementation of free */
  2. /* Dec14/92 by Brian Edmonds */
  3. /* Thanks to Rafal Maszkowski for the Total line */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <strings.h>
  8. #include <getopt.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. /* set this to whatever you want by default */
  13. int bytedivisor = 1024;
  14. int total = 0;
  15.  
  16. int main( int argc, char **argv ) {
  17.     char buf1[80], buf2[80];
  18.     char *titles[5], name[32];
  19.     int i, n = 0, col[5], rtime = 0, old_fmt = 0, first_line;
  20.  
  21.     static int sum[5]; /* statics get initialized to zero */
  22.  
  23.     /* check startup flags */
  24.     while( (i = getopt( argc, argv, "bkmos:t") ) != -1 )
  25.         switch (i) {
  26.         case 'b': bytedivisor = 1; break;
  27.         case 'k': bytedivisor = 1024; break;
  28.         case 'm': bytedivisor = 1024*1024; break;
  29.         case 'o': old_fmt = 1; break;
  30.         case 's': rtime = 1000000 * atof(optarg); break;
  31.         case 't': total = 1; break;
  32.         default:
  33.             fprintf( stderr, "usage: %s [-b|-k|-m] [-o] [-s] [-t]\n", argv[0] );
  34.             return -1;
  35.     }
  36.  
  37.     /* redirect stdin to /proc/meminfo */
  38.     close( 0 );
  39.     if( open( "/proc/meminfo", O_RDONLY ) < 0 ) {
  40.         perror( "open" );
  41.         return -1;
  42.     }
  43.  
  44.     do {
  45.     for(i=0; i<5; i++)
  46.         sum[i]=0;
  47.     first_line = 1;
  48.         /* get the column titles */
  49.         fseek(stdin, 0L, SEEK_SET);
  50.         fgets( buf1, 80, stdin );
  51.         for( i=0 ; i<5 ; i++ ) {
  52.             titles[i] = strtok( ( i ? NULL : buf1 ), " \t:" );
  53.             if( ! titles[i] ) {
  54.                 fprintf( stderr, "free: error reading /proc/meminfo\n" );
  55.                 return -1;
  56.             }
  57.         }
  58.         fprintf( stdout, "%-7s %10s %10s %10s %10s %10s\n",
  59.                  "", titles[0], titles[1], titles[2], titles[3], titles[4] );
  60.  
  61.         /* read and translate data lines */
  62.         while( fgets( buf2, 80, stdin ) ) {
  63.             n = sscanf( buf2, "%s %d %d %d %d %d", name,
  64.                         &col[0], &col[1], &col[2], &col[3], &col[4] );
  65.             if( n < 1 ) 
  66.                 continue;
  67.             fprintf( stdout, "%-7s", name );
  68.             for( i=1 ; i<n ; i++ ) {
  69.                 fprintf( stdout, " %10d", col[i-1]/bytedivisor );
  70.                 sum[i-1] += col[i-1];
  71.             }
  72.             fprintf( stdout, "\n" );
  73.             if (first_line && !old_fmt) {
  74.                 first_line = 0;
  75.                 fprintf( stdout, "-/+ buffers: %16d %10d\n",
  76.                          (col[1] - col[4])/bytedivisor,
  77.                          (col[2] + col[4])/bytedivisor );
  78.             }
  79.         }
  80.         if (total == 1) {
  81.             fprintf( stdout, "Total: ");
  82.             for( i=1 ; i<n ; i++ )
  83.                 fprintf( stdout, " %10d", sum[i-1]/bytedivisor );
  84.             fprintf( stdout, "\n" );
  85.         }
  86.         if (rtime) {
  87.             fprintf( stdout, "\n\n" );
  88.         usleep(rtime);
  89.     }
  90.     } while (rtime);
  91.  
  92.     return 0;
  93. }
  94.