summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/openreadnclose.c26
-rw-r--r--src/libstddjb/openreadnclose_nb.c9
-rw-r--r--src/libstddjb/readnclose.c18
3 files changed, 28 insertions, 25 deletions
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 ;
+}