spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

commit cbac9231f348a7b018dfc6ac175cf6c3c78ae872
parent 053ab46b88574588dcbaff5db0757414d453b446
Author: lostd <lostd@2f30.org>
Date:   Sun,  2 Apr 2017 18:30:37 +0300

Read up to len and terminate at the first non-printable

Diffstat:
Mfile.c | 26+++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/file.c b/file.c @@ -1,24 +1,48 @@ +#include <ctype.h> #include <err.h> #include <fcntl.h> #include <unistd.h> +ssize_t +readn(int fd, void *buf, size_t nbytes) +{ + size_t nleft = nbytes; + ssize_t n; + + do { + n = read(fd, buf, nleft); + if (n == 0) + break; + else if (n == -1) + return -1; + nleft -= n; + buf += n; + } while (nleft > 0); + return (nbytes - nleft); +} + int fileread(void *arg, char *buf, size_t len) { char *path = arg; ssize_t n; int fd; + int i; fd = open(path, O_RDONLY); if (fd == -1) { warn("open %s", path); return -1; } - n = read(fd, buf, len); + n = readn(fd, buf, len); close(fd); if (n == -1 || n == 0) return -1; else buf[n - 1] = '\0'; + /* stop at the first non-printable character */ + for (i = 0; i < len; i++) + if (!isprint(buf[i])) + buf[i] = '\0'; return 0; }