Xinqi Bao's Git

signal handling to exit gracefully + small coding style fixes
[slstatus.git] / slstatus.c
index 76f0518..cb5f1f2 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/wireless.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <linux/wireless.h>
 #include <netdb.h>
 #include <pwd.h>
+#include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -37,8 +38,6 @@ struct arg {
        const char *args;
 };
 
        const char *args;
 };
 
-static unsigned short int delay;
-
 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);
@@ -64,8 +63,11 @@ static char *username(void);
 static char *vol_perc(const char *);
 static char *wifi_perc(const char *);
 static char *wifi_essid(const char *);
 static char *vol_perc(const char *);
 static char *wifi_perc(const char *);
 static char *wifi_essid(const char *);
+static void sighandler(const int);
 
 
+static unsigned short int delay;
 static Display *dpy;
 static Display *dpy;
+static int done = 0;
 
 #include "config.h"
 
 
 #include "config.h"
 
@@ -471,7 +473,7 @@ uid(void)
 }
 
 
 }
 
 
-static char * 
+static char *
 vol_perc(const char *snd_card)
 { /* FIX THIS SHIT! */
        long int vol, max, min;
 vol_perc(const char *snd_card)
 { /* FIX THIS SHIT! */
        long int vol, max, min;
@@ -517,7 +519,7 @@ wifi_perc(const char *wificard)
 
        fp = fopen(concat, "r");
 
 
        fp = fopen(concat, "r");
 
-       if(fp == NULL) {
+       if (fp == NULL) {
                warn("Error opening wifi operstate file");
                return smprintf(UNKNOWN_STR);
        }
                warn("Error opening wifi operstate file");
                return smprintf(UNKNOWN_STR);
        }
@@ -559,7 +561,7 @@ wifi_essid(const char *wificard)
        memset(&wreq, 0, sizeof(struct iwreq));
        wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
        sprintf(wreq.ifr_name, wificard);
        memset(&wreq, 0, sizeof(struct iwreq));
        wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
        sprintf(wreq.ifr_name, wificard);
-       if(sockfd == -1) {
+       if (sockfd == -1) {
                warn("Cannot open socket for interface: %s", wificard);
                return smprintf(UNKNOWN_STR);
        }
                warn("Cannot open socket for interface: %s", wificard);
                return smprintf(UNKNOWN_STR);
        }
@@ -569,12 +571,22 @@ wifi_essid(const char *wificard)
                return smprintf(UNKNOWN_STR);
        }
 
                return smprintf(UNKNOWN_STR);
        }
 
+       close(sockfd);
+
        if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
                return smprintf(UNKNOWN_STR);
        else
                return smprintf("%s", (char *)wreq.u.essid.pointer);
 }
 
        if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
                return smprintf(UNKNOWN_STR);
        else
                return smprintf("%s", (char *)wreq.u.essid.pointer);
 }
 
+static void
+sighandler(int signo)
+{
+       if (signo == SIGTERM || signo == SIGINT) {
+               done = 1;
+       }
+}
+
 int
 main(void)
 {
 int
 main(void)
 {
@@ -582,11 +594,17 @@ main(void)
        char status_string[4096];
        char *res, *element;
        struct arg argument;
        char status_string[4096];
        char *res, *element;
        struct arg argument;
+       struct sigaction act;
+
+       memset(&act, 0, sizeof(act));
+       act.sa_handler = sighandler;
+       sigaction(SIGINT,  &act, 0);
+       sigaction(SIGTERM, &act, 0);
 
        dpy = XOpenDisplay(NULL);
 
 
        dpy = XOpenDisplay(NULL);
 
-       for (;;) {
-               memset(status_string, 0, sizeof(status_string));
+       while (!done) {
+               status_string[0] = '\0';
                for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
                        argument = args[i];
                        if (argument.args == NULL)
                for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
                        argument = args[i];
                        if (argument.args == NULL)