skalibs

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

commit 2af587d68d11b1826c356364b4e9679199eee2ed
parent 8ef88c7d9a7108409c1e58c21b3d0590c859384f
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Thu, 18 May 2023 02:19:59 +0000

 Add slurpn, refactor slurp, fix avltree/cdbmake bugs, add strerr_warnf

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

Diffstat:
MNEWS | 3+++
Mpackage/deps.mak | 6++++--
Msrc/include/skalibs/avltree.h | 2+-
Msrc/include/skalibs/cdbmake.h | 2+-
Msrc/include/skalibs/djbunix.h | 8++++++++
Msrc/include/skalibs/strerr.h | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/libstddjb/openslurpclose.c | 8+-------
Asrc/libstddjb/openslurpnclose.c | 13+++++++++++++
Msrc/libstddjb/slurp.c | 26+-------------------------
Asrc/libstddjb/slurpn.c | 35+++++++++++++++++++++++++++++++++++
10 files changed, 119 insertions(+), 36 deletions(-)

diff --git a/NEWS b/NEWS @@ -3,7 +3,10 @@ Changelog for skalibs. In 2.13.2.0 ----------- + - Bugfixes. - New accessor function: selfpipe_fd(). + - New functions: slurpn(), openslurpnclose(). + - New strerr macros to warn with a "fatal" message In 2.13.1.1 diff --git a/package/deps.mak b/package/deps.mak @@ -438,7 +438,8 @@ src/libstddjb/openc_write.o src/libstddjb/openc_write.lo: src/libstddjb/openc_wr 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/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/openslurpclose.o src/libstddjb/openslurpclose.lo: src/libstddjb/openslurpclose.c src/include/skalibs/djbunix.h +src/libstddjb/openslurpnclose.o src/libstddjb/openslurpnclose.lo: src/libstddjb/openslurpnclose.c src/include/skalibs/djbunix.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 src/libstddjb/openwritenclose_unsafe.o src/libstddjb/openwritenclose_unsafe.lo: src/libstddjb/openwritenclose_unsafe.c src/include/skalibs/djbunix.h @@ -493,7 +494,8 @@ src/libstddjb/skagetlnmaxsep.o src/libstddjb/skagetlnmaxsep.lo: src/libstddjb/sk src/libstddjb/skagetlnsep.o src/libstddjb/skagetlnsep.lo: src/libstddjb/skagetlnsep.c src/include/skalibs/buffer.h src/include/skalibs/siovec.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h src/libstddjb/skagetlnsep_loose.o src/libstddjb/skagetlnsep_loose.lo: src/libstddjb/skagetlnsep_loose.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h src/libstddjb/skalibs_tzisright.o src/libstddjb/skalibs_tzisright.lo: src/libstddjb/skalibs_tzisright.c src/libstddjb/djbtime-internal.h -src/libstddjb/slurp.o src/libstddjb/slurp.lo: src/libstddjb/slurp.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h +src/libstddjb/slurp.o src/libstddjb/slurp.lo: src/libstddjb/slurp.c src/include/skalibs/djbunix.h +src/libstddjb/slurpn.o src/libstddjb/slurpn.lo: src/libstddjb/slurpn.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/stralloc.h src/libstddjb/socket_accept4.o src/libstddjb/socket_accept4.lo: src/libstddjb/socket_accept4.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h src/include/skalibs/sysdeps.h src/include/skalibs/uint16.h src/libstddjb/socket_accept4_u32.o src/libstddjb/socket_accept4_u32.lo: src/libstddjb/socket_accept4_u32.c src/include/skalibs/socket.h src/include/skalibs/uint32.h src/libstddjb/socket_accept6.o src/libstddjb/socket_accept6.lo: src/libstddjb/socket_accept6.c src/include/skalibs/djbunix.h src/include/skalibs/ip46.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h src/include/skalibs/sysdeps.h src/include/skalibs/uint16.h diff --git a/src/include/skalibs/avltree.h b/src/include/skalibs/avltree.h @@ -51,8 +51,8 @@ extern int avltree_insert (avltree *, uint32_t) ; extern int avltree_delete (avltree *, void const *) ; -#define avltree_iter(t, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, p) #define avltree_iter_nocancel(t, cut, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), cut, avltree_root(t), f, p) +#define avltree_iter(t, f, p) avltree_iter_nocancel(t, avltree_totalsize(t), f, p) #define avltree_iter_withcancel(t, f, cancelf, p) avlnode_iter_withcancel(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, cancelf, p) #endif diff --git a/src/include/skalibs/cdbmake.h b/src/include/skalibs/cdbmake.h @@ -22,7 +22,7 @@ struct cdbmaker_s #define CDBMAKER_ZERO { .hplist = GENALLOC_ZERO, .pos = 2048, .b = BUFFER_INIT(&fd_writev, -1, 0, 0) } extern int cdbmake_start (cdbmaker *, int) ; -extern int cdbmake_add (cdbmaker *, char const *, unsigned int, char const *, unsigned int) ; +extern int cdbmake_add (cdbmaker *, char const *, uint32_t, char const *, uint32_t) ; extern int cdbmake_finish (cdbmaker *) ; #endif diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h @@ -84,6 +84,14 @@ extern int sagethostname (stralloc *) ; extern int slurp (stralloc *, int) ; extern int openslurpclose (stralloc *, char const *) ; +/* + TODO: next ABI break: change to +#define slurp(sa, fd) slurpn((fd), (sa), 0) +#define openslurpclose(sa, fn) openslurpnclose((fn), (sa), 0) +*/ + +extern int slurpn (int, stralloc *, size_t) ; +extern int openslurpnclose (char const *, stralloc *, size_t) ; 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) ; diff --git a/src/include/skalibs/strerr.h b/src/include/skalibs/strerr.h @@ -25,6 +25,8 @@ extern char const *PROG ; #define strerr_warnwn(n, ...) strerr_warnn((n)+2, PROG, ": warning: ", __VA_ARGS__) #define strerr_warnwnsys(n, ...) strerr_warnnsys((n)+2, PROG, ": warning: ", __VA_ARGS__) +#define strerr_warnfn(n, ...) strerr_warnn((n)+2, PROG, ": fatal: ", __VA_ARGS__) +#define strerr_warnfnsys(n, ...) strerr_warnnsys((n)+2, PROG, ": fatal: ", __VA_ARGS__) #define strerr_diewn(e, n, ...) strerr_dien(e, (n)+2, PROG, ": warning: ", __VA_ARGS__) #define strerr_diewnsys(e, n, ...) strerr_diensys(e, (n)+2, PROG, ": warning: ", __VA_ARGS__) #define strerr_diefn(e, n, ...) strerr_dien(e, (n)+2, PROG, ": fatal: ", __VA_ARGS__) @@ -32,6 +34,8 @@ extern char const *PROG ; #define strerr_warnw(...) strerr_warn(PROG, ": warning: ", __VA_ARGS__) #define strerr_warnwsys(...) strerr_warnsys(PROG, ": warning: ", __VA_ARGS__) +#define strerr_warnf(...) strerr_warn(PROG, ": fatal: ", __VA_ARGS__) +#define strerr_warnfsys(...) strerr_warnsys(PROG, ": fatal: ", __VA_ARGS__) #define strerr_diew(e, ...) strerr_die(e, PROG, ": warning: ", __VA_ARGS__) #define strerr_diewsys(e, ...) strerr_diesys(e, PROG, ": warning: ", __VA_ARGS__) #define strerr_dief(e, ...) strerr_die(e, PROG, ": fatal: ", __VA_ARGS__) @@ -39,6 +43,8 @@ extern char const *PROG ; #define strerr_warnwun(n, ...) strerr_warnwn((n)+1, "unable to ", __VA_ARGS__) #define strerr_warnwunsys(n, ...) strerr_warnwnsys((n)+1, "unable to ", __VA_ARGS__) +#define strerr_warnfun(n, ...) strerr_warnfn((n)+1, "unable to ", __VA_ARGS__) +#define strerr_warnfunsys(n, ...) strerr_warnfnsys((n)+1, "unable to ", __VA_ARGS__) #define strerr_diewun(e, n, ...) strerr_diewn(e, (n)+1, "unable to ", __VA_ARGS__) #define strerr_diewunsys(e, n, ...) strerr_diewnsys(e, (n)+1, "unable to ", __VA_ARGS__) #define strerr_diefun(e, n, ...) strerr_diefn(e, (n)+1, ": unable to ", __VA_ARGS__) @@ -46,6 +52,8 @@ extern char const *PROG ; #define strerr_warnwu(...) strerr_warnw("unable to ", __VA_ARGS__) #define strerr_warnwusys(...) strerr_warnwsys("unable to ", __VA_ARGS__) +#define strerr_warnfu(...) strerr_warnf("unable to ", __VA_ARGS__) +#define strerr_warnfusys(...) strerr_warnfsys("unable to ", __VA_ARGS__) #define strerr_diewu(e, ...) strerr_diew(e, "unable to ", __VA_ARGS__) #define strerr_diewusys(e, ...) strerr_diewsys(e, "unable to ", __VA_ARGS__) #define strerr_diefu(e, ...) strerr_dief(e, "unable to ", __VA_ARGS__) @@ -91,6 +99,28 @@ extern char const *PROG ; #define strerr_warnw9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnwnsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) #define strerr_warnw10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnwnsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) +#define strerr_warnf1x(x1) strerr_warnfn(1, x1) +#define strerr_warnf2x(x1, x2) strerr_warnfn(2, x1, x2) +#define strerr_warnf3x(x1, x2, x3) strerr_warnfn(3, x1, x2, x3) +#define strerr_warnf4x(x1, x2, x3, x4) strerr_warnfn(4, x1, x2, x3, x4) +#define strerr_warnf5x(x1, x2, x3, x4, x5) strerr_warnfn(5, x1, x2, x3, x4, x5) +#define strerr_warnf6x(x1, x2, x3, x4, x5, x6) strerr_warnfn(6, x1, x2, x3, x4, x5, x6) +#define strerr_warnf7x(x1, x2, x3, x4, x5, x6, x7) strerr_warnfn(7, x1, x2, x3, x4, x5, x6, x7) +#define strerr_warnf8x(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfn(8, x1, x2, x3, x4, x5, x6, x7, x8) +#define strerr_warnf9x(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfn(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) +#define strerr_warnf10x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfn(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + +#define strerr_warnf1sys(x1) strerr_warnfnsys(1, x1) +#define strerr_warnf2sys(x1, x2) strerr_warnfnsys(2, x1, x2) +#define strerr_warnf3sys(x1, x2, x3) strerr_warnfnsys(3, x1, x2, x3) +#define strerr_warnf4sys(x1, x2, x3, x4) strerr_warnfnsys(4, x1, x2, x3, x4) +#define strerr_warnf5sys(x1, x2, x3, x4, x5) strerr_warnfnsys(5, x1, x2, x3, x4, x5) +#define strerr_warnf6sys(x1, x2, x3, x4, x5, x6) strerr_warnfnsys(6, x1, x2, x3, x4, x5, x6) +#define strerr_warnf7sys(x1, x2, x3, x4, x5, x6, x7) strerr_warnfnsys(7, x1, x2, x3, x4, x5, x6, x7) +#define strerr_warnf8sys(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfnsys(8, x1, x2, x3, x4, x5, x6, x7, x8) +#define strerr_warnf9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfnsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) +#define strerr_warnf10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfnsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + #define strerr_diew1x(e, x1) strerr_diewn(e, 1, x1) #define strerr_diew2x(e, x1, x2) strerr_diewn(e, 2, x1, x2) #define strerr_diew3x(e, x1, x2, x3) strerr_diewn(e, 3, x1, x2, x3) @@ -157,6 +187,28 @@ extern char const *PROG ; #define strerr_warnwu9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnwunsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) #define strerr_warnwu10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnwunsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) +#define strerr_warnfu1x(x1) strerr_warnfun(1, x1) +#define strerr_warnfu2x(x1, x2) strerr_warnfun(2, x1, x2) +#define strerr_warnfu3x(x1, x2, x3) strerr_warnfun(3, x1, x2, x3) +#define strerr_warnfu4x(x1, x2, x3, x4) strerr_warnfun(4, x1, x2, x3, x4) +#define strerr_warnfu5x(x1, x2, x3, x4, x5) strerr_warnfun(5, x1, x2, x3, x4, x5) +#define strerr_warnfu6x(x1, x2, x3, x4, x5, x6) strerr_warnfun(6, x1, x2, x3, x4, x5, x6) +#define strerr_warnfu7x(x1, x2, x3, x4, x5, x6, x7) strerr_warnfun(7, x1, x2, x3, x4, x5, x6, x7) +#define strerr_warnfu8x(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfun(8, x1, x2, x3, x4, x5, x6, x7, x8) +#define strerr_warnfu9x(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfun(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) +#define strerr_warnfu10x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfun(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + +#define strerr_warnfu1sys(x1) strerr_warnfunsys(1, x1) +#define strerr_warnfu2sys(x1, x2) strerr_warnfunsys(2, x1, x2) +#define strerr_warnfu3sys(x1, x2, x3) strerr_warnfunsys(3, x1, x2, x3) +#define strerr_warnfu4sys(x1, x2, x3, x4) strerr_warnfunsys(4, x1, x2, x3, x4) +#define strerr_warnfu5sys(x1, x2, x3, x4, x5) strerr_warnfunsys(5, x1, x2, x3, x4, x5) +#define strerr_warnfu6sys(x1, x2, x3, x4, x5, x6) strerr_warnfunsys(6, x1, x2, x3, x4, x5, x6) +#define strerr_warnfu7sys(x1, x2, x3, x4, x5, x6, x7) strerr_warnfunsys(7, x1, x2, x3, x4, x5, x6, x7) +#define strerr_warnfu8sys(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfunsys(8, x1, x2, x3, x4, x5, x6, x7, x8) +#define strerr_warnfu9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfunsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9) +#define strerr_warnfu10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfunsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + #define strerr_diewu1x(e, x1) strerr_diewun(e, 1, x1) #define strerr_diewu2x(e, x1, x2) strerr_diewun(e, 2, x1, x2) #define strerr_diewu3x(e, x1, x2, x3) strerr_diewun(e, 3, x1, x2, x3) diff --git 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 @@ -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 @@ -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 @@ -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 ; +}