#include #include #include #include #include #include #include #include #include #include #include #include #include "../pathnames.h" enum meminfo_row { meminfo_main = 0, meminfo_swap }; enum meminfo_col { meminfo_total = 0, meminfo_used, meminfo_free, meminfo_shared, meminfo_buffers, meminfo_cached }; unsigned read_total_main(void); /* * This code is slightly modified from the procps package. */ static char buf[300]; /* This macro opens FILE only if necessary and seeks to 0 so that successive calls to the functions are more efficient. It also reads the current contents of the file into the global buf. */ #define FILE_TO_BUF(FILE) { \ static int n, fd = -1; \ if (fd == -1 && (fd = open(FILE, O_RDONLY)) == -1) { \ fprintf(stdout, "ERROR: file %s, %s\r", FILE, strerror(errno)); \ close(fd); \ return 0; \ } \ lseek(fd, 0L, SEEK_SET); \ if ((n = read(fd, buf, sizeof buf - 1)) < 0) { \ fprintf(stdout, "ERROR: file %s, %s\r", FILE, strerror(errno)); \ close(fd); \ fd = -1; \ return 0; \ } \ buf[n] = '\0'; \ } static int getuptime(double *uptime_secs) { struct sysinfo si; if (sysinfo(&si) < 0) return -1; *uptime_secs = si.uptime; return 0; } /* The following /proc/meminfo parsing routine assumes the following format: [