/* This file is part of limb https://lila.oss/limb
* Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/allreadwrite.h>
#include <limb/djbunix.h>
ssize_t
open_read_closeat(char *dst, size_t dlen, int bfd, const char *file)
{
int fd = openb_readat(bfd, file);
if (fd < 0) return -1;
errno = 0;
ssize_t r = allread(fd, dst, dlen);
if (errno) goto err;
if ((size_t) r == dlen) {
char c;
ssize_t rr = fd_read(fd, &c, 1);
if (rr < 0) r = rr;
else if (rr == 1) (errno = ENOBUFS, r = -1);
}
err:
fd_close(fd);
return r;
}