Xinqi Bao's Git

Reduce -o | -n to -s
[slstatus.git] / slstatus.c
index af26fe7..4b6770b 100644 (file)
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 
+#include <dirent.h>
 #include <err.h>
 #include <fcntl.h>
 #include <ifaddrs.h>
@@ -51,6 +52,7 @@ static const char *ip(const char *iface);
 static const char *kernel_release(void);
 static const char *keyboard_indicators(void);
 static const char *load_avg(void);
+static const char *num_files(const char *dir);
 static const char *ram_free(void);
 static const char *ram_perc(void);
 static const char *ram_used(void);
@@ -68,12 +70,11 @@ static const char *vol_perc(const char *card);
 static const char *wifi_perc(const char *iface);
 static const char *wifi_essid(const char *iface);
 static void sighandler(const int signo);
-static void usage(const int eval);
+static void usage(void);
 
 char *argv0;
 static unsigned short int delay = 0;
 static unsigned short int done;
-static unsigned short int dflag, oflag, nflag;
 static Display *dpy;
 
 #include "config.h"
@@ -402,6 +403,29 @@ load_avg(void)
        return bprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
 }
 
+static const char *
+num_files(const char *dir)
+{
+       struct dirent *dp;
+       DIR *fd;
+       int num = 0;
+
+       if ((fd = opendir(dir)) == NULL) {
+               warn("Failed to get number of files in directory %s", dir);
+               return UNKNOWN_STR;
+       }
+
+       while ((dp = readdir(fd)) != NULL) {
+               if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
+                       continue; /* skip self and parent */
+               num++;
+       }
+
+       closedir(fd);
+
+       return bprintf("%d", num);
+}
+
 static const char *
 ram_free(void)
 {
@@ -813,54 +837,36 @@ sighandler(const int signo)
 }
 
 static void
-usage(const int eval)
+usage(void)
 {
-       fprintf(stderr, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0);
-       exit(eval);
+       fprintf(stderr, "usage: %s [-s]\n", argv0);
+       exit(1);
 }
 
 int
 main(int argc, char *argv[])
 {
-       unsigned short int i;
-       char status_string[MAXLEN];
-       char *element;
        struct arg argument;
        struct sigaction act;
-       size_t len;
+       size_t i, len;
+       int sflag = 0;
+       char status_string[MAXLEN];
+       char *element;
 
        ARGBEGIN {
-               case 'd':
-                       dflag = 1;
-                       break;
-               case 'o':
-                       oflag = 1;
+               case 's':
+                       sflag = 1;
                        break;
-               case 'n':
-                       nflag = 1;
-                       break;
-               case 'v':
-                       printf("slstatus (C) 2016-2017 slstatus engineers\n");
-                       return 0;
-               case 'h':
-                       usage(0);
                default:
-                       usage(1);
+                       usage();
        } ARGEND
 
-       if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) {
-               usage(1);
-       }
-       if (dflag && daemon(1, 1) < 0) {
-               err(1, "daemon");
-       }
-
        memset(&act, 0, sizeof(act));
        act.sa_handler = sighandler;
        sigaction(SIGINT,  &act, 0);
        sigaction(SIGTERM, &act, 0);
 
-       if (!oflag) {
+       if (!sflag) {
                dpy = XOpenDisplay(NULL);
        }
 
@@ -882,11 +888,8 @@ main(int argc, char *argv[])
                        }
                }
 
-               if (oflag) {
-                       printf("%s\n", status_string);
-               } else if (nflag) {
+               if (sflag) {
                        printf("%s\n", status_string);
-                       done = 1;
                } else {
                        XStoreName(dpy, DefaultRootWindow(dpy), status_string);
                        XSync(dpy, False);
@@ -901,7 +904,7 @@ main(int argc, char *argv[])
                }
        }
 
-       if (!oflag) {
+       if (!sflag) {
                XStoreName(dpy, DefaultRootWindow(dpy), NULL);
                XCloseDisplay(dpy);
        }