Commits

Thomas Dahms committed ddf6ba1

everything a bit more static

  • Participants
  • Parent commits a4ebd2b

Comments (0)

Files changed (1)

 #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>
 
 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;
-}
-
-char *
-mktimes(char *fmt)
+void
+mktimes(char *str, char *fmt)
 {
 	char buf[129];
 	time_t tim;
 		exit(1);
 	}
 
-	return smprintf("%s", buf);
+	sprintf(str, "%s", buf);
 }
 
 void
 	XSync(dpy, False);
 }
 
-char *
-loadavg(void)
+void
+loadavg(char *str)
 {
 	double avgs[3];
 
 		exit(1);
 	}
 
-	return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
+	sprintf(str, "%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
 }
 
-char *
-readfile(char *base, char *file)
+void
+readfile(char *str, char *base, char *file)
 {
-	char *path, line[513];
+	char *path = malloc(strlen(base) + strlen(file) + 1);
+	char line[513];
 	FILE *fd;
 
 	memset(line, 0, sizeof(line));
 
-	path = smprintf("%s/%s", base, file);
+	sprintf(path, "%s/%s", base, file);
 	fd = fopen(path, "r");
-	if (fd == NULL)
-		return NULL;
+	if (fd == NULL) {
+		free(path);
+		str = "";
+		return;
+	}
 	free(path);
 
-	if (fgets(line, sizeof(line)-1, fd) == NULL)
-		return NULL;
+	if (fgets(line, sizeof(line)-1, fd) == NULL) {
+		str = "";
+		return;
+	}
 	fclose(fd);
 
-	return smprintf("%s", line);
+	sprintf(str, "%s", line);
 }
 
 /*
  * Linux seems to change the filenames after suspend/hibernate
  * according to a random scheme. So just check for both possibilities.
  */
-char *
-getbattery(char *base)
+void
+getbattery(char *bttry, char *base)
 {
-	char *co;
+	char *co = malloc(256);
 	int descap, remcap;
 
 	descap = -1;
 	remcap = -1;
 
-	co = readfile(base, "present");
+	readfile(co, base, "present");
 	if (co[0] != '1') {
 		free(co);
-		return smprintf("not present");
+		sprintf(bttry, "not present");
+		return;
 	}
-	free(co);
 
-	co = readfile(base, "charge_full_design");
-	if (co == NULL) {
-		co = readfile(base, "energy_full_design");
-		if (co == NULL)
-			return smprintf("");
+	readfile(co, base, "charge_full_design");
+	if (strlen(co) == 0) {
+		readfile(co, base, "energy_full_design");
+		if (strlen(co) == 0) {
+			free(co);
+			bttry = "";
+			return;
+		}
 	}
 	sscanf(co, "%d", &descap);
-	free(co);
 
-	co = readfile(base, "charge_now");
-	if (co == NULL) {
-		co = readfile(base, "energy_now");
-		if (co == NULL)
-			return smprintf("");
+	readfile(co, base, "charge_now");
+	if (strlen(co) == 0) {
+		readfile(co, base, "energy_now");
+		if (strlen(co) == 0) {
+			free(co);
+			bttry = "";
+			return;
+		}
 	}
 	sscanf(co, "%d", &remcap);
 	free(co);
 
-	if (remcap < 0 || descap < 0)
-		return smprintf("invalid");
+	if (remcap < 0 || descap < 0) {
+		sprintf(bttry, "invalid");
+		return;
+	}
 
-	return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
+	sprintf(bttry, "%.0f", ((float)remcap / (float)descap) * 100);
 }
 
 int
 main(void)
 {
-	char *status;
-	char *avgs;
-	char *tmbln;
-	char *bttry;
+	char *status = malloc(3*256);
+	char *avgs = malloc(256);
+	char *tmbln = malloc(256);
+	char *bttry = malloc(256);
 
 	if (!(dpy = XOpenDisplay(NULL))) {
 		fprintf(stderr, "dwmstatus: cannot open display.\n");
 	}
 
 	for (;;sleep(1)) {
-		avgs = loadavg();
-		tmbln = mktimes("%a %d %b %T");
-		bttry = getbattery("/sys/class/power_supply/BAT0/");
-		status = smprintf("battery %s%%, load %s, %s", bttry, avgs, tmbln);
+		loadavg(avgs);
+		mktimes(tmbln, "%a %d %b %T");
+		getbattery(bttry, "/sys/class/power_supply/BAT0");
+		sprintf(status, "battery %s%%, load %s, %s", bttry, avgs, tmbln);
 		setstatus(status);
-		free(avgs);
-		free(tmbln);
-		free(status);
 	}
 
+	free(avgs);
+	free(tmbln);
+	free(bttry);
+	free(status);
 	XCloseDisplay(dpy);
 
 	return 0;
 }
-