sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

uptime.c (2446B)


      1 #define _BSD_SOURCE
      2 #define _GNU_SOURCE
      3 #include <unistd.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <stdarg.h>
      7 #include <string.h>
      8 #include <strings.h>
      9 #include <sys/time.h>
     10 #include <sys/sysinfo.h>
     11 #include <time.h>
     12 #include <sys/types.h>
     13 #include <dirent.h>
     14 #include <sys/statvfs.h>
     15 
     16 
     17 #include <X11/Xlib.h>
     18 
     19 char *tzparis = "Europe/Paris";
     20 
     21 static Display *dpy;
     22 
     23 char *
     24 smprintf(char *fmt, ...)
     25 {
     26 	va_list fmtargs;
     27 	char *buf = NULL;
     28 
     29 	va_start(fmtargs, fmt);
     30 	if (vasprintf(&buf, fmt, fmtargs) == -1){
     31 		fprintf(stderr, "malloc vasprintf\n");
     32 		exit(1);
     33     }
     34 	va_end(fmtargs);
     35 
     36 	return buf;
     37 }
     38 
     39 void
     40 settz(char *tzname)
     41 {
     42 	setenv("TZ", tzname, 1);
     43 }
     44 
     45 char *
     46 mktimes(char *fmt, char *tzname)
     47 {
     48 	char buf[129];
     49 	time_t tim;
     50 	struct tm *timtm;
     51 
     52 	memset(buf, 0, sizeof(buf));
     53 	settz(tzname);
     54 	tim = time(NULL);
     55 	timtm = localtime(&tim);
     56 	if (timtm == NULL) {
     57 		perror("localtime");
     58 		exit(1);
     59 	}
     60 
     61 	if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
     62 		fprintf(stderr, "strftime == 0\n");
     63 		exit(1);
     64 	}
     65 
     66 	return smprintf(buf);
     67 }
     68 
     69 void
     70 setstatus(char *str)
     71 {
     72 	XStoreName(dpy, DefaultRootWindow(dpy), str);
     73 	XSync(dpy, False);
     74 }
     75 
     76 char *
     77 loadavg(void)
     78 {
     79 	double avgs[3];
     80 
     81 	if (getloadavg(avgs, 3) < 0) {
     82 		perror("getloadavg");
     83 		exit(1);
     84 	}
     85 
     86 	return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
     87 }
     88 
     89 
     90 char *
     91 up() {
     92     struct sysinfo info;
     93     int h,m = 0;
     94     sysinfo(&info);
     95     h = info.uptime/3600;
     96     m = (info.uptime - h*3600 )/60;
     97     return smprintf("%dh%dm",h,m);
     98 }
     99 
    100 
    101 int runevery(time_t *ltime, int sec){
    102     /* return 1 if sec elapsed since last run
    103      * else return 0 
    104     */
    105     time_t now = time(NULL);
    106     
    107     if ( difftime(now, *ltime ) >= sec)
    108     {
    109         *ltime = now;
    110         return(1);
    111     }
    112     else 
    113         return(0);
    114 }
    115 
    116 int
    117 main(void)
    118 {
    119 	char *status = NULL;
    120 	char *tmprs = NULL;
    121 	char *avgs = NULL;
    122     time_t count60 = 0;
    123     char *uptm = NULL;
    124     
    125 	if (!(dpy = XOpenDisplay(NULL))) {
    126 		fprintf(stderr, "dwmstatus: cannot open display.\n");
    127 		return 1;
    128 	}
    129 
    130 	for (;;sleep(1)) {
    131 	    /* checks every minutes */
    132 	    if ( runevery(&count60, 60) )
    133         {
    134             free(tmprs);
    135             free(uptm);
    136             tmprs = mktimes("%d/%m/%y %H:%M", tzparis);
    137             uptm = up();
    138         }
    139         /* checks every second */
    140 		avgs = loadavg();
    141 
    142 		status = smprintf("%s | Up:%s | %s",
    143 				 avgs, uptm, tmprs);
    144 		setstatus(status);
    145 		free(avgs);
    146 		free(status);
    147 	}
    148 
    149 	XCloseDisplay(dpy);
    150 
    151 	return 0;
    152 }
    153