From c4e81d83b248aa356849fc6a31920c177b5df999 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 8 Jun 2022 09:48:11 +0000 Subject: Clean up / homogenize openreadnclose() family Signed-off-by: Laurent Bercot --- NEWS | 4 +++- doc/libstddjb/djbunix.html | 2 +- package/deps.mak | 6 ++++-- src/include/skalibs/djbunix.h | 1 + src/include/skalibs/unix-transactional.h | 2 +- src/libstddjb/openreadnclose.c | 26 +------------------------- src/libstddjb/openreadnclose_nb.c | 9 +++++++++ src/libstddjb/readnclose.c | 18 ++++++++++++++++++ src/libunixonacid/openreadnclose_at.c | 14 ++------------ 9 files changed, 40 insertions(+), 42 deletions(-) create mode 100644 src/libstddjb/openreadnclose_nb.c create mode 100644 src/libstddjb/readnclose.c diff --git a/NEWS b/NEWS index 203c08b..7833fd7 100644 --- a/NEWS +++ b/NEWS @@ -8,7 +8,9 @@ In 2.12.0.0 * random_char() removed. * random_string() renamed to random_buf(). * early versions added. - - New djbunix functions: waitn_posix(), waitn_reap_posix() + - New djbunix functions: waitn_posix(), waitn_reap_posix(). + - readnclose() made public, now doesn't touch errno on success. + - openreadnclose_at() now returns a ssize_t. In 2.11.2.0 diff --git a/doc/libstddjb/djbunix.html b/doc/libstddjb/djbunix.html index d1470b4..7c90c8a 100644 --- a/doc/libstddjb/djbunix.html +++ b/doc/libstddjb/djbunix.html @@ -522,7 +522,7 @@ Returns 1 if it succeeds, and 0 (and sets errno) if it fails. ssize_t openreadnclose (char const *file, char *s, size_t n)
Reads at most n bytes from file file into preallocated buffer s. Returns -1 (and sets errno) if it fails; else returns the -number of read bytes. If that number is not n, errno is set to EPIPE. +number of read bytes.

diff --git a/package/deps.mak b/package/deps.mak index 9b94a52..0faa1df 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -437,7 +437,8 @@ src/libstddjb/openc_readb.o src/libstddjb/openc_readb.lo: src/libstddjb/openc_re src/libstddjb/openc_trunc.o src/libstddjb/openc_trunc.lo: src/libstddjb/openc_trunc.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/libstddjb/openc_write.o src/libstddjb/openc_write.lo: src/libstddjb/openc_write.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/libstddjb/openreadfileclose.o src/libstddjb/openreadfileclose.lo: src/libstddjb/openreadfileclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h -src/libstddjb/openreadnclose.o src/libstddjb/openreadnclose.lo: src/libstddjb/openreadnclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h +src/libstddjb/openreadnclose.o src/libstddjb/openreadnclose.lo: src/libstddjb/openreadnclose.c src/include/skalibs/djbunix.h +src/libstddjb/openreadnclose_nb.o src/libstddjb/openreadnclose_nb.lo: src/libstddjb/openreadnclose_nb.c src/include/skalibs/djbunix.h src/libstddjb/openslurpclose.o src/libstddjb/openslurpclose.lo: src/libstddjb/openslurpclose.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h src/libstddjb/openwritenclose.o src/libstddjb/openwritenclose.lo: src/libstddjb/openwritenclose.c src/include/skalibs/djbunix.h src/libstddjb/openwritenclose_suffix.o src/libstddjb/openwritenclose_suffix.lo: src/libstddjb/openwritenclose_suffix.c src/include/skalibs/djbunix.h @@ -448,6 +449,7 @@ src/libstddjb/openwritevnclose_unsafe.o src/libstddjb/openwritevnclose_unsafe.lo src/libstddjb/path_canonicalize.o src/libstddjb/path_canonicalize.lo: src/libstddjb/path_canonicalize.c src/include/skalibs/djbunix.h src/libstddjb/pipe_internal.o src/libstddjb/pipe_internal.lo: src/libstddjb/pipe_internal.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/libstddjb/prog.o src/libstddjb/prog.lo: src/libstddjb/prog.c src/include/skalibs/strerr2.h +src/libstddjb/readnclose.o src/libstddjb/readnclose.lo: src/libstddjb/readnclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/libstddjb/rm_rf.o src/libstddjb/rm_rf.lo: src/libstddjb/rm_rf.c src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/libstddjb/rm_rf_in_tmp.o src/libstddjb/rm_rf_in_tmp.lo: src/libstddjb/rm_rf_in_tmp.c src/include/skalibs/direntry.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h src/libstddjb/rm_rf_tmp.o src/libstddjb/rm_rf_tmp.lo: src/libstddjb/rm_rf_tmp.c src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h @@ -735,7 +737,7 @@ src/libunixonacid/openc_writeatb.o src/libunixonacid/openc_writeatb.lo: src/libu src/libunixonacid/opendir_at.o src/libunixonacid/opendir_at.lo: src/libunixonacid/opendir_at.c src/include/skalibs/direntry.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/unix-transactional.h src/libunixonacid/opengetlnclose.o src/libunixonacid/opengetlnclose.lo: src/libunixonacid/opengetlnclose.c src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/include/skalibs/unix-transactional.h src/libunixonacid/opengetlnclose_at.o src/libunixonacid/opengetlnclose_at.lo: src/libunixonacid/opengetlnclose_at.c src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/djbunix.h src/include/skalibs/skamisc.h src/include/skalibs/unix-transactional.h -src/libunixonacid/openreadnclose_at.o src/libunixonacid/openreadnclose_at.lo: src/libunixonacid/openreadnclose_at.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/unix-transactional.h +src/libunixonacid/openreadnclose_at.o src/libunixonacid/openreadnclose_at.lo: src/libunixonacid/openreadnclose_at.c src/include/skalibs/djbunix.h src/include/skalibs/unix-transactional.h src/libunixonacid/openslurpclose_at.o src/libunixonacid/openslurpclose_at.lo: src/libunixonacid/openslurpclose_at.c src/include/skalibs/djbunix.h src/include/skalibs/unix-transactional.h src/libunixonacid/openwritenclose_at.o src/libunixonacid/openwritenclose_at.lo: src/libunixonacid/openwritenclose_at.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/unix-transactional.h src/libunixonacid/openwritevnclose_at.o src/libunixonacid/openwritevnclose_at.lo: src/libunixonacid/openwritevnclose_at.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/siovec.h src/include/skalibs/unix-transactional.h diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h index dec232c..449368f 100644 --- a/src/include/skalibs/djbunix.h +++ b/src/include/skalibs/djbunix.h @@ -84,6 +84,7 @@ extern int sagethostname (stralloc *) ; extern int slurp (stralloc *, int) ; extern int openslurpclose (stralloc *, char const *) ; +extern ssize_t readnclose (int fd, char *, size_t) ; /* closes fd */ extern ssize_t openreadnclose (char const *, char *, size_t) ; extern ssize_t openreadnclose_nb (char const *, char *, size_t) ; extern int openreadfileclose (char const *, stralloc *, size_t) ; diff --git a/src/include/skalibs/unix-transactional.h b/src/include/skalibs/unix-transactional.h index 5b9c4fa..73a8d7f 100644 --- a/src/include/skalibs/unix-transactional.h +++ b/src/include/skalibs/unix-transactional.h @@ -38,7 +38,7 @@ extern int openc_truncatb (int, char const *) ; extern int openc_appendat (int, char const *) ; extern int openc_appendatb (int, char const *) ; -extern size_t openreadnclose_at (int, char const *, char *, size_t) ; +extern ssize_t openreadnclose_at (int, char const *, char *, size_t) ; extern int openslurpclose_at (int, char const *, stralloc *) ; extern int opengetlnclose_at (int, char const *, stralloc *, int) ; diff --git a/src/libstddjb/openreadnclose.c b/src/libstddjb/openreadnclose.c index 4466796..5f39c43 100644 --- a/src/libstddjb/openreadnclose.c +++ b/src/libstddjb/openreadnclose.c @@ -1,33 +1,9 @@ /* ISC license. */ -#include -#include -#include #include -static ssize_t readnclose (int fd, char *s, size_t n) -{ - size_t r ; - errno = 0 ; - r = allread(fd, s, n) ; - if (errno) - { - fd_close(fd) ; - return -1 ; - } - fd_close(fd) ; - if (r < n) errno = EPIPE ; - return r ; -} - ssize_t openreadnclose (char const *file, char *s, size_t n) { int fd = openbc_read(file) ; - return fd < 0 ? fd : readnclose(fd, s, n) ; -} - -ssize_t openreadnclose_nb (char const *file, char *s, size_t n) -{ - int fd = openc_read(file) ; - return fd < 0 ? fd : readnclose(fd, s, n) ; + return fd == -1 ? fd : readnclose(fd, s, n) ; } diff --git a/src/libstddjb/openreadnclose_nb.c b/src/libstddjb/openreadnclose_nb.c new file mode 100644 index 0000000..5f6e046 --- /dev/null +++ b/src/libstddjb/openreadnclose_nb.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include + +ssize_t openreadnclose_nb (char const *file, char *s, size_t n) +{ + int fd = openc_read(file) ; + return fd == -1 ? fd : readnclose(fd, s, n) ; +} diff --git a/src/libstddjb/readnclose.c b/src/libstddjb/readnclose.c new file mode 100644 index 0000000..28a3acb --- /dev/null +++ b/src/libstddjb/readnclose.c @@ -0,0 +1,18 @@ +/* ISC license. */ + +#include + +#include +#include + +ssize_t readnclose (int fd, char *s, size_t n) +{ + int e = errno ; + size_t r ; + errno = 0 ; + r = allread(fd, s, n) ; + fd_close(fd) ; + if (errno) return -1 ; + errno = e ; + return r ; +} diff --git a/src/libunixonacid/openreadnclose_at.c b/src/libunixonacid/openreadnclose_at.c index 8100997..4858a69 100644 --- a/src/libunixonacid/openreadnclose_at.c +++ b/src/libunixonacid/openreadnclose_at.c @@ -1,20 +1,10 @@ /* ISC license. */ -#include - -#include #include #include -size_t openreadnclose_at (int dirfd, char const *file, char *s, size_t n) +ssize_t openreadnclose_at (int dirfd, char const *file, char *s, size_t n) { - size_t r ; - int e ; int fd = openc_readatb(dirfd, file) ; - if (fd < 0) return 0 ; - r = allread(fd, s, n) ; - e = errno ; - fd_close(fd) ; - errno = e ; - return r ; + return fd == -1 ? -1 : readnclose(fd, s, n) ; } -- cgit v1.2.3