diff options
Diffstat (limited to 'src/somebarinfo.c')
| -rw-r--r-- | src/somebarinfo.c | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/src/somebarinfo.c b/src/somebarinfo.c index 0dc2d50..3371674 100644 --- a/src/somebarinfo.c +++ b/src/somebarinfo.c @@ -1,18 +1,41 @@ #include "signal.h" #include "text.h" -#include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <fcntl.h> #include <string.h> +#include <sys/stat.h> #define SOMEBAR_CMD "status" struct state { - FILE * file; + int fd; struct text * text; }; +static bool +file_exists(const char * path) +{ + int fd = open(path, O_RDONLY); + bool exists = fd > 0; + if(exists) close(fd); + return exists; +} + +static int +open_fifo(const char * path) +{ + int rc; + if(!file_exists(path)) { + rc = mkfifo(path, 0644); + if(rc < 0) return -1; + } + + return open(path, O_WRONLY); +} + + static int init_state(struct state * s) { @@ -30,8 +53,8 @@ init_state(struct state * s) s->text = NULL; - s->file = fopen(path, "a"); - if(s->file == NULL) return -1; + s->fd = open_fifo(path); + if(s->fd < 1) return -1; s->text = text_new(); return 0; @@ -40,7 +63,7 @@ init_state(struct state * s) static void deinit_state(struct state * s) { - if(s->file) fclose(s->file); + if(s->fd > 0) close(s->fd); if(s->text) text_free(s->text); } @@ -50,14 +73,16 @@ update_status(struct state * s) const size_t cmd_len = strlen(SOMEBAR_CMD), msg_len = TEXT_SIZE + cmd_len + 3; + + size_t actual_len; + char msg[msg_len]; text_update(s->text); + snprintf(msg, msg_len, "%s %s\n", SOMEBAR_CMD, text_get_val(s->text)); - snprintf(msg, msg_len, "%s %s\n", SOMEBAR_CMD, s->text->value); - - fputs(msg, s->file); - fflush(s->file); + actual_len = strlen(msg); + write(s->fd, msg, actual_len); } static void |
