Xinqi Bao's Git

Remove d- and v-flags
[slstatus.git] / slstatus.c
index af26fe7..b31f21d 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,12 @@ 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 unsigned short int oflag, nflag;
 static Display *dpy;
 
 #include "config.h"
@@ -402,6 +404,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,10 +838,10 @@ 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 [-o | -n]\n", argv0);
+       exit(1);
 }
 
 int
@@ -830,29 +855,18 @@ main(int argc, char *argv[])
        size_t len;
 
        ARGBEGIN {
-               case 'd':
-                       dflag = 1;
-                       break;
                case 'o':
                        oflag = 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");
+       if (oflag && nflag) {
+               usage();
        }
 
        memset(&act, 0, sizeof(act));