skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit c4e81d83b248aa356849fc6a31920c177b5df999
parent 6d419ff3e5a0e645e8fe9635d6ca139d5b7e3e7a
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Wed,  8 Jun 2022 09:48:11 +0000

 Clean up / homogenize openreadnclose() family

Signed-off-by: Laurent Bercot <ska@appnovation.com>

Diffstat:
MNEWS | 4+++-
Mdoc/libstddjb/djbunix.html | 2+-
Mpackage/deps.mak | 6++++--
Msrc/include/skalibs/djbunix.h | 1+
Msrc/include/skalibs/unix-transactional.h | 2+-
Msrc/libstddjb/openreadnclose.c | 26+-------------------------
Asrc/libstddjb/openreadnclose_nb.c | 9+++++++++
Asrc/libstddjb/readnclose.c | 18++++++++++++++++++
Msrc/libunixonacid/openreadnclose_at.c | 14++------------
9 files changed, 40 insertions(+), 42 deletions(-)

diff --git 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 @@ -522,7 +522,7 @@ Returns 1 if it succeeds, and 0 (and sets errno) if it fails. <code> ssize_t openreadnclose (char const *file, char *s, size_t n) </code> <br /> Reads at most <em>n</em> bytes from file <em>file</em> into preallocated buffer <em>s</em>. Returns -1 (and sets errno) if it fails; else returns the -number of read bytes. If that number is not <em>n</em>, errno is set to EPIPE. +number of read bytes. </p> <p> diff --git 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 @@ -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 @@ -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 @@ -1,33 +1,9 @@ /* ISC license. */ -#include <sys/types.h> -#include <errno.h> -#include <skalibs/allreadwrite.h> #include <skalibs/djbunix.h> -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 @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include <skalibs/djbunix.h> + +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 @@ -0,0 +1,18 @@ +/* ISC license. */ + +#include <errno.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/djbunix.h> + +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 @@ -1,20 +1,10 @@ /* ISC license. */ -#include <errno.h> - -#include <skalibs/allreadwrite.h> #include <skalibs/djbunix.h> #include <skalibs/unix-transactional.h> -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) ; }