wmsl

Block-based window manager status line
git clone https://git.sinitax.com/sinitax/wmsl
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 9806aff5927a13ef6b84ac4f6bd41509a4954500
parent 0d0c642cb930e7e63db450b200a50e11b2629ef8
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri,  1 Jul 2022 19:43:36 +0200

Add timeout to reading block output

Diffstat:
Mwmsl.c | 46++++++++++++++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/wmsl.c b/wmsl.c @@ -1,13 +1,14 @@ -#include <stdlib.h> -#include <stdio.h> +#include <X11/Xlib.h> + +#include <unistd.h> +#include <signal.h> +#include <time.h> #include <math.h> #include <string.h> -#include <time.h> -#include <unistd.h> #include <stdarg.h> -#include <signal.h> - -#include <X11/Xlib.h> +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> #define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) #define OUTPUTMAX 256 @@ -91,6 +92,33 @@ concat_status(const char *text, size_t len) status_text[status_len] = '\0'; } +bool +read_statusline(int fd, char *buf, size_t size) +{ + struct timeval timeout; + fd_set fdset; + int ret, nread; + char *tok; + + FD_ZERO(&fdset); + FD_SET(fd, &fdset); + + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + ret = select(fd + 1, &fdset, NULL, NULL, &timeout); + if (ret <= 0) return false; + + nread = read(fd, buf, size); + if (!nread) return false; + + if (!(tok = strchr(buf, '\n'))) + return false; + *tok = '\0'; + + return true; +} + void update_blocks(void) { @@ -114,12 +142,10 @@ update_blocks(void) cmd = popen(blocks[i].command, "r"); if (!cmd) continue; - if (!fgets(tmp_output, OUTPUTMAX, cmd)) { + if (!read_statusline(fileno(cmd), tmp_output, OUTPUTMAX)) { output_len = 0; } else { output_len = strlen(tmp_output); - if (tmp_output[output_len - 1] == '\n') - output_len -= 1; } pclose(cmd);