/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <errno.h>36#include <limits.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include "un-namespace.h"41#include "libc_private.h"42#include "local.h"4344/*45* Expand the line buffer. Return -1 on error.46*/47int48__slbexpand(FILE *fp, size_t newsize)49{50void *p;5152if (fp->_lb._size >= newsize)53return (0);54if (newsize > INT_MAX) {55errno = ENOMEM;56return (-1);57}58if ((p = realloc(fp->_lb._base, newsize)) == NULL)59return (-1);60fp->_lb._base = p;61fp->_lb._size = newsize;62return (0);63}6465/*66* Get an input line. The returned pointer often (but not always)67* points into a stdio buffer. Fgetln does not alter the text of68* the returned line (which is thus not a C string because it will69* not necessarily end with '\0'), but does allow callers to modify70* it if they wish. Thus, we set __SMOD in case the caller does.71*/72char *73fgetln(FILE *fp, size_t *lenp)74{75unsigned char *p;76char *ret;77size_t len;78size_t off;7980FLOCKFILE_CANCELSAFE(fp);81ORIENT(fp, -1);82/* make sure there is input */83if (fp->_r <= 0 && __srefill(fp)) {84*lenp = 0;85ret = NULL;86goto end;87}8889/* look for a newline in the input */90if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) {91/*92* Found one. Flag buffer as modified to keep fseek from93* `optimising' a backward seek, in case the user stomps on94* the text.95*/96p++; /* advance over it */97ret = (char *)fp->_p;98*lenp = len = p - fp->_p;99fp->_flags |= __SMOD;100fp->_r -= len;101fp->_p = p;102goto end;103}104105/*106* We have to copy the current buffered data to the line buffer.107* As a bonus, though, we can leave off the __SMOD.108*109* OPTIMISTIC is length that we (optimistically) expect will110* accommodate the `rest' of the string, on each trip through the111* loop below.112*/113#define OPTIMISTIC 80114115for (len = fp->_r, off = 0;; len += fp->_r) {116size_t diff;117118/*119* Make sure there is room for more bytes. Copy data from120* file buffer to line buffer, refill file and look for121* newline. The loop stops only when we find a newline.122*/123if (__slbexpand(fp, len + OPTIMISTIC))124goto error;125(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,126len - off);127off = len;128if (__srefill(fp)) {129if (__sfeof(fp))130break;131goto error;132}133if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL)134continue;135136/* got it: finish up the line (like code above) */137p++;138diff = p - fp->_p;139len += diff;140if (__slbexpand(fp, len))141goto error;142(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,143diff);144fp->_r -= diff;145fp->_p = p;146break;147}148*lenp = len;149ret = (char *)fp->_lb._base;150end:151FUNLOCKFILE_CANCELSAFE();152return (ret);153154error:155*lenp = 0; /* ??? */156fp->_flags |= __SERR;157ret = NULL;158goto end;159}160161162