diff options
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/openslurpclose.c | 8 | ||||
-rw-r--r-- | src/libstddjb/openslurpnclose.c | 13 | ||||
-rw-r--r-- | src/libstddjb/slurp.c | 26 | ||||
-rw-r--r-- | src/libstddjb/slurpn.c | 35 |
4 files changed, 50 insertions, 32 deletions
diff --git a/src/libstddjb/openslurpclose.c b/src/libstddjb/openslurpclose.c index 8485413..e6b0570 100644 --- a/src/libstddjb/openslurpclose.c +++ b/src/libstddjb/openslurpclose.c @@ -1,14 +1,8 @@ /* ISC license. */ -#include <skalibs/stralloc.h> #include <skalibs/djbunix.h> int openslurpclose (stralloc *sa, char const *fn) { - int r ; - int fd = openbc_read(fn) ; - if (fd == -1) return 0 ; - r = slurp(sa, fd) ; - fd_close(fd) ; - return r ; + return openslurpnclose(fn, sa, 0) ; } diff --git a/src/libstddjb/openslurpnclose.c b/src/libstddjb/openslurpnclose.c new file mode 100644 index 0000000..3672e16 --- /dev/null +++ b/src/libstddjb/openslurpnclose.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/djbunix.h> + +int openslurpnclose (char const *fn, stralloc *sa, size_t max) +{ + int r ; + int fd = openbc_read(fn) ; + if (fd == -1) return 0 ; + r = slurpn(fd, sa, max) ; + fd_close(fd) ; + return r ; +} diff --git a/src/libstddjb/slurp.c b/src/libstddjb/slurp.c index 7437142..eccd833 100644 --- a/src/libstddjb/slurp.c +++ b/src/libstddjb/slurp.c @@ -1,32 +1,8 @@ /* ISC license. */ -#include <sys/types.h> - -#include <skalibs/allreadwrite.h> -#include <skalibs/stralloc.h> #include <skalibs/djbunix.h> -#define N 4096 - int slurp (stralloc *sa, int fd) { - size_t sabase = sa->len ; - int wasnull = !sa->s ; - for (;;) - { - ssize_t r ; - if (!stralloc_readyplus(sa, N)) break ; - r = fd_read(fd, sa->s + sa->len, N) ; - switch (r) - { - case -1 : goto err ; - case 0 : return 1 ; - default : sa->len += r ; - } - } - -err: - if (wasnull) stralloc_free(sa) ; - else sa->len = sabase ; - return 0 ; + return slurpn(fd, sa, 0) ; } diff --git a/src/libstddjb/slurpn.c b/src/libstddjb/slurpn.c new file mode 100644 index 0000000..c9c5c11 --- /dev/null +++ b/src/libstddjb/slurpn.c @@ -0,0 +1,35 @@ +/* ISC license. */ + +#include <sys/types.h> +#include <errno.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> + +#define N 4096 + +int slurpn (int fd, stralloc *sa, size_t max) +{ + size_t sabase = sa->len ; + int wasnull = !sa->s ; + for (;;) + { + ssize_t r ; + size_t n = max && sa->len + N > max ? max - sa->len : N ; + if (!n) { errno = ENOBUFS ; goto err ; } + if (!stralloc_readyplus(sa, n)) break ; + r = fd_read(fd, sa->s + sa->len, n) ; + switch (r) + { + case -1 : goto err ; + case 0 : return 1 ; + default : sa->len += r ; + } + } + +err: + if (wasnull) stralloc_free(sa) ; + else sa->len = sabase ; + return 0 ; +} |