summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraaron marcher <me@drkhsh.at>2017-08-06 15:02:16 +0200
committeraaron marcher <me@drkhsh.at>2017-08-06 15:02:16 +0200
commit4b4b2ac0790a51ea624397f829f29ed92e82fd53 (patch)
tree3f84bcbfb7dec14f0bff9e0cd996aa5d141b701d
parentad39aa012c5da8f1f53c8f7366d5177e538da255 (diff)
add num_files() function for maildirs ;)
-rw-r--r--README.md1
-rw-r--r--config.def.h1
-rw-r--r--slstatus.c25
3 files changed, 27 insertions, 0 deletions
diff --git a/README.md b/README.md
index e6318e5..408477e 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ The following information is included:
- Kernel version
- Keyboard indicators
- Load averages
+- Number of files in a directory (hint: Maildir)
- Memory status (free memory, percentage, total memory and used memory)
- Swap status (free swap, percentage, total swap and used swap)
- Temperature
diff --git a/config.def.h b/config.def.h
index a961dbc..ed339ed 100644
--- a/config.def.h
+++ b/config.def.h
@@ -27,6 +27,7 @@
- kernel_release (uname -r) [argument: NULL]
- keyboard_indicators (caps/num lock indicators) [agrument: NULL]
- load_avg (load average) [argument: NULL]
+- num_files (number of files in a directory - hint: maildir) [argument: path]
- ram_free (free ram in GB) [argument: NULL]
- ram_perc (ram usage in percent) [argument: NULL]
- ram_total (total ram in GB) [argument: NULL]
diff --git a/slstatus.c b/slstatus.c
index af26fe7..5564771 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -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);
@@ -403,6 +405,29 @@ load_avg(void)
}
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)
{
long free;