Add pscanf()
This reduces a lot of equivalent code to one function call.
This commit is contained in:
parent
796b661284
commit
e1e1b1d79a
1 changed files with 66 additions and 157 deletions
223
slstatus.c
223
slstatus.c
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
@ -93,52 +94,50 @@ bprintf(const char *fmt, ...)
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pscanf(const char *path, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
va_list ap;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if (!(fp = fopen(path, "r"))) {
|
||||||
|
warn("fopen %s: %s\n", path, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
va_start(ap, fmt);
|
||||||
|
n = vfscanf(fp, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return (n == EOF) ? -1 : n;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
battery_perc(const char *bat)
|
battery_perc(const char *bat)
|
||||||
{
|
{
|
||||||
int n, perc;
|
int perc;
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
|
||||||
fp = fopen(path, "r");
|
return (pscanf(path, "%i", &perc) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%d", perc) : unknown_str;
|
||||||
warn("Failed to open file %s", path);
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "%i", &perc);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%d", perc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
battery_power(const char *bat)
|
battery_power(const char *bat)
|
||||||
{
|
{
|
||||||
|
int watts;
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
FILE *fp;
|
|
||||||
int n, watts;
|
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
|
||||||
fp = fopen(path, "r");
|
return (pscanf(path, "%i", &watts) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%d", (watts + 500000) / 1000000) : unknown_str;
|
||||||
warn("Failed to open file %s", path);
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "%i", &watts);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%d", (watts + 500000) / 1000000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
battery_state(const char *bat)
|
battery_state(const char *bat)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
|
||||||
struct {
|
struct {
|
||||||
char *state;
|
char *state;
|
||||||
char *symbol;
|
char *symbol;
|
||||||
|
@ -149,81 +148,55 @@ battery_state(const char *bat)
|
||||||
{ "Unknown", "/" },
|
{ "Unknown", "/" },
|
||||||
};
|
};
|
||||||
size_t i;
|
size_t i;
|
||||||
int n;
|
|
||||||
char path[PATH_MAX], state[12];
|
char path[PATH_MAX], state[12];
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
|
||||||
fp = fopen(path, "r");
|
if (pscanf(path, "%12s", state) != 1) {
|
||||||
if (fp == NULL) {
|
|
||||||
warn("Failed to open file %s", path);
|
|
||||||
return unknown_str;
|
return unknown_str;
|
||||||
}
|
}
|
||||||
n = fscanf(fp, "%12s", state);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
for (i = 0; i < LEN(map); i++) {
|
for (i = 0; i < LEN(map); i++) {
|
||||||
if (!strcmp(map[i].state, state)) {
|
if (!strcmp(map[i].state, state)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (i == LEN(map)) ? "?" : map[i].symbol;
|
return (i == LEN(map)) ? "?" : map[i].symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
cpu_freq(void)
|
cpu_freq(void)
|
||||||
{
|
{
|
||||||
int n, freq;
|
int freq;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
|
return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
|
||||||
if (fp == NULL) {
|
"%i", &freq) != 1) ?
|
||||||
warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
|
bprintf("%d", (freq + 500) / 1000) : unknown_str;
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "%i", &freq);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%d", (freq + 500) / 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
cpu_perc(void)
|
cpu_perc(void)
|
||||||
{
|
{
|
||||||
struct timespec delay;
|
struct timespec delay;
|
||||||
int n, perc;
|
int perc;
|
||||||
long double a[4], b[4];
|
long double a[4], b[4];
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen("/proc/stat", "r");
|
if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
|
||||||
if (fp == NULL) {
|
&a[3]) != 4) {
|
||||||
warn("Failed to open file /proc/stat");
|
|
||||||
return unknown_str;
|
return unknown_str;
|
||||||
}
|
}
|
||||||
n = fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 4)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
delay.tv_sec = (interval / 2) / 1000;
|
delay.tv_sec = (interval / 2) / 1000;
|
||||||
delay.tv_nsec = ((interval / 2) % 1000) * 1000000;
|
delay.tv_nsec = ((interval / 2) % 1000) * 1000000;
|
||||||
nanosleep(&delay, NULL);
|
nanosleep(&delay, NULL);
|
||||||
|
|
||||||
fp = fopen("/proc/stat", "r");
|
if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2],
|
||||||
if (fp == NULL) {
|
&b[3]) != 4) {
|
||||||
warn("Failed to open file /proc/stat");
|
|
||||||
return unknown_str;
|
return unknown_str;
|
||||||
}
|
}
|
||||||
n = fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 4)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
|
perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) /
|
||||||
|
((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
|
||||||
|
|
||||||
return bprintf("%d", perc);
|
return bprintf("%d", perc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,20 +270,10 @@ disk_used(const char *mnt)
|
||||||
static const char *
|
static const char *
|
||||||
entropy(void)
|
entropy(void)
|
||||||
{
|
{
|
||||||
int n, num;
|
int num;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp= fopen("/proc/sys/kernel/random/entropy_avail", "r");
|
return (pscanf("/proc/sys/kernel/random/entropy_avail", "%d", &num) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%d", num) : unknown_str;
|
||||||
warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "%d", &num);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%d", num);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
@ -438,92 +401,48 @@ static const char *
|
||||||
ram_free(void)
|
ram_free(void)
|
||||||
{
|
{
|
||||||
long free;
|
long free;
|
||||||
FILE *fp;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
fp = fopen("/proc/meminfo", "r");
|
return (pscanf("/proc/meminfo", "MemFree: %ld kB\n", &free) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%f", (float)free / 1024 / 1024) : unknown_str;
|
||||||
warn("Failed to open file /proc/meminfo");
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "MemFree: %ld kB\n", &free);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%f", (float)free / 1024 / 1024);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
ram_perc(void)
|
ram_perc(void)
|
||||||
{
|
{
|
||||||
long total, free, buffers, cached;
|
long total, free, buffers, cached;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen("/proc/meminfo", "r");
|
return (pscanf("/proc/meminfo",
|
||||||
if (fp == NULL) {
|
"MemTotal: %ld kB\n"
|
||||||
warn("Failed to open file /proc/meminfo");
|
"MemFree: %ld kB\n"
|
||||||
return unknown_str;
|
"MemAvailable: %ld kB\nBuffers: %ld kB\n"
|
||||||
}
|
"Cached: %ld kB\n",
|
||||||
if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
|
&total, &free, &buffers, &buffers, &cached) == 5) ?
|
||||||
fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
|
bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total) :
|
||||||
fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
|
unknown_str;
|
||||||
&buffers, &buffers) != 2 ||
|
|
||||||
fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
|
|
||||||
goto scanerr;
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
return bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total);
|
|
||||||
|
|
||||||
scanerr:
|
|
||||||
fclose(fp);
|
|
||||||
return unknown_str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
ram_total(void)
|
ram_total(void)
|
||||||
{
|
{
|
||||||
long total;
|
long total;
|
||||||
FILE *fp;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
fp = fopen("/proc/meminfo", "r");
|
return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%f", (float)total / 1024 / 1024) : unknown_str;
|
||||||
warn("Failed to open file /proc/meminfo");
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "MemTotal: %ld kB\n", &total);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%f", (float)total / 1024 / 1024);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
ram_used(void)
|
ram_used(void)
|
||||||
{
|
{
|
||||||
long free, total, buffers, cached;
|
long total, free, buffers, cached;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen("/proc/meminfo", "r");
|
return (pscanf("/proc/meminfo",
|
||||||
if (fp == NULL) {
|
"MemTotal: %ld kB\n"
|
||||||
warn("Failed to open file /proc/meminfo");
|
"MemFree: %ld kB\n"
|
||||||
return unknown_str;
|
"MemAvailable: %ld kB\nBuffers: %ld kB\n"
|
||||||
}
|
"Cached: %ld kB\n",
|
||||||
if (fscanf(fp, "MemTotal: %ld kB\n", &total) != 1 ||
|
&total, &free, &buffers, &buffers, &cached) == 5) ?
|
||||||
fscanf(fp, "MemFree: %ld kB\n", &free) != 1 ||
|
bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024) :
|
||||||
fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n",
|
unknown_str;
|
||||||
&buffers, &buffers) != 2 ||
|
|
||||||
fscanf(fp, "Cached: %ld kB\n", &cached) != 1)
|
|
||||||
goto scanerr;
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
return bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
|
|
||||||
|
|
||||||
scanerr:
|
|
||||||
fclose(fp);
|
|
||||||
return unknown_str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
@ -680,20 +599,10 @@ swap_used(void)
|
||||||
static const char *
|
static const char *
|
||||||
temp(const char *file)
|
temp(const char *file)
|
||||||
{
|
{
|
||||||
int n, temp;
|
int temp;
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen(file, "r");
|
return (pscanf(file, "%d", &temp) == 1) ?
|
||||||
if (fp == NULL) {
|
bprintf("%d", temp / 1000) : unknown_str;
|
||||||
warn("Failed to open file %s", file);
|
|
||||||
return unknown_str;
|
|
||||||
}
|
|
||||||
n = fscanf(fp, "%d", &temp);
|
|
||||||
fclose(fp);
|
|
||||||
if (n != 1)
|
|
||||||
return unknown_str;
|
|
||||||
|
|
||||||
return bprintf("%d", temp / 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
|
Loading…
Reference in a new issue