From: norly Date: Tue, 14 Feb 2012 01:56:17 +0000 (+0000) Subject: More cleanup, 'temp' and 'uptime' use fileRead(). X-Git-Url: https://git.enpas.org/?p=sysstatus.git;a=commitdiff_plain;h=1778a770b69425bd02750b45d9dbadc5a2ec7c78 More cleanup, 'temp' and 'uptime' use fileRead(). --- diff --git a/Makefile b/Makefile index 55db2ce..c7d3355 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CCFLAGS=-Wall -Wextra -O3 LDOUT=sysstatus LIBS=-lasound -all: *.c *.h Makefile +sysstatus: *.c *.h Makefile make -C statuses $(CC) $(CCFLAGS) -o $(LDOUT) $(LIBS) sysstatus.c statuses/*.o diff --git a/config.h b/config.h index 58495c7..53f1c58 100644 --- a/config.h +++ b/config.h @@ -6,8 +6,8 @@ //#define SHOW_SECONDS -#define CPU_HISTORY_SIZE 10 #define NUM_CPUS 2 +#define CPU_HISTORY_SIZE 10 #define NETIF_BASEDIR "/sys/class/net/" diff --git a/statuses/Makefile b/statuses/Makefile index d3f8a46..5f8162a 100644 --- a/statuses/Makefile +++ b/statuses/Makefile @@ -1,7 +1,7 @@ STATUSES=cpuusage.o datetime.o memusage.o netif.o power.o temp.o uptime.o volume_alsa.o OTHERS=tools.o -all: *.c *.h Makefile $(STATUSES) $(OTHERS) +all: *.c *.h ../config.h Makefile $(STATUSES) $(OTHERS) .c.o: $(CC) $(CCFLAGS) -c -o $@ $< diff --git a/statuses/datetime.c b/statuses/datetime.c index 7c0e479..42b37aa 100644 --- a/statuses/datetime.c +++ b/statuses/datetime.c @@ -7,30 +7,31 @@ void status_datetime() { time_t nows = 0; struct tm *nowtm; - + nows = time(NULL); - if (nows != ((time_t) -1)) + if (nows == ((time_t) -1)) { - nowtm = localtime(&nows); - - printf(" ^fg(#666666)%d.%d.%d ^fg(grey)%d:%.2d" - - #ifdef SHOW_SECONDS - ":%.2d" - #endif - " " - ,nowtm -> tm_mday, - (nowtm -> tm_mon) + 1, - (nowtm -> tm_year) + 1900, - nowtm -> tm_hour, - nowtm -> tm_min - - #ifdef SHOW_SECONDS - ,nowtm -> tm_sec - #endif - - ); - } - else printf(" ^fg(red)ERROR: DATETIME"); + return; + } + + nowtm = localtime(&nows); + + printf(" ^fg(#666666)%d.%d.%d ^fg(grey)%d:%.2d" + + #ifdef SHOW_SECONDS + ":%.2d" + #endif + " " + ,nowtm -> tm_mday, + (nowtm -> tm_mon) + 1, + (nowtm -> tm_year) + 1900, + nowtm -> tm_hour, + nowtm -> tm_min + + #ifdef SHOW_SECONDS + ,nowtm -> tm_sec + #endif + + ); } diff --git a/statuses/netif.c b/statuses/netif.c index cd131f2..865e4a7 100644 --- a/statuses/netif.c +++ b/statuses/netif.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include "netif.h" @@ -18,7 +17,7 @@ void status_netif(char *ifname) int ifpathlen; char stline[16]; - size_t stlen; + ssize_t stlen; double ifsum = 0.0; int ifsumpower; diff --git a/statuses/temp.c b/statuses/temp.c index 83e1ac9..5737e29 100644 --- a/statuses/temp.c +++ b/statuses/temp.c @@ -7,25 +7,25 @@ void status_temp(char *title, char *sysfile) { char stline[16]; int stfile; - size_t stlen; + ssize_t stlen; - stfile = open(sysfile, 0); - if (stfile != -1) + stlen = fileRead(stline, sizeof(stline), sysfile); + if (stlen <= 0) + return; + + // Read a valid value? Sometimes we get garbage from sysfs... + if (stlen < 6 || stlen > 7) { - stlen = read(stfile, stline, sizeof(stline)); - close(stfile); - if (stlen >= 6 && stlen <= 7) - { - fputs(" ^fg(#FF33FF)", stdout); - fputs(title, stdout); - fwrite(stline, 1, stlen - 4, stdout); - /* - fputs(".", stdout); - fwrite(&stline[stlen - 3], 1, 1, stdout); - */ - fputs("°C ", stdout); - } - else - printf(" ^fg(red)%sERROR ", title); + printf(" ^fg(red)%sERROR ", title); + return; } + + fputs(" ^fg(#FF33FF)", stdout); + fputs(title, stdout); + fwrite(stline, 1, stlen - 4, stdout); + /* + fputs(".", stdout); + fwrite(&stline[stlen - 3], 1, 1, stdout); + */ + fputs("°C ", stdout); } diff --git a/statuses/uptime.c b/statuses/uptime.c index 8db1c45..d512cdb 100644 --- a/statuses/uptime.c +++ b/statuses/uptime.c @@ -3,51 +3,59 @@ #include #include #include "uptime.h" +#include "../config.h" void status_uptime() { char stline[16]; int stfile; - size_t stlen; + ssize_t stlen; int i; int upts, uptm, upth, uptd; - - stfile = open("/proc/uptime", 0); - if (stfile != -1) + + fputs(" ^fg(#AAAAAA)up: ", stdout); + + stlen = fileRead(stline, sizeof(stline), "/proc/uptime"); + if (stlen < 0) + { + fputs(" ^fg(red)ERROR ", stdout); + return; + } + + // Cut first element + for(i = 0; i < stlen; i++) { - stlen = read(stfile, stline, sizeof(stline)); - close(stfile); - - for(i = 0; i < stlen; i++) - if (stline[i] == ' ') - stline[i] = '\0'; - - upts = atoi(stline); - uptd = upts / (24 * 60 * 60); - upts -= uptd * (24 * 60 * 60); - upth = upts / (60 * 60); - upts -= upth * (60 * 60); - uptm = upts / (60); - upts -= uptm * (60); - - fputs(" ^fg(#AAAAAA)up: ", stdout); - - if (uptd > 0) - printf("%dd ", uptd); - - printf("%d:%.2d" - - #ifdef SHOW_SECONDS - ":%.2d" - #endif - - " " - ,upth - ,uptm - - #ifdef SHOW_SECONDS - ,upts - #endif - ); + if (stline[i] == ' ') + { + stline[i] = '\0'; + break; + } } + + // Split time into days, hours, mins, secs + upts = atoi(stline); + uptd = upts / (24 * 60 * 60); + upts -= uptd * (24 * 60 * 60); + upth = upts / (60 * 60); + upts -= upth * (60 * 60); + uptm = upts / (60); + upts -= uptm * (60); + + if (uptd > 0) + printf("%dd ", uptd); + + printf("%d:%.2d" + + #ifdef SHOW_SECONDS + ":%.2d" + #endif + + " " + ,upth + ,uptm + + #ifdef SHOW_SECONDS + ,upts + #endif + ); }