From 978019d34f71c08393d44f66a1ab71ccc5008ff1 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 8 Sep 2011 02:54:02 +0000 Subject: [PATCH] sanity checks --- LoadAverageMeter.c | 13 ++++++++----- UptimeMeter.c | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 6c19e2d2..7646ad65 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -20,12 +20,15 @@ int LoadMeter_attributes[] = { LOAD }; static inline void LoadAverageMeter_scan(double* one, double* five, double* fifteen) { int activeProcs, totalProcs, lastProc; + *one = 0; *five = 0; *fifteen = 0; FILE *fd = fopen(PROCDIR "/loadavg", "r"); - int total = fscanf(fd, "%lf %lf %lf %d/%d %d", one, five, fifteen, - &activeProcs, &totalProcs, &lastProc); - (void) total; - assert(total == 6); - fclose(fd); + if (fd) { + int total = fscanf(fd, "%lf %lf %lf %d/%d %d", one, five, fifteen, + &activeProcs, &totalProcs, &lastProc); + (void) total; + assert(total == 6); + fclose(fd); + } } static void LoadAverageMeter_setValues(Meter* this, char* buffer, int size) { diff --git a/UptimeMeter.c b/UptimeMeter.c index cec6c040..d676850d 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -21,8 +21,10 @@ int UptimeMeter_attributes[] = { static void UptimeMeter_setValues(Meter* this, char* buffer, int len) { double uptime = 0; FILE* fd = fopen(PROCDIR "/uptime", "r"); - fscanf(fd, "%lf", &uptime); - fclose(fd); + if (fd) { + fscanf(fd, "%lf", &uptime); + fclose(fd); + } int totalseconds = (int) ceil(uptime); int seconds = totalseconds % 60; int minutes = (totalseconds/60) % 60;