summaryrefslogtreecommitdiffstats
path: root/rose
diff options
context:
space:
mode:
authorRalf Baechle <ralf@linux-mips.org>2017-01-30 16:38:49 +0100
committerRalf Baechle <ralf@linux-mips.org>2017-02-06 23:43:46 +0100
commitd975e83dee7fdcd68e5ef4c2c10c24ec486d3328 (patch)
treefd072e78a47ce31cbde343ceeba9aed4a67b137a /rose
parent3c24792336bb197485156e7e0ff78fcda3e338b9 (diff)
rsmemsiz: Rewrite the last instance of FILE_TO_BUF().
Expand the one remaining invocation to FILE_TO_BUF(), then fix the return type and attempt to close fd -1 and format nicely. Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'rose')
-rw-r--r--rose/rsmemsiz.c53
1 files changed, 25 insertions, 28 deletions
diff --git a/rose/rsmemsiz.c b/rose/rsmemsiz.c
index 5e306db..882912a 100644
--- a/rose/rsmemsiz.c
+++ b/rose/rsmemsiz.c
@@ -22,33 +22,6 @@ enum meminfo_col { meminfo_total = 0, meminfo_used, meminfo_free,
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;
@@ -81,10 +54,34 @@ static unsigned **meminfo(void)
{
static unsigned *row[MAX_ROW + 1]; /* row pointers */
static unsigned num[MAX_ROW * MAX_COL]; /* number storage */
+ static int n, fd = -1;
+ char buf[300];
char *p;
int i, j, k, l;
- FILE_TO_BUF(PROC_MEMINFO_FILE)
+ /*
+ * Open FILE only if necessary and seek 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.
+ */
+ if (fd == -1 && (fd = open(PROC_MEMINFO_FILE, O_RDONLY)) == -1) {
+ fprintf(stdout, "ERROR: file %s, %s\r",
+ PROC_MEMINFO_FILE, strerror(errno));
+
+ return NULL;
+ }
+ lseek(fd, 0L, SEEK_SET);
+ n = read(fd, buf, sizeof buf - 1);
+ if (n < 0) {
+ fprintf(stdout, "ERROR: file %s, %s\r",
+ PROC_MEMINFO_FILE, strerror(errno));
+ close(fd);
+ fd = -1;
+
+ return NULL;
+ }
+ buf[n] = '\0';
+
if (!row[0]) /* init ptrs 1st time through */
for (i=0; i < MAX_ROW; i++) /* std column major order: */
row[i] = num + MAX_COL*i; /* A[i][j] = A + COLS*i + j */