More cleanup, 'temp' and 'uptime' use fileRead().
authornorly <ny-git@enpas.org>
Tue, 14 Feb 2012 01:56:17 +0000 (01:56 +0000)
committernorly <ny-git@enpas.org>
Tue, 14 Feb 2012 01:57:03 +0000 (01:57 +0000)
Makefile
config.h
statuses/Makefile
statuses/datetime.c
statuses/netif.c
statuses/temp.c
statuses/uptime.c

index 55db2ce1d3a63cb01d376999c9dc1551069fa5e0..c7d33559067d9c68c6162740df62793e6de0c8f6 100644 (file)
--- 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
 
index 58495c77514dc76059c7040df23c70608417efa7..53f1c580337f2767062f8be11ba9861371475779 100644 (file)
--- 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/"
 
index d3f8a46f5aa697093304b6d497d3f201879f10f1..5f8162a08b771cb89478a8b1d5125161d329facf 100644 (file)
@@ -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 $@ $<
index 7c0e47968e7b2797f492896505747e4a69769d0e..42b37aafb389006661ba4b5d4986db3b594f3f9e 100644 (file)
@@ -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
+
+               );
 }
index cd131f237544e89d50e615d2cea13b6971697db4..865e4a7a22962d7c5b471cd92f8dc370264e07d1 100644 (file)
@@ -1,6 +1,5 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
 #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;
index 83e1ac9171c93119dcd242bc9b4f9057c819651d..5737e29c8a1e8a110a18f89cdcefc54d49a8c425 100644 (file)
@@ -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);
 }
index 8db1c4550d8ea1455200ab1f15b3bc4acf1de420..d512cdb84512f143a5fc2a3e0a4d91385e5e54c4 100644 (file)
@@ -3,51 +3,59 @@
 #include <unistd.h>
 #include <stdlib.h>
 #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
+               );
 }