dwmstatus.c (1991B)
1 #define _BSD_SOURCE 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <stdarg.h> 6 #include <string.h> 7 #include <strings.h> 8 #include <sys/time.h> 9 #include <time.h> 10 #include <sys/types.h> 11 #include <sys/wait.h> 12 13 #include <X11/Xlib.h> 14 15 char *tzargentina = "America/Buenos_Aires"; 16 char *tzutc = "UTC"; 17 char *tzberlin = "Europe/Berlin"; 18 19 static Display *dpy; 20 21 char * 22 smprintf(char *fmt, ...) 23 { 24 va_list fmtargs; 25 char *ret; 26 int len; 27 28 va_start(fmtargs, fmt); 29 len = vsnprintf(NULL, 0, fmt, fmtargs); 30 va_end(fmtargs); 31 32 ret = malloc(++len); 33 if (ret == NULL) { 34 perror("malloc"); 35 exit(1); 36 } 37 38 va_start(fmtargs, fmt); 39 vsnprintf(ret, len, fmt, fmtargs); 40 va_end(fmtargs); 41 42 return ret; 43 } 44 45 void 46 settz(char *tzname) 47 { 48 setenv("TZ", tzname, 1); 49 } 50 51 char * 52 mktimes(char *fmt, char *tzname) 53 { 54 char buf[129]; 55 time_t tim; 56 struct tm *timtm; 57 58 memset(buf, 0, sizeof(buf)); 59 settz(tzname); 60 tim = time(NULL); 61 timtm = localtime(&tim); 62 if (timtm == NULL) { 63 perror("localtime"); 64 exit(1); 65 } 66 67 if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) { 68 fprintf(stderr, "strftime == 0\n"); 69 exit(1); 70 } 71 72 return smprintf("%s", buf); 73 } 74 75 void 76 setstatus(char *str) 77 { 78 XStoreName(dpy, DefaultRootWindow(dpy), str); 79 XSync(dpy, False); 80 } 81 82 char * 83 loadavg(void) 84 { 85 double avgs[3]; 86 87 if (getloadavg(avgs, 3) < 0) { 88 perror("getloadavg"); 89 exit(1); 90 } 91 92 return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); 93 } 94 95 int 96 main(void) 97 { 98 char *status; 99 char *avgs; 100 char *tmar; 101 char *tmutc; 102 char *tmbln; 103 104 if (!(dpy = XOpenDisplay(NULL))) { 105 fprintf(stderr, "dwmstatus: cannot open display.\n"); 106 return 1; 107 } 108 109 for (;;sleep(90)) { 110 avgs = loadavg(); 111 tmar = mktimes("%H:%M", tzargentina); 112 tmutc = mktimes("%H:%M", tzutc); 113 tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin); 114 115 status = smprintf("L:%s A:%s U:%s %s", 116 avgs, tmar, tmutc, tmbln); 117 setstatus(status); 118 free(avgs); 119 free(tmar); 120 free(tmutc); 121 free(tmbln); 122 free(status); 123 } 124 125 XCloseDisplay(dpy); 126 127 return 0; 128 } 129