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

  1. /* Added uptime().  J. Cowley, 19 Mar 1993.
  2.  *
  3.  * $Log: sysinfo.c,v $
  4.  * Revision 1.3  1994/01/29  17:49:27  johnsonm
  5.  * Fixed comment problem stupidly introduced with the last revision...
  6.  *
  7.  * Revision 1.2  1994/01/29  17:42:22  johnsonm
  8.  * includes sysinfo.h so we know about any changes.
  9.  *
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "sysinfo.h"
  19.  
  20. #define LOAD_FILE "/proc/loadavg"
  21.  
  22. void loadavg(double *av1, double *av5, double *av15)
  23. {
  24.     int n;
  25.     char buff[80];
  26.     static int fd = -1;
  27.  
  28.     if (fd < 0) {
  29.     if ((fd = open(LOAD_FILE, O_RDONLY)) < 0) {
  30.         perror(LOAD_FILE);
  31.         exit(1);
  32.     }
  33.     }
  34.  
  35.     lseek(fd, 0L, SEEK_SET);
  36.     n = read(fd, buff, sizeof buff - 1);
  37.     if (n < 0) {
  38.     perror(LOAD_FILE);
  39.     exit(1);
  40.     }
  41.     buff[n] = '\0';
  42.  
  43.     if (sscanf(buff, "%lf %lf %lf", av1, av5, av15) < 3) {
  44.     fprintf(stderr, "bad data in " LOAD_FILE "\n");
  45.     exit(1);
  46.     }
  47.  
  48.     return;
  49. }
  50.  
  51. #define MEM_FILE "/proc/meminfo"
  52.  
  53. void meminfo(unsigned *total, unsigned *used, unsigned *free,
  54.          unsigned *shared, unsigned *buffers)
  55. {
  56.     int n;
  57.     char *cp, buff[1024];
  58.     static int fd = -1;
  59.  
  60.     if (fd < 0) {
  61.     if ((fd = open(MEM_FILE, O_RDONLY)) < 0) {
  62.         perror(MEM_FILE);
  63.         exit(1);
  64.     }
  65.     }
  66.  
  67.     lseek(fd, 0L, SEEK_SET);
  68.     n = read(fd, buff, sizeof buff - 1);
  69.     if (n < 0) {
  70.     perror(MEM_FILE);
  71.     exit(1);
  72.     }
  73.     buff[n] = '\0';
  74.  
  75.     /* skip over the first line */
  76.     cp = strchr(buff, '\n');
  77.     if (cp)
  78.     cp = strchr(cp, ' ');
  79.  
  80.     if (!cp || sscanf(cp, "%u %u %u %u %u", total, used,
  81.               free, shared, buffers) < 5) {
  82.     fprintf(stderr, "bad data in " MEM_FILE "\n");
  83.     exit(1);
  84.     }
  85.  
  86.     return;
  87. }
  88.  
  89. #define UPTIME_FILE "/proc/uptime"
  90.  
  91. void uptime(double *uptime_secs, double *idle_secs)
  92. {
  93.     int n;
  94.     char buff[80];
  95.     static int fd = -1;
  96.  
  97.     if (fd < 0) {
  98.     if ((fd = open(UPTIME_FILE, O_RDONLY)) < 0) {
  99.         perror(UPTIME_FILE);
  100.         exit(1);
  101.     }
  102.     }
  103.  
  104.     lseek(fd, 0L, SEEK_SET);
  105.     n = read(fd, buff, sizeof buff - 1);
  106.     if (n < 0) {
  107.     perror(UPTIME_FILE);
  108.     exit(1);
  109.     }
  110.     buff[n] = '\0';
  111.  
  112.     if (sscanf(buff, "%lf %lf", uptime_secs, idle_secs) < 2) {
  113.     fprintf(stderr, "bad data in " UPTIME_FILE "\n");
  114.     exit(1);
  115.     }
  116.  
  117.     return;
  118. }
  119.  
  120.