Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / readopt.h / readopt.c

/* 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/bytestr.h>
#include <limb/djbunix.h>
#include <limb/readopt.h>

ssize_t
readopt(char *dst, size_t dlen, int bfd, const char *name)
{
    ssize_t r;
    r = open_read_closeat(dst, dlen, bfd, name);
    if (r < 0 && errno != ENOBUFS) return -1;

    if (r < 0) {
        r = byte_chr(dst, dlen, '\n');
        if ((size_t) r == dlen) return -1;
    } else {
        r = byte_chr(dst, r, '\n');
    }

    return r;
}