tAdd a plugin that simply reads from a file - spoon - dwm status utility (2f30 fork) (HTM) git clone git://src.adamsgaard.dk/spoon (DIR) Log (DIR) Files (DIR) Refs (DIR) LICENSE --- (DIR) commit 053ab46b88574588dcbaff5db0757414d453b446 (DIR) parent 173be77dfcc78c114619535d1b19c3a449af03cd (HTM) Author: lostd <lostd@2f30.org> Date: Thu, 30 Mar 2017 17:39:16 +0300 Add a plugin that simply reads from a file It's intended for single-line files with a newline. Diffstat: M Makefile | 4 ++-- M config.def.h | 1 + A file.c | 24 ++++++++++++++++++++++++ M spoon.c | 1 + M stub.c | 7 +++++++ 5 files changed, 35 insertions(+), 2 deletions(-) --- (DIR) diff --git a/Makefile b/Makefile t@@ -1,8 +1,8 @@ VERSION = 0.3 PREFIX = /usr/local -SRC = spoon.c batt.c wifi.c cpu.c temp.c date.c load.c\ +SRC = spoon.c batt.c wifi.c cpu.c temp.c date.c load.c file.c\ strlcpy.c strlcat.c stub.c mix.c xkblayout.c mpd.c -OBJ = spoon.o batt.o wifi.o cpu.o temp.o date.o load.o\ +OBJ = spoon.o batt.o wifi.o cpu.o temp.o date.o load.o file.o\ strlcpy.o strlcat.o stub.o BIN = spoon DISTFILES = $(SRC) types.h util.h config.def.h Makefile LICENSE configure (DIR) diff --git a/config.def.h b/config.def.h t@@ -11,5 +11,6 @@ struct ent ents[] = { { .fmt = "%s ", .read = battread, .arg = NULL }, { .fmt = "%s ", .read = wifiread, .arg = NULL }, { .fmt = "[%s] ", .read = xkblayoutread, .arg = NULL }, + { .fmt = "%s ", .read = fileread, .arg = "/etc/myname" }, { .fmt = "%s", .read = dateread, .arg = (char []){"%a %d %b %Y %H:%M %Z"} }, }; (DIR) diff --git a/file.c b/file.c t@@ -0,0 +1,24 @@ +#include <err.h> +#include <fcntl.h> +#include <unistd.h> + +int +fileread(void *arg, char *buf, size_t len) +{ + char *path = arg; + ssize_t n; + int fd; + + fd = open(path, O_RDONLY); + if (fd == -1) { + warn("open %s", path); + return -1; + } + n = read(fd, buf, len); + close(fd); + if (n == -1 || n == 0) + return -1; + else + buf[n - 1] = '\0'; + return 0; +} (DIR) diff --git a/spoon.c b/spoon.c t@@ -17,6 +17,7 @@ int mpdread(void *, char *, size_t); int tempread(void *, char *, size_t); int wifiread(void *, char *, size_t); int xkblayoutread(void *, char *, size_t); +int fileread(void *, char *, size_t); struct ent { char *fmt; (DIR) diff --git a/stub.c b/stub.c t@@ -48,3 +48,10 @@ xkblayoutread(void *arg, char *buf, size_t len) { return -1; } + +#pragma weak fileread +int +fileread(void *arg, char *buf, size_t len) +{ + return -1; +}