j5

aditionnal tools for the s6 service manager
Log | Files | Refs | README

commit 65b58aa716b629650d6f1a25699e6184168ec0c6
parent 88497203a33f3f7620e3b128cb7a99849fedc58a
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sat, 26 Jan 2019 23:18:07 +0100

adjust openreadclose api

Diffstat:
MMakefile | 7+++----
Mj5-envdir.c | 13+++++++------
Aopen.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mopen.h | 4++++
Dopenreadclose.c | 28----------------------------
Dopenreadclose.h | 8--------
Dopenwriteclose.c | 27---------------------------
Dopenwriteclose.h | 8--------
8 files changed, 66 insertions(+), 81 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ bin = j5-envdir -obj = openwriteclose.o openreadclose.o buffer.o fmt.o stat.o hier.o log.o mem.o str.o stralloc.o +obj = open.o buffer.o fmt.o stat.o hier.o log.o mem.o str.o stralloc.o .PHONY: all all: ${bin} @@ -22,11 +22,10 @@ j5-envdir: j5-envdir.o ${obj} buffer.o: buffer.c buffer.h mem.h stralloc.h fmt.o: fmt.c fmt.h str.h hier.o: hier.c hier.h open.h str.h stralloc.h -j5-envdir.o: j5-envdir.c openreadclose.h openwriteclose.h log.h str.h mem.h arg.h stat.h stralloc.h genalloc.h +j5-envdir.o: j5-envdir.c open.h log.h str.h mem.h arg.h stat.h stralloc.h genalloc.h log.o: log.c buffer.h log.h mem.o: mem.c mem.h -openreadclose.o: openreadclose.c stralloc.h open.h -openwriteclose.o: openwriteclose.c stralloc.h open.h +open.o: open.c open.h stat.o: stat.c stat.h str.o: str.c str.h stralloc.o: stralloc.c mem.h fmt.h str.h stralloc.h diff --git a/j5-envdir.c b/j5-envdir.c @@ -2,8 +2,7 @@ #include <dirent.h> #include <unistd.h> -#include "openreadclose.h" -#include "openwriteclose.h" +#include "open.h" #include "log.h" #include "str.h" #include "mem.h" @@ -109,8 +108,8 @@ main(int argc, char **argv) stralloc_cats(&sa, de->d_name); stralloc_cat0(&sa); if (!stat_isfile(sa.x)) continue; - if (!openreadclose(sa.x, &a.val, 256)) - log_fatal_sys_2(100, "openreadclose ", sa.x); + if (!open_readclose(sa.x, &a.val, 256)) + log_fatal_sys_2(100, "open read close ", sa.x); a.key = de->d_name; chomp(&a.val, flag_n); if (!genalloc_append(&ga, a)) die_nomem(); @@ -129,9 +128,11 @@ main(int argc, char **argv) if (flag_f) { stralloc src = STRALLOC_ZERO; stralloc dst = STRALLOC_ZERO; - if (!openreadclose(flag_f, &src, 1024)) log_fatal_2(102, "open read close ", flag_f); + if (!open_readclose(flag_f, &src, 1024)) + log_fatal_sys_2(102, "open read close ", flag_f); if (!replace(&dst, src.x, src.n, pre, &ga, suf)) die_nomem(); - if (!openwriteclose(flag_f, dst.x, dst.n)) log_fatal_2(102, "open write close ", flag_f); + if (!open_writeclose(flag_f, dst.x, dst.n)) + log_fatal_sys_2(102, "open write close ", flag_f); } for (size_t i = 0; i < genalloc_len(&ga, keyval); ++i) { diff --git a/open.c b/open.c @@ -0,0 +1,52 @@ +#include <errno.h> +#include <unistd.h> +#include "open.h" +#include "stralloc.h" + +int +open_writeclose(char const *path, char const *s, size_t n) +{ + ssize_t w; + int fd = open_write(path); + + if (fd == -1) return 0; + do { + w = write(fd, s, n); + if (w == -1) { + if (errno == EINTR) continue; + if (errno == EAGAIN) continue; + if (errno == EWOULDBLOCK) continue; + close(fd); + return 0; + } + s += w; + n -= w; + } while (n > 0); + + close(fd); + return 1; +} + +int +open_readclose(char const *path, stralloc *sa, size_t bufsize) +{ + ssize_t r; + int fd = open_read(path); + + if (fd == -1) return 0; + do { + if (!stralloc_readyplus(sa, bufsize)) { close(fd); return 0; } + r = read(fd, sa->x + sa->n, bufsize); + if (r == -1) { + if (errno == EINTR) continue; + if (errno == EAGAIN) continue; + if (errno == EWOULDBLOCK) continue; + close(fd); + return 0; + } + sa->n += r; + } while (r != 0); + + close(fd); + return 1; +} diff --git a/open.h b/open.h @@ -2,6 +2,7 @@ #define OPEN_H #include <fcntl.h> +#include "stralloc.h" #define open_append(path) open(path, O_WRONLY | O_NONBLOCK | O_APPEND | O_CREAT, 0600) #define open_excl(path) open(path, O_WRONLY | O_EXCL | O_CREAT, 0600) @@ -9,4 +10,7 @@ #define open_trunc(path) open(path, O_WRONLY | O_NONBLOCK | O_TRUNC | O_CREAT, 0600) #define open_write(path) open(path, O_WRONLY | O_NDELAY) +int open_readclose(char const *, stralloc *, size_t); +int open_writeclose(char const *, char const *, size_t); + #endif diff --git a/openreadclose.c b/openreadclose.c @@ -1,28 +0,0 @@ -#include <errno.h> -#include <unistd.h> -#include "stralloc.h" -#include "open.h" - -int -openreadclose(char const *path, stralloc *sa, size_t bufsize) -{ - ssize_t r; - int fd = open_read(path); - - if (fd == -1) return 0; - do { - if (!stralloc_readyplus(sa, bufsize)) { close(fd); return 0; } - r = read(fd, sa->x + sa->n, bufsize); - if (r == -1) { - if (errno == EINTR) continue; - if (errno == EAGAIN) continue; - if (errno == EWOULDBLOCK) continue; - close(fd); - return 0; - } - sa->n += r; - } while (r != 0); - - close(fd); - return 1; -} diff --git a/openreadclose.h b/openreadclose.h @@ -1,8 +0,0 @@ -#ifndef OPENREADCLOSE_H -#define OPENREADCLOSE_H - -#include "stralloc.h" - -int openreadclose(char const *, stralloc *, size_t); - -#endif diff --git a/openwriteclose.c b/openwriteclose.c @@ -1,27 +0,0 @@ -#include <errno.h> -#include <unistd.h> -#include "open.h" - -int -openwriteclose(char const *path, char const *s, size_t n) -{ - ssize_t w; - int fd = open_write(path); - - if (fd == -1) return 0; - do { - w = write(fd, s, n); - if (w == -1) { - if (errno == EINTR) continue; - if (errno == EAGAIN) continue; - if (errno == EWOULDBLOCK) continue; - close(fd); - return 0; - } - s += w; - n -= w; - } while (n > 0); - - close(fd); - return 1; -} diff --git a/openwriteclose.h b/openwriteclose.h @@ -1,8 +0,0 @@ -#ifndef OPENWRITECLOSE_H -#define OPENWRITECLOSE_H - -#include "stralloc.h" - -int openwriteclose(char const *, char const *, size_t); - -#endif