text: #define DEFAULT_SOURCE
  #include <unistd.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdarg.h>
  #include <string.h>
  #include <strings.h>
  #include <sys/time.h>
  #include <time.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  
  #include <X11/Xlib.h>
  
  char *tzcopenhagen = "Europe/Copenhagen";
  
  static Display *dpy;
  
  char * smprintf(char *fmt, ...)
  {
  	va_list fmtargs;
  	char *ret;
  	int len;
  
  	va_start(fmtargs, fmt);
  	len = vsnprintf(NULL, 0, fmt, fmtargs);
  	va_end(fmtargs);
  
  	ret = malloc(++len);
  	if (ret == NULL) {
  		perror("malloc");
  		exit(1);
  	}
  
  	va_start(fmtargs, fmt);
  	vsnprintf(ret, len, fmt, fmtargs);
  	va_end(fmtargs);
  
  	return ret;
  }
  
  void settz(char *tzname)
  {
  	setenv("TZ", tzname, 1);
  }
  
  char * mktimes(char *fmt, char *tzname)
  {
  	char buf[129];
  	time_t tim;
  	struct tm *timtm;
  
  	settz(tzname);
  	tim = time(NULL);
  	timtm = localtime(&tim);
  	if (timtm == NULL)
  		return smprintf("");
  
  	if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
  		fprintf(stderr, "strftime == 0\n");
  		return smprintf("");
  	}
  
  	return smprintf("%s", buf);
  }
  
  void setstatus(char *str)
  {
  	XStoreName(dpy, DefaultRootWindow(dpy), str);
  	XSync(dpy, False);
  }
  
  char * loadavg(void)
  {
  	double avgs[3];
  
  	if (getloadavg(avgs, 3) < 0)
  		return smprintf("");
  
  	return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  }
  
  char * readfile(char *base, char *file)
  {
  	char *path, line[513];
  	FILE *fd;
  
  	memset(line, 0, sizeof(line));
  
  	path = smprintf("%s/%s", base, file);
  	fd = fopen(path, "r");
  	free(path);
  	if (fd == NULL)
  		return NULL;
  
  	if (fgets(line, sizeof(line)-1, fd) == NULL)
  		return NULL;
  	fclose(fd);
  
  	return smprintf("%s", line);
  }
  
  char * gettemperature(char *base, char *sensor)
  {
  	char *co;
  
  	co = readfile(base, sensor);
  	if (co == NULL)
  		return smprintf("");
  	return smprintf("%02.0f°C", atof(co) / 1000);
  }
  
  int main(void)
  {
  	char *status;
  	char *avgs;
  	char *tmcph;
  	char *t0, *t1, *t2;
  
  	if (!(dpy = XOpenDisplay(NULL))) {
  		fprintf(stderr, "dwmstatus: cannot open display.\n");
  		return 1;
  	}
  
  	for (;;sleep(60)) {
  		avgs = loadavg();
  		tmcph = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzcopenhagen);
  		t0 = gettemperature("/sys/class/hwmon/hwmon0", "temp1_input");
  		t1 = gettemperature("/sys/class/hwmon/hwmon2", "temp1_input");
  		t2 = gettemperature("/sys/class/hwmon/hwmon4", "temp1_input");
  
  		status = smprintf("T:%s|%s|%s L:%s %s",
  				t0,t1,t2,avgs,tmcph
  				);
  		setstatus(status);
  
  		free(t0);
  		free(t1);
  		free(t2);
  		free(avgs);
  		free(tmcph);
  		free(status);
  	}
  
  	XCloseDisplay(dpy);
  
  	return 0;
  }
  
  