1 /* $NetBSD: fgetln.c,v 1.7 1998/11/15 17:19:53 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)fgetline.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 __RCSID("$NetBSD: fgetln.c,v 1.7 1998/11/15 17:19:53 christos Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include "namespace.h" 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include "local.h" 53 #include "reentrant.h" 54 55 #ifdef __weak_alias 56 __weak_alias(fgetln,_fgetln); 57 #endif 58 59 int __slbexpand __P((FILE *, size_t)); 60 61 /* 62 * Expand the line buffer. Return -1 on error. 63 #ifdef notdef 64 * The `new size' does not account for a terminating '\0', 65 * so we add 1 here. 66 #endif 67 */ 68 int 69 __slbexpand(fp, newsize) 70 FILE *fp; 71 size_t newsize; 72 { 73 void *p; 74 75 #ifdef notdef 76 ++newsize; 77 #endif 78 if (fp->_lb._size >= newsize) 79 return (0); 80 if ((p = realloc(fp->_lb._base, newsize)) == NULL) 81 return (-1); 82 fp->_lb._base = p; 83 fp->_lb._size = newsize; 84 return (0); 85 } 86 87 /* 88 * Get an input line. The returned pointer often (but not always) 89 * points into a stdio buffer. Fgetline does not alter the text of 90 * the returned line (which is thus not a C string because it will 91 * not necessarily end with '\0'), but does allow callers to modify 92 * it if they wish. Thus, we set __SMOD in case the caller does. 93 */ 94 char * 95 fgetln(fp, lenp) 96 FILE *fp; 97 size_t *lenp; 98 { 99 unsigned char *p; 100 size_t len; 101 size_t off; 102 103 FLOCKFILE(fp); 104 105 /* make sure there is input */ 106 if (fp->_r <= 0 && __srefill(fp)) { 107 *lenp = 0; 108 FUNLOCKFILE(fp); 109 return (NULL); 110 } 111 112 /* look for a newline in the input */ 113 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) { 114 char *ret; 115 116 /* 117 * Found one. Flag buffer as modified to keep fseek from 118 * `optimising' a backward seek, in case the user stomps on 119 * the text. 120 */ 121 p++; /* advance over it */ 122 ret = (char *)fp->_p; 123 *lenp = len = p - fp->_p; 124 fp->_flags |= __SMOD; 125 fp->_r -= len; 126 fp->_p = p; 127 FUNLOCKFILE(fp); 128 return (ret); 129 } 130 131 /* 132 * We have to copy the current buffered data to the line buffer. 133 * As a bonus, though, we can leave off the __SMOD. 134 * 135 * OPTIMISTIC is length that we (optimistically) expect will 136 * accomodate the `rest' of the string, on each trip through the 137 * loop below. 138 */ 139 #define OPTIMISTIC 80 140 141 for (len = fp->_r, off = 0;; len += fp->_r) { 142 size_t diff; 143 144 /* 145 * Make sure there is room for more bytes. Copy data from 146 * file buffer to line buffer, refill file and look for 147 * newline. The loop stops only when we find a newline. 148 */ 149 if (__slbexpand(fp, len + OPTIMISTIC)) 150 goto error; 151 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 152 len - off); 153 off = len; 154 if (__srefill(fp)) 155 break; /* EOF or error: return partial line */ 156 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL) 157 continue; 158 159 /* got it: finish up the line (like code above) */ 160 p++; 161 diff = p - fp->_p; 162 len += diff; 163 if (__slbexpand(fp, len)) 164 goto error; 165 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 166 diff); 167 fp->_r -= diff; 168 fp->_p = p; 169 break; 170 } 171 *lenp = len; 172 #ifdef notdef 173 fp->_lb._base[len] = 0; 174 #endif 175 FUNLOCKFILE(fp); 176 return ((char *)fp->_lb._base); 177 178 error: 179 *lenp = 0; /* ??? */ 180 FUNLOCKFILE(fp); 181 return (NULL); /* ??? */ 182 } 183