Xinqi Bao's Git
projects
/
slstatus.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
add extra error tests to swap_*() && fix bytes_read bug
[slstatus.git]
/
slstatus.c
diff --git
a/slstatus.c
b/slstatus.c
index
f39a2dc
..
ac09c9f
100644
(file)
--- a/
slstatus.c
+++ b/
slstatus.c
@@
-20,6
+20,7
@@
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
+#include <sys/utsname.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
@@
-29,7
+30,6
@@
#include "extern/arg.h"
#include "extern/strlcat.h"
#include "extern/arg.h"
#include "extern/strlcat.h"
-#include "extern/strlcpy.h"
#include "extern/concat.h"
struct arg {
#include "extern/concat.h"
struct arg {
@@
-68,13
+68,14
@@
static char *username(void);
static char *vol_perc(const char *card);
static char *wifi_perc(const char *iface);
static char *wifi_essid(const char *iface);
static char *vol_perc(const char *card);
static char *wifi_perc(const char *iface);
static char *wifi_essid(const char *iface);
+static char *kernel_release(void);
static void set_status(const char *str);
static void sighandler(const int signo);
static void usage(void);
char *argv0;
char concat[];
static void set_status(const char *str);
static void sighandler(const int signo);
static void usage(void);
char *argv0;
char concat[];
-static unsigned short int delay;
+static unsigned short int delay
= 0
;
static unsigned short int done;
static unsigned short int dflag, oflag;
static Display *dpy;
static unsigned short int done;
static unsigned short int dflag, oflag;
static Display *dpy;
@@
-163,7
+164,7
@@
cpu_perc(void)
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
fclose(fp);
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
fclose(fp);
- delay
= (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1))
;
+ delay
++
;
sleep(delay);
fp = fopen("/proc/stat", "r");
sleep(delay);
fp = fopen("/proc/stat", "r");
@@
-404,19
+405,22
@@
ram_used(void)
static char *
run_command(const char *cmd)
{
static char *
run_command(const char *cmd)
{
+ char *nlptr;
FILE *fp;
FILE *fp;
- char buf[1024] =
"n/a"
;
+ char buf[1024] =
UNKNOWN_STR
;
fp = popen(cmd, "r");
if (fp == NULL) {
warn("Failed to get command output for %s", cmd);
return smprintf(UNKNOWN_STR);
}
fp = popen(cmd, "r");
if (fp == NULL) {
warn("Failed to get command output for %s", cmd);
return smprintf(UNKNOWN_STR);
}
- fgets(buf, sizeof(buf)
-1
, fp);
+ fgets(buf, sizeof(buf), fp);
pclose(fp);
pclose(fp);
-
buf[strlen(buf)] = '\0';
buf[strlen(buf)] = '\0';
- strtok(buf, "\n");
+
+ if ((nlptr = strstr(buf, "\n")) != NULL) {
+ nlptr[0] = '\0';
+ }
return smprintf("%s", buf);
}
return smprintf("%s", buf);
}
@@
-424,7
+428,7
@@
run_command(const char *cmd)
static char *
swap_free(void)
{
static char *
swap_free(void)
{
- long free;
+ long
total,
free;
FILE *fp;
char buf[2048];
size_t bytes_read;
FILE *fp;
char buf[2048];
size_t bytes_read;
@@
-435,15
+439,24
@@
swap_free(void)
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
- bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_free: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
buf[bytes_read] = '\0';
fclose(fp);
buf[bytes_read] = '\0';
fclose(fp);
- if (bytes_read == 0 || bytes_read == sizeof(buf)) {
- warn("Failed to read /proc/meminfo\n");
- return smprintf(UNKNOWN_STR);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf(
"%s",
UNKNOWN_STR);
}
}
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
- match = strstr(buf, "SwapFree");
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%f", (float)free / 1024 / 1024);
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%f", (float)free / 1024 / 1024);
@@
-463,21
+476,29
@@
swap_perc(void)
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
- bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_perc: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
buf[bytes_read] = '\0';
fclose(fp);
buf[bytes_read] = '\0';
fclose(fp);
- if (bytes_read == 0 || bytes_read == sizeof(buf)) {
- warn("Failed to read /proc/meminfo\n");
- return smprintf(UNKNOWN_STR);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf(
"%s",
UNKNOWN_STR);
}
}
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
- match = strstr(buf, "SwapCached");
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapCached: %ld kB\n", &cached);
sscanf(match, "SwapCached: %ld kB\n", &cached);
- match = strstr(buf, "SwapTotal");
- sscanf(match, "SwapTotal: %ld kB\n", &total);
-
- match = strstr(buf, "SwapFree");
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%d%%", 100 * (total - free - cached) / total);
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%d%%", 100 * (total - free - cached) / total);
@@
-497,15
+518,18
@@
swap_total(void)
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
- bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_total: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
buf[bytes_read] = '\0';
fclose(fp);
buf[bytes_read] = '\0';
fclose(fp);
- if (bytes_read == 0 || bytes_read == sizeof(buf)) {
- warn("Failed to read /proc/meminfo\n");
- return smprintf(UNKNOWN_STR);
- }
- match = strstr(buf, "SwapTotal");
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapTotal: %ld kB\n", &total);
return smprintf("%f", (float)total / 1024 / 1024);
sscanf(match, "SwapTotal: %ld kB\n", &total);
return smprintf("%f", (float)total / 1024 / 1024);
@@
-525,21
+549,28
@@
swap_used(void)
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf(UNKNOWN_STR);
}
- bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_used: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
buf[bytes_read] = '\0';
fclose(fp);
buf[bytes_read] = '\0';
fclose(fp);
- if (bytes_read == 0 || bytes_read == sizeof(buf)) {
- warn("Failed to read /proc/meminfo\n");
- return smprintf(UNKNOWN_STR);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf(
"%s",
UNKNOWN_STR);
}
}
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
- match = strstr(buf, "SwapCached");
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapCached: %ld kB\n", &cached);
sscanf(match, "SwapCached: %ld kB\n", &cached);
- match = strstr(buf, "SwapTotal");
- sscanf(match, "SwapTotal: %ld kB\n", &total);
-
- match = strstr(buf, "SwapFree");
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
sscanf(match, "SwapFree: %ld kB\n", &free);
return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
@@
-706,6
+737,16
@@
wifi_essid(const char *iface)
return smprintf("%s", (char *)wreq.u.essid.pointer);
}
return smprintf("%s", (char *)wreq.u.essid.pointer);
}
+static char *
+kernel_release(void)
+{
+ struct utsname udata;
+ if (uname(&udata) < 0)
+ return smprintf(UNKNOWN_STR);
+
+ return smprintf("%s", udata.release);
+}
+
static void
set_status(const char *str)
{
static void
set_status(const char *str)
{
@@
-732,7
+773,7
@@
int
main(int argc, char *argv[])
{
unsigned short int i;
main(int argc, char *argv[])
{
unsigned short int i;
- char status_string[
4096
];
+ char status_string[
2048
];
char *res, *element;
struct arg argument;
struct sigaction act;
char *res, *element;
struct arg argument;
struct sigaction act;
@@
-784,7
+825,8
@@
main(int argc, char *argv[])
element = smprintf(UNKNOWN_STR);
warnx("Failed to format output");
}
element = smprintf(UNKNOWN_STR);
warnx("Failed to format output");
}
- strlcat(status_string, element, sizeof(status_string));
+ if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string))
+ warnx("Output too long");
free(res);
free(element);
}
free(res);
free(element);
}
@@
-799,8
+841,13
@@
main(int argc, char *argv[])
* subtract delay time spend in function
* calls from the actual global delay time
*/
* subtract delay time spend in function
* calls from the actual global delay time
*/
- sleep(UPDATE_INTERVAL - delay);
- delay = 0;
+ if ((UPDATE_INTERVAL - delay) <= 0) {
+ delay = 0;
+ continue;
+ } else {
+ sleep(UPDATE_INTERVAL - delay);
+ delay = 0;
+ }
}
if (!oflag) {
}
if (!oflag) {