More cleanup, 'temp' and 'uptime' use fileRead().
[sysstatus.git] / statuses / uptime.c
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
+               );
 }