commit 053ab46b88574588dcbaff5db0757414d453b446
parent 173be77dfcc78c114619535d1b19c3a449af03cd
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:
5 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -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
diff --git a/config.def.h b/config.def.h
@@ -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"} },
};
diff --git a/file.c b/file.c
@@ -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;
+}
diff --git a/spoon.c b/spoon.c
@@ -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;
diff --git a/stub.c b/stub.c
@@ -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;
+}