xref: /netbsd-src/lib/libc/stdio/fgetstr.c (revision 555c77845c87d9da003293ebef8f491375415b21)
1*555c7784Suwe /* $NetBSD: fgetstr.c,v 1.12 2017/06/08 15:59:45 uwe Exp $	*/
26321f42fSchristos 
386eafd3eSroy /*
486eafd3eSroy  * Copyright (c) 2009 The NetBSD Foundation, Inc.
56321f42fSchristos  *
686eafd3eSroy  * This code is derived from software contributed to The NetBSD Foundation
786eafd3eSroy  * by Roy Marples.
86321f42fSchristos  *
96321f42fSchristos  * Redistribution and use in source and binary forms, with or without
106321f42fSchristos  * modification, are permitted provided that the following conditions
116321f42fSchristos  * are met:
126321f42fSchristos  * 1. Redistributions of source code must retain the above copyright
136321f42fSchristos  *    notice, this list of conditions and the following disclaimer.
146321f42fSchristos  * 2. Redistributions in binary form must reproduce the above copyright
156321f42fSchristos  *    notice, this list of conditions and the following disclaimer in the
166321f42fSchristos  *    documentation and/or other materials provided with the distribution.
176321f42fSchristos  *
1886eafd3eSroy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1986eafd3eSroy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2086eafd3eSroy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2186eafd3eSroy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2286eafd3eSroy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2386eafd3eSroy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2486eafd3eSroy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2586eafd3eSroy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2686eafd3eSroy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2786eafd3eSroy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286321f42fSchristos  */
296321f42fSchristos 
306321f42fSchristos #include <sys/cdefs.h>
31*555c7784Suwe __RCSID("$NetBSD: fgetstr.c,v 1.12 2017/06/08 15:59:45 uwe Exp $");
326321f42fSchristos 
336321f42fSchristos #include "namespace.h"
346321f42fSchristos 
356321f42fSchristos #include <assert.h>
3686eafd3eSroy #include <errno.h>
3786eafd3eSroy #include <limits.h>
386321f42fSchristos #include <stdio.h>
3986eafd3eSroy 
406321f42fSchristos #include "reentrant.h"
416321f42fSchristos #include "local.h"
426321f42fSchristos 
436321f42fSchristos /*
4486eafd3eSroy  * Get an input line.
4586eafd3eSroy  * This now uses getdelim(3) for a code reduction.
46*555c7784Suwe  * The upside is that strings are now always null-terminated, but relying
4786eafd3eSroy  * on this is non portable - better to use the POSIX getdelim(3) function.
486321f42fSchristos  */
496321f42fSchristos char *
__fgetstr(FILE * __restrict fp,size_t * __restrict lenp,int sep)5086eafd3eSroy __fgetstr(FILE *__restrict fp, size_t *__restrict lenp, int sep)
516321f42fSchristos {
52755657beSroy 	ssize_t n;
536321f42fSchristos 
546321f42fSchristos 	_DIAGASSERT(fp != NULL);
556321f42fSchristos 	_DIAGASSERT(lenp != NULL);
566321f42fSchristos 
5700711901Sjoerg 	n = __getdelim(&_EXT(fp)->_fgetstr_buf, &_EXT(fp)->_fgetstr_len, sep, fp);
58755657beSroy 	if (n == -1) {
5986eafd3eSroy 		*lenp = 0;
607cfa0468Sroy 		if (__sferror(fp) && errno == EOVERFLOW) /* fixup errno */
61755657beSroy 			errno = EINVAL;
6286eafd3eSroy 		return NULL;
636321f42fSchristos 	}
64755657beSroy 	*lenp = n;
6500711901Sjoerg 	return _EXT(fp)->_fgetstr_buf;
66755657beSroy }
67