diff options
| author | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
| commit | 49590ebb174f8436d0f85af8e4f24bcb6f346b1d (patch) | |
| tree | b078a006ed285bd3440c919c91abac5915ca20ce /src/sandbarinfo.c | |
| parent | 970dcc487d7a6a6bc99e37b65ee512a2d651a583 (diff) | |
Diffstat (limited to 'src/sandbarinfo.c')
| -rw-r--r-- | src/sandbarinfo.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/sandbarinfo.c b/src/sandbarinfo.c new file mode 100644 index 0000000..c2c5022 --- /dev/null +++ b/src/sandbarinfo.c @@ -0,0 +1,91 @@ +#include "signal.h" +#include "text.h" + +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <sys/stat.h> +#include <assert.h> + +struct state { + int fd; + struct text * text; +}; + +static int +open_fifo(const char * path) +{ + if (access(path, F_OK) != 0) { + printf("FIFO doesn't exist\n"); + return -1; + } + + if (access(path, W_OK) != 0) { + printf("FIFO not writable\n"); + return -1; + } + + return open(path, O_WRONLY); +} + + +static int +init_state(struct state * s, const char * path) +{ + s->text = NULL; + + s->fd = open_fifo(path); + if(s->fd < 1) return -1; + + s->text = text_new(); + return 0; +} + +static void +deinit_state(struct state * s) +{ + if(s->fd > 0) close(s->fd); + if(s->text) text_free(s->text); +} + +static void +update_status(struct state * s) +{ + const size_t msg_len = TEXT_SIZE + 15; + size_t actual_len; + char msg[msg_len]; + + text_update(s->text); + snprintf(msg, msg_len, "all status %s\n", text_get_val(s->text)); + + actual_len = strlen(msg); + write(s->fd, msg, actual_len); +} + +static void +run_loop(struct state * s) +{ + while(signal_running) { + update_status(s); + usleep(1000 * 800); + } +} + +int main(int argc, char ** argv) +{ + int rc; + struct state s; + + assert(argc == 2); + + signal_setup_actions(); + + rc = init_state(&s, argv[1]); + + if(rc == 0) run_loop(&s); + + deinit_state(&s); + + return 0; +} |
