xref: /netbsd-src/lib/libc/stdio/fgets.c (revision 526d942790cc352b6ea7931f9884cc6bdbf20404)
1*526d9427Schristos /*	$NetBSD: fgets.c,v 1.28 2012/03/15 18:22:30 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4255db7b2Sjtc  * Copyright (c) 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
3523312f88Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)fgets.c	8.2 (Berkeley) 12/22/93";
3923312f88Schristos #else
40*526d9427Schristos __RCSID("$NetBSD: fgets.c,v 1.28 2012/03/15 18:22:30 christos Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
44b48252f3Slukem #include <assert.h>
4561f28255Scgd #include <stdio.h>
46695bc799Sdsl #include <errno.h>
4761f28255Scgd #include <string.h>
48da2013acSjtc #include "reentrant.h"
493fdac2b8Sthorpej #include "local.h"
5014183450Schristos #ifdef _FORTIFY_SOURCE
5114183450Schristos #undef fgets
5214183450Schristos #endif
5361f28255Scgd 
5461f28255Scgd /*
5561f28255Scgd  * Read at most n-1 characters from the given file.
5661f28255Scgd  * Stop when a newline has been read, or the count runs out.
5761f28255Scgd  * Return first argument, or NULL if no characters were read.
5861f28255Scgd  */
5961f28255Scgd char *
fgets(char * buf,int n,FILE * fp)60*526d9427Schristos fgets(char *buf, int n, FILE *fp)
6161f28255Scgd {
62695bc799Sdsl 	int len;
63c8bafd62Sperry 	char *s;
64c8bafd62Sperry 	unsigned char *p, *t;
6561f28255Scgd 
66b48252f3Slukem 	_DIAGASSERT(buf != NULL);
67b48252f3Slukem 	_DIAGASSERT(fp != NULL);
6861f28255Scgd 
69da2013acSjtc 	FLOCKFILE(fp);
7017f3654aSyamt 	_SET_ORIENTATION(fp, -1);
7161f28255Scgd 	s = buf;
7261f28255Scgd 	n--;			/* leave space for NUL */
7301a0f8eaSdsl 	do {
7461f28255Scgd 		/*
7561f28255Scgd 		 * If the buffer is empty, refill it.
7661f28255Scgd 		 */
771bb83f4aSitojun 		if (fp->_r <= 0) {
7861f28255Scgd 			if (__srefill(fp)) {
7961f28255Scgd 				/* EOF/error: stop with partial or no line */
80caa80a1eSkleink 				if (s == buf) {
81caa80a1eSkleink 					FUNLOCKFILE(fp);
8261f28255Scgd 					return (NULL);
83caa80a1eSkleink 				}
8461f28255Scgd 				break;
8561f28255Scgd 			}
8661f28255Scgd 		}
871bb83f4aSitojun 		len = fp->_r;
8861f28255Scgd 		p = fp->_p;
8961f28255Scgd 
9061f28255Scgd 		/*
9161f28255Scgd 		 * Scan through at most n bytes of the current buffer,
9261f28255Scgd 		 * looking for '\n'.  If found, copy up to and including
9361f28255Scgd 		 * newline, and stop.  Otherwise, copy entire chunk
9461f28255Scgd 		 * and loop.
9561f28255Scgd 		 */
96695bc799Sdsl 		if (len > n) {
97695bc799Sdsl 			if (n < 0) {
98695bc799Sdsl 				/*
99695bc799Sdsl 				 * Caller's length <= 0
100695bc799Sdsl 				 * We can't write into the buffer, so cannot
101695bc799Sdsl 				 * return a string, so must return NULL.
102695bc799Sdsl 				 * Set errno and __SERR so it is consistent.
103695bc799Sdsl 				 * TOG gives no indication of what to do here!
104695bc799Sdsl 				 */
105695bc799Sdsl 				errno = EINVAL;
106695bc799Sdsl 				fp->_flags |= __SERR;
107695bc799Sdsl 				FUNLOCKFILE(fp);
108695bc799Sdsl 				return NULL;
109695bc799Sdsl 			}
11061f28255Scgd 			len = n;
111695bc799Sdsl 		}
112cfbb35edSchristos 		t = memchr(p, '\n', (size_t)len);
11361f28255Scgd 		if (t != NULL) {
114695bc799Sdsl 			len = (int)(++t - p);
11561f28255Scgd 			fp->_r -= len;
11661f28255Scgd 			fp->_p = t;
117cfbb35edSchristos 			(void)memcpy(s, p, (size_t)len);
11861f28255Scgd 			s[len] = 0;
1195e5a72ffSexplorer 			FUNLOCKFILE(fp);
120*526d9427Schristos 			return buf;
12161f28255Scgd 		}
12261f28255Scgd 		fp->_r -= len;
12361f28255Scgd 		fp->_p += len;
124cfbb35edSchristos 		(void)memcpy(s, p, (size_t)len);
12561f28255Scgd 		s += len;
126255db7b2Sjtc 		n -= len;
12701a0f8eaSdsl 	} while (n != 0);
12861f28255Scgd 	*s = 0;
129da2013acSjtc 	FUNLOCKFILE(fp);
130*526d9427Schristos 	return buf;
13161f28255Scgd }
132