diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/djbunix.h | 1 | ||||
-rw-r--r-- | src/include/skalibs/unix-transactional.h | 2 | ||||
-rw-r--r-- | src/libstddjb/openreadnclose.c | 26 | ||||
-rw-r--r-- | src/libstddjb/openreadnclose_nb.c | 9 | ||||
-rw-r--r-- | src/libstddjb/readnclose.c | 18 | ||||
-rw-r--r-- | src/libunixonacid/openreadnclose_at.c | 14 |
6 files changed, 32 insertions, 38 deletions
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 <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 new file mode 100644 index 0000000..5f6e046 --- /dev/null +++ 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 new file mode 100644 index 0000000..28a3acb --- /dev/null +++ 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 index 8100997..4858a69 100644 --- 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) ; } |