1
0
Fork 0

Implement esnprintf() and make formatted calls more efficient

Within the components, snprintf() was unchecked and had inefficient
calls in some places.
We implement esnprintf() that does all the dirty laundry for us
and use it exclusively now.
This commit is contained in:
Laslo Hunhold 2018-05-19 19:33:04 +02:00 committed by Aaron Marcher
parent f31b113e7e
commit 422cadfd5f
6 changed files with 72 additions and 23 deletions

View file

@ -14,9 +14,16 @@
int perc; int perc;
char path[PATH_MAX]; char path[PATH_MAX];
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", if (esnprintf(path, sizeof(path),
bat, "/capacity"); "/sys/class/power_supply/%s/capacity",
return (pscanf(path, "%d", &perc) == 1) ? bprintf("%d", perc) : NULL; bat) < 0) {
return NULL;
}
if (pscanf(path, "%d", &perc) != 1) {
return NULL;
}
return bprintf("%d", perc);
} }
const char * const char *
@ -32,8 +39,11 @@
size_t i; size_t i;
char path[PATH_MAX], state[12]; char path[PATH_MAX], state[12];
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", if (esnprintf(path, sizeof(path),
bat, "/status"); "/sys/class/power_supply/%s/status",
bat) < 0) {
return NULL;
}
if (pscanf(path, "%12s", state) != 1) { if (pscanf(path, "%12s", state) != 1) {
return NULL; return NULL;
} }
@ -53,20 +63,29 @@
float timeleft; float timeleft;
char path[PATH_MAX], state[12]; char path[PATH_MAX], state[12];
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", if (esnprintf(path, sizeof(path),
bat, "/status"); "/sys/class/power_supply/%s/status",
bat) < 0) {
return NULL;
}
if (pscanf(path, "%12s", state) != 1) { if (pscanf(path, "%12s", state) != 1) {
return NULL; return NULL;
} }
if (!strcmp(state, "Discharging")) { if (!strcmp(state, "Discharging")) {
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", if (esnprintf(path, sizeof(path),
bat, "/charge_now"); "/sys/class/power_supply/%s/charge_now",
bat) < 0) {
return NULL;
}
if (pscanf(path, "%d", &charge_now) != 1) { if (pscanf(path, "%d", &charge_now) != 1) {
return NULL; return NULL;
} }
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", if (esnprintf(path, sizeof(path),
bat, "/current_now"); "/sys/class/power_supply/%s/current_now",
bat) < 0) {
return NULL;
}
if (pscanf(path, "%d", &current_now) != 1) { if (pscanf(path, "%d", &current_now) != 1) {
return NULL; return NULL;
} }

View file

@ -17,8 +17,11 @@
oldrxbytes = rxbytes; oldrxbytes = rxbytes;
snprintf(path, sizeof(path), if (esnprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/rx_bytes", interface); "/sys/class/net/%s/statistics/rx_bytes",
interface) < 0) {
return NULL;
}
if (pscanf(path, "%llu", &rxbytes) != 1) { if (pscanf(path, "%llu", &rxbytes) != 1) {
return NULL; return NULL;
} }
@ -37,8 +40,11 @@
oldtxbytes = txbytes; oldtxbytes = txbytes;
snprintf(path, sizeof(path), if (esnprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/tx_bytes", interface); "/sys/class/net/%s/statistics/tx_bytes",
interface) < 0) {
return NULL;
}
if (pscanf(path, "%llu", &txbytes) != 1) { if (pscanf(path, "%llu", &txbytes) != 1) {
return NULL; return NULL;
} }

View file

@ -23,8 +23,11 @@
char status[5]; char status[5];
FILE *fp; FILE *fp;
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, if (esnprintf(path, sizeof(path),
"/operstate"); "/sys/class/net/%s/operstate",
iface) < 0) {
return NULL;
}
if (!(fp = fopen(path, "r"))) { if (!(fp = fopen(path, "r"))) {
warn("fopen '%s':", path); warn("fopen '%s':", path);
return NULL; return NULL;
@ -69,7 +72,10 @@
memset(&wreq, 0, sizeof(struct iwreq)); memset(&wreq, 0, sizeof(struct iwreq));
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name),
"%s", iface) < 0) {
return NULL;
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
warn("socket 'AF_INET':"); warn("socket 'AF_INET':");

View file

@ -91,12 +91,8 @@ main(int argc, char *argv[])
if (!(res = args[i].func(args[i].args))) { if (!(res = args[i].func(args[i].args))) {
res = unknown_str; res = unknown_str;
} }
if ((ret = snprintf(status + len, sizeof(status) - len, if ((ret = esnprintf(status + len, sizeof(status) - len,
args[i].fmt, res)) < 0) { args[i].fmt, res)) < 0) {
warn("snprintf:");
break;
} else if ((size_t)ret >= sizeof(status) - len) {
warn("snprintf: Output truncated");
break; break;
} }
len += ret; len += ret;

21
util.c
View file

@ -48,6 +48,27 @@ die(const char *fmt, ...)
exit(1); exit(1);
} }
int
esnprintf(char *str, size_t size, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintf(str, size, fmt, ap);
va_end(ap);
if (ret < 0) {
warn("snprintf:");
return -1;
} else if ((size_t)ret >= size) {
warn("snprintf: Output truncated");
return -1;
}
return ret;
}
const char * const char *
bprintf(const char *fmt, ...) bprintf(const char *fmt, ...)
{ {

1
util.h
View file

@ -8,6 +8,7 @@ extern char *argv0;
void warn(const char *, ...); void warn(const char *, ...);
void die(const char *, ...); void die(const char *, ...);
int esnprintf(char *str, size_t size, const char *fmt, ...);
const char *bprintf(const char *fmt, ...); const char *bprintf(const char *fmt, ...);
const char *fmt_scaled(size_t); const char *fmt_scaled(size_t);
int pscanf(const char *path, const char *fmt, ...); int pscanf(const char *path, const char *fmt, ...);