From d3d8b8ee03a773b7f2cd1aad16ee43d9784e5139 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sun, 4 Sep 2016 00:10:49 +0300 Subject: added daemonization support --- arg.h | 51 +++++++++++++++++++++++++++++++ concat.h | 19 ++++++++++++ slstatus.c | 102 +++++++++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 145 insertions(+), 27 deletions(-) create mode 100644 arg.h create mode 100644 concat.h diff --git a/arg.h b/arg.h new file mode 100644 index 0000000..6c4a528 --- /dev/null +++ b/arg.h @@ -0,0 +1,51 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/concat.h b/concat.h new file mode 100644 index 0000000..9f138d7 --- /dev/null +++ b/concat.h @@ -0,0 +1,19 @@ +/* + * Thanks to lloyd for contribution + */ + +extern char concat[4096]; + +extern void +ccat(const unsigned short int count, ...) +{ + va_list ap; + unsigned short int i; + concat[0] = '\0'; + + va_start(ap, count); + for(i = 0; i < count; i++) + strlcat(concat, va_arg(ap, char *), sizeof(concat)); + va_end(ap); + return; +} diff --git a/slstatus.c b/slstatus.c index ab461c8..5c83a84 100644 --- a/slstatus.c +++ b/slstatus.c @@ -24,8 +24,15 @@ #undef strlcat #undef strlcpy +#include "arg.h" #include "strlcat.h" #include "strlcpy.h" +#include "concat.h" + +char *argv0; +char concat[]; + +FILE *foutput; struct arg { char *(*func)(); @@ -107,7 +114,7 @@ battery_perc(const char *battery) fp = fopen(batterynowfile, "r"); if (fp == NULL ) { - fprintf(stderr, "Error opening battery file: %s: %s\n", + fprintf(foutput, "Error opening battery file: %s: %s\n", batterynowfile, strerror(errno)); return smprintf(UNKNOWN_STR); @@ -118,7 +125,7 @@ battery_perc(const char *battery) fp = fopen(batteryfullfile, "r"); if (fp == NULL) { - fprintf(stderr, "Error opening battery file: %s\n", + fprintf(foutput, "Error opening battery file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -139,7 +146,7 @@ cpu_perc(void) FILE *fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -151,7 +158,7 @@ cpu_perc(void) fp = fopen("/proc/stat","r"); if (fp == NULL) { - fprintf(stderr, "Error opening stat file: %s\n", + fprintf(foutput, "Error opening stat file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -181,7 +188,7 @@ disk_free(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -195,7 +202,7 @@ disk_perc(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -210,7 +217,7 @@ disk_total(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -224,7 +231,7 @@ disk_used(const char *mountpoint) struct statvfs fs; if (statvfs(mountpoint, &fs) < 0) { - fprintf(stderr, "Could not get filesystem info: %s\n", + fprintf(foutput, "Could not get filesystem info: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -239,7 +246,7 @@ entropy(void) FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open entropy file: %s\n", + fprintf(foutput, "Could not open entropy file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -262,7 +269,7 @@ hostname(void) FILE *fp = fopen("/proc/sys/kernel/hostname", "r"); if (fp == NULL) { - fprintf(stderr, "Could not open hostname file: %s\n", + fprintf(foutput, "Could not open hostname file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -283,7 +290,7 @@ ip(const char *interface) char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { - fprintf(stderr, "Error getting IP address: %s\n", + fprintf(foutput, "Error getting IP address: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -298,7 +305,7 @@ ip(const char *interface) if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) { if (s != 0) { - fprintf(stderr, "Error getting IP address.\n"); + fprintf(foutput, "Error getting IP address.\n"); return smprintf(UNKNOWN_STR); } return smprintf("%s", host); @@ -317,7 +324,7 @@ load_avg(void) double avgs[3]; if (getloadavg(avgs, 3) < 0) { - fprintf(stderr, "Error getting load avg.\n"); + fprintf(foutput, "Error getting load avg.\n"); return smprintf(UNKNOWN_STR); } @@ -331,7 +338,7 @@ ram_free(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -349,7 +356,7 @@ ram_perc(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -371,7 +378,7 @@ ram_total(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -388,7 +395,7 @@ ram_used(void) FILE *fp = fopen("/proc/meminfo", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening meminfo file: %s\n", + fprintf(foutput, "Error opening meminfo file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -411,7 +418,7 @@ run_command(const char* command) char buffer[64]; if (fp == NULL) { - fprintf(stderr, "Could not get command output for: %s: %s\n", + fprintf(foutput, "Could not get command output for: %s: %s\n", command, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -436,7 +443,7 @@ temp(const char *file) FILE *fp = fopen(file, "r"); if (fp == NULL) { - fprintf(stderr, "Could not open temperature file: %s\n", + fprintf(foutput, "Could not open temperature file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -469,7 +476,7 @@ username(void) if (pw == NULL) return smprintf("%s", pw->pw_name); - fprintf(stderr, "Could not get username: %s\n", + fprintf(foutput, "Could not get username: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -502,7 +509,7 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_malloc(&vol_info); snd_mixer_selem_id_malloc(&mute_info); if (vol_info == NULL || mute_info == NULL) { - fprintf(stderr, "Could not get alsa volume.\n"); + fprintf(foutput, "Could not get alsa volume.\n"); return smprintf(UNKNOWN_STR); } snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL); @@ -545,7 +552,7 @@ wifi_perc(const char *wificard) fp = fopen(path, "r"); if(fp == NULL) { - fprintf(stderr, "Error opening wifi operstate file: %s\n", + fprintf(foutput, "Error opening wifi operstate file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -557,7 +564,7 @@ wifi_perc(const char *wificard) fp = fopen("/proc/net/wireless", "r"); if (fp == NULL) { - fprintf(stderr, "Error opening wireless file: %s\n", + fprintf(foutput, "Error opening wireless file: %s\n", strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -589,13 +596,13 @@ wifi_essid(const char *wificard) wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); if(sockfd == -1) { - fprintf(stderr, "Cannot open socket for interface: %s: %s\n", + fprintf(foutput, "Cannot open socket for interface: %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { - fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n", + fprintf(foutput, "Get ESSID ioctl failed for interface %s: %s\n", wificard, strerror(errno)); return smprintf(UNKNOWN_STR); } @@ -606,16 +613,57 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } +void +usage(void) +{ + fprintf(stderr, "usage: %s [-d] [-l path]\n", argv0); + exit(0); +} + int -main(void) +main(int argc, char *argv[]) { + unsigned short int dflag, lflag; size_t i; - char status_string[4096]; + char status_string[4096], logpath[4096]; char *res, *element; struct arg argument; + foutput = stderr; dpy = XOpenDisplay(0x0); + ARGBEGIN { + case 'd': + dflag = 1; + break; + case 'l': + strlcpy(logpath, EARGF(usage()), sizeof(logpath)-1); + logpath[strlen(logpath)+1] = '\0'; + foutput = fopen(logpath, "a"); + if (foutput == NULL) { + fprintf(stderr, "failed to open log file at %s: %s\n", + logpath, strerror(errno)); + return (1); + } + lflag = 1; + break; + default: + usage(); + } ARGEND + + if (dflag && !lflag) { + ccat(2, getenv("HOME"), "/.slstatus_log"); + foutput = fopen(concat, "a"); + if (foutput == NULL) { + fprintf(stderr, "failed to open log file at %s: %s\n", + logpath, strerror(errno)); + return (1); + } + } + + if (dflag) + daemon(0, 0); + for (;;) { memset(status_string, 0, sizeof(status_string)); for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { -- cgit v1.2.3