Commits

Thomas Dahms committed 0456cd0

even more static

Comments (0)

Files changed (1)

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