Xinqi Bao's Git
projects
/
slstatus.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
Fixed out of boundary write on long lines.
[slstatus.git]
/
slstatus.c
diff --git
a/slstatus.c
b/slstatus.c
index
155cf2e
..
6deb68c
100644
(file)
--- a/
slstatus.c
+++ b/
slstatus.c
@@
-25,11
+25,7
@@
#include <unistd.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
-#undef strlcat
-
-#include "extern/arg.h"
-#include "extern/strlcat.h"
-#include "extern/concat.h"
+#include "arg.h"
struct arg {
char *(*func)();
struct arg {
char *(*func)();
@@
-51,6
+47,7
@@
static char *gid(void);
static char *hostname(void);
static char *ip(const char *iface);
static char *kernel_release(void);
static char *hostname(void);
static char *ip(const char *iface);
static char *kernel_release(void);
+static char *keyboard_indicators(void);
static char *load_avg(void);
static char *ram_free(void);
static char *ram_perc(void);
static char *load_avg(void);
static char *ram_free(void);
static char *ram_perc(void);
@@
-72,7
+69,6
@@
static void sighandler(const int signo);
static void usage(const int eval);
char *argv0;
static void usage(const int eval);
char *argv0;
-char concat[];
static unsigned short int delay = 0;
static unsigned short int done;
static unsigned short int dflag, oflag;
static unsigned short int delay = 0;
static unsigned short int done;
static unsigned short int dflag, oflag;
@@
-107,12
+103,13
@@
static char *
battery_perc(const char *bat)
{
int perc;
battery_perc(const char *bat)
{
int perc;
+ char path[PATH_MAX];
FILE *fp;
FILE *fp;
-
ccat(3
, "/sys/class/power_supply/", bat, "/capacity");
- fp = fopen(
concat
, "r");
+
snprintf(path, sizeof(path), "%s%s%s"
, "/sys/class/power_supply/", bat, "/capacity");
+ fp = fopen(
path
, "r");
if (fp == NULL) {
if (fp == NULL) {
- warn("Failed to open file %s",
concat
);
+ warn("Failed to open file %s",
path
);
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%i", &perc);
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%i", &perc);
@@
-124,13
+121,14
@@
battery_perc(const char *bat)
static char *
battery_state(const char *bat)
{
static char *
battery_state(const char *bat)
{
+ char path[PATH_MAX];
char state[12];
FILE *fp;
char state[12];
FILE *fp;
-
ccat(3
, "/sys/class/power_supply/", bat, "/status");
- fp = fopen(
concat
, "r");
+
snprintf(path, sizeof(path), "%s%s%s"
, "/sys/class/power_supply/", bat, "/status");
+ fp = fopen(
path
, "r");
if (fp == NULL) {
if (fp == NULL) {
- warn("Failed to open file %s",
concat
);
+ warn("Failed to open file %s",
path
);
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%12s", state);
return smprintf("%s", UNKNOWN_STR);
}
fscanf(fp, "%12s", state);
@@
-142,6
+140,8
@@
battery_state(const char *bat)
return smprintf("-");
} else if (strcmp(state, "Full") == 0) {
return smprintf("=");
return smprintf("-");
} else if (strcmp(state, "Full") == 0) {
return smprintf("=");
+ } else if (strcmp(state, "Unknown") == 0) {
+ return smprintf("/");
} else {
return smprintf("?");
}
} else {
return smprintf("?");
}
@@
-325,6
+325,29
@@
kernel_release(void)
return smprintf("%s", udata.release);
}
return smprintf("%s", udata.release);
}
+static char *
+keyboard_indicators(void)
+{
+ Display *dpy = XOpenDisplay(NULL);
+ XKeyboardState state;
+ XGetKeyboardControl(dpy, &state);
+ XCloseDisplay(dpy);
+
+ switch (state.led_mask) {
+ case 1:
+ return smprintf("c");
+ break;
+ case 2:
+ return smprintf("n");
+ break;
+ case 3:
+ return smprintf("cn");
+ break;
+ default:
+ return smprintf("");
+ }
+}
+
static char *
load_avg(void)
{
static char *
load_avg(void)
{
@@
-424,9
+447,9
@@
run_command(const char *cmd)
warn("Failed to get command output for %s", cmd);
return smprintf("%s", UNKNOWN_STR);
}
warn("Failed to get command output for %s", cmd);
return smprintf("%s", UNKNOWN_STR);
}
- fgets(buf, sizeof(buf), fp);
+ fgets(buf, sizeof(buf)
- 1
, fp);
pclose(fp);
pclose(fp);
- buf[sizeof(buf)] = '\0';
+ buf[sizeof(buf)
- 1
] = '\0';
if ((nlptr = strstr(buf, "\n")) != NULL) {
nlptr[0] = '\0';
if ((nlptr = strstr(buf, "\n")) != NULL) {
nlptr[0] = '\0';
@@
-450,7
+473,7
@@
swap_free(void)
return smprintf("%s", UNKNOWN_STR);
}
return smprintf("%s", UNKNOWN_STR);
}
- if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf)
- 1
, fp)) == 0) {
warn("swap_free: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
warn("swap_free: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
@@
-487,7
+510,7
@@
swap_perc(void)
return smprintf("%s", UNKNOWN_STR);
}
return smprintf("%s", UNKNOWN_STR);
}
- if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf)
- 1
, fp)) == 0) {
warn("swap_perc: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
warn("swap_perc: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
@@
-528,7
+551,7
@@
swap_total(void)
warn("Failed to open file /proc/meminfo");
return smprintf("%s", UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf("%s", UNKNOWN_STR);
}
- if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf)
- 1
, fp)) == 0) {
warn("swap_total: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
warn("swap_total: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
@@
-559,7
+582,7
@@
swap_used(void)
warn("Failed to open file /proc/meminfo");
return smprintf("%s", UNKNOWN_STR);
}
warn("Failed to open file /proc/meminfo");
return smprintf("%s", UNKNOWN_STR);
}
- if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf)
- 1
, fp)) == 0) {
warn("swap_used: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
warn("swap_used: read error");
fclose(fp);
return smprintf("%s", UNKNOWN_STR);
@@
-661,9
+684,7
@@
vol_perc(const char *card)
}
close(afd);
}
close(afd);
- if (v == 0) {
- return smprintf("mute");
- }
+
return smprintf("%d%%", v & 0xff);
}
return smprintf("%d%%", v & 0xff);
}
@@
-673,13
+694,14
@@
wifi_perc(const char *iface)
int perc;
char buf[255];
char *datastart;
int perc;
char buf[255];
char *datastart;
+ char path[PATH_MAX];
char status[5];
FILE *fp;
char status[5];
FILE *fp;
-
ccat(3
, "/sys/class/net/", iface, "/operstate");
- fp = fopen(
concat
, "r");
+
snprintf(path, sizeof(path), "%s%s%s"
, "/sys/class/net/", iface, "/operstate");
+ fp = fopen(
path
, "r");
if (fp == NULL) {
if (fp == NULL) {
- warn("Failed to open file %s",
concat
);
+ warn("Failed to open file %s",
path
);
return smprintf("%s", UNKNOWN_STR);
}
fgets(status, 5, fp);
return smprintf("%s", UNKNOWN_STR);
}
fgets(status, 5, fp);
@@
-693,13
+715,13
@@
wifi_perc(const char *iface)
warn("Failed to open file /proc/net/wireless");
return smprintf("%s", UNKNOWN_STR);
}
warn("Failed to open file /proc/net/wireless");
return smprintf("%s", UNKNOWN_STR);
}
- ccat(2, iface, ":");
+
fgets(buf, sizeof(buf), fp);
fgets(buf, sizeof(buf), fp);
fgets(buf, sizeof(buf), fp);
fclose(fp);
fgets(buf, sizeof(buf), fp);
fgets(buf, sizeof(buf), fp);
fgets(buf, sizeof(buf), fp);
fclose(fp);
- if ((datastart = strstr(buf,
concat
)) == NULL) {
+ if ((datastart = strstr(buf,
iface
)) == NULL) {
return smprintf("%s", UNKNOWN_STR);
}
datastart = (datastart+(strlen(iface)+1));
return smprintf("%s", UNKNOWN_STR);
}
datastart = (datastart+(strlen(iface)+1));
@@
-810,9
+832,7
@@
main(int argc, char *argv[])
element = smprintf("%s", UNKNOWN_STR);
warnx("Failed to format output");
}
element = smprintf("%s", UNKNOWN_STR);
warnx("Failed to format output");
}
- if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string)) {
- warnx("Output too long");
- }
+ strncat(status_string, element, sizeof(status_string) - strlen(status_string) - 1);
free(res);
free(element);
}
free(res);
free(element);
}