Xinqi Bao's Git
projects
/
slstatus.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
removed setstatus() && simplified main()
[slstatus.git]
/
slstatus.c
diff --git
a/slstatus.c
b/slstatus.c
index
5246bc0
..
2e57fbb
100644
(file)
--- a/
slstatus.c
+++ b/
slstatus.c
@@
-1,12
+1,10
@@
/* See LICENSE file for copyright and license details. */
#include <alsa/asoundlib.h>
/* See LICENSE file for copyright and license details. */
#include <alsa/asoundlib.h>
-#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <limits.h>
#include <linux/wireless.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <limits.h>
#include <linux/wireless.h>
-#include <locale.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h>
@@
-29,14
+27,12
@@
#include "strlcat.h"
#include "strlcpy.h"
#include "strlcat.h"
#include "strlcpy.h"
-typedef char *(*op_fun)();
struct arg {
struct arg {
-
op_fun func
;
+
char *(*func)()
;
const char *format;
const char *args;
};
const char *format;
const char *args;
};
-static void setstatus(const char *);
static char *smprintf(const char *, ...);
static char *battery_perc(const char *);
static char *cpu_perc(void);
static char *smprintf(const char *, ...);
static char *battery_perc(const char *);
static char *cpu_perc(void);
@@
-67,14
+63,6
@@
static Display *dpy;
#include "config.h"
#include "config.h"
-static void
-setstatus(const char *str)
-{
- /* set WM_NAME via X11 */
- XStoreName(dpy, DefaultRootWindow(dpy), str);
- XSync(dpy, False);
-}
-
static char *
smprintf(const char *fmt, ...)
{
static char *
smprintf(const char *fmt, ...)
{
@@
-252,7
+240,8
@@
entropy(void)
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) {
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) {
- fprintf(stderr, "Could not open entropy file.\n");
+ fprintf(stderr, "Could not open entropy file: %s\n",
+ strerror(errno));
return smprintf(UNKNOWN_STR);
}
return smprintf(UNKNOWN_STR);
}
@@
-264,8
+253,7
@@
entropy(void)
static char *
gid(void)
{
static char *
gid(void)
{
- gid_t gid = getgid();
- return smprintf("%d", gid);
+ return smprintf("%d", getgid());
}
static char *
}
static char *
@@
-476,45
+464,31
@@
uptime(void)
static char *
username(void)
{
static char *
username(void)
{
- register struct passwd *pw;
- register uid_t uid;
-
- uid = geteuid();
- pw = getpwuid(uid);
+ uid_t uid = geteuid();
+ struct passwd *pw = getpwuid(uid);
- if (pw)
+ if (pw
== NULL
)
return smprintf("%s", pw->pw_name);
return smprintf("%s", pw->pw_name);
- else {
- fprintf(stderr, "Could not get username: %s\n",
- strerror(errno));
- return smprintf(UNKNOWN_STR);
- }
+ fprintf(stderr, "Could not get username: %s\n",
+ strerror(errno));
return smprintf(UNKNOWN_STR);
}
static char *
uid(void)
{
return smprintf(UNKNOWN_STR);
}
static char *
uid(void)
{
- /* FIXME: WHY USE register modifier? */
- register uid_t uid;
-
- uid = geteuid();
-
- if (uid)
- return smprintf("%d", uid);
- else {
- fprintf(stderr, "Could not get uid.\n");
- return smprintf(UNKNOWN_STR);
- }
-
- return smprintf(UNKNOWN_STR);
+ return smprintf("%d", geteuid());
}
static char *
vol_perc(const char *soundcard)
{
}
static char *
vol_perc(const char *soundcard)
{
+ /*
+ * TODO: FIXME:
+ * https://github.com/drkh5h/slstatus/issues/12
+ */
int mute = 0;
long vol = 0, max = 0, min = 0;
snd_mixer_t *handle;
int mute = 0;
long vol = 0, max = 0, min = 0;
snd_mixer_t *handle;
@@
-563,12
+537,14
@@
wifi_perc(const char *wificard)
char path[64];
char status[5];
char needle[strlen(wificard)+2];
char path[64];
char status[5];
char needle[strlen(wificard)+2];
- FILE *fp
= fopen(path, "r")
;
+ FILE *fp;
strlcpy(path, "/sys/class/net/", sizeof(path));
strlcat(path, wificard, sizeof(path));
strlcat(path, "/operstate", sizeof(path));
strlcpy(path, "/sys/class/net/", sizeof(path));
strlcat(path, wificard, sizeof(path));
strlcat(path, "/operstate", sizeof(path));
+ fp = fopen(path, "r");
+
if(fp == NULL) {
fprintf(stderr, "Error opening wifi operstate file: %s\n",
strerror(errno));
if(fp == NULL) {
fprintf(stderr, "Error opening wifi operstate file: %s\n",
strerror(errno));
@@
-640,10
+616,6
@@
main(void)
struct arg argument;
dpy = XOpenDisplay(0x0);
struct arg argument;
dpy = XOpenDisplay(0x0);
- if (!dpy) {
- fprintf(stderr, "Cannot open display!\n");
- exit(1);
- }
for (;;) {
memset(status_string, 0, sizeof(status_string));
for (;;) {
memset(status_string, 0, sizeof(status_string));
@@
-663,10
+635,16
@@
main(void)
free(element);
}
free(element);
}
- setstatus(status_string);
+ XStoreName(dpy, DefaultRootWindow(dpy), status_string);
+ XSync(dpy, False);
sleep(UPDATE_INTERVAL -1);
}
sleep(UPDATE_INTERVAL -1);
}
+ /* NOT REACHED */
+ /*
+ * TODO: find out a way to exit successfully
+ * to prevent memory leaks
+ */
XCloseDisplay(dpy);
return 0;
}
XCloseDisplay(dpy);
return 0;
}