xref: /minix3/lib/libc/stdio/fgetstr.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* $NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
7*2fe8fb19SBen Gras  * by Roy Marples.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
10*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
11*2fe8fb19SBen Gras  * are met:
12*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
13*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
14*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
15*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
16*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
17*2fe8fb19SBen Gras  *
18*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*2fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*2fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*2fe8fb19SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*2fe8fb19SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*2fe8fb19SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*2fe8fb19SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*2fe8fb19SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*2fe8fb19SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*2fe8fb19SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*2fe8fb19SBen Gras  */
29*2fe8fb19SBen Gras 
30*2fe8fb19SBen Gras #include <sys/cdefs.h>
31*2fe8fb19SBen Gras __RCSID("$NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $");
32*2fe8fb19SBen Gras 
33*2fe8fb19SBen Gras #include "namespace.h"
34*2fe8fb19SBen Gras 
35*2fe8fb19SBen Gras #include <assert.h>
36*2fe8fb19SBen Gras #include <errno.h>
37*2fe8fb19SBen Gras #include <limits.h>
38*2fe8fb19SBen Gras #include <stdio.h>
39*2fe8fb19SBen Gras 
40*2fe8fb19SBen Gras #include "reentrant.h"
41*2fe8fb19SBen Gras #include "local.h"
42*2fe8fb19SBen Gras 
43*2fe8fb19SBen Gras /*
44*2fe8fb19SBen Gras  * Get an input line.
45*2fe8fb19SBen Gras  * This now uses getdelim(3) for a code reduction.
46*2fe8fb19SBen Gras  * The upside is that strings are now always NULL terminated, but relying
47*2fe8fb19SBen Gras  * on this is non portable - better to use the POSIX getdelim(3) function.
48*2fe8fb19SBen Gras  */
49*2fe8fb19SBen Gras char *
__fgetstr(FILE * __restrict fp,size_t * __restrict lenp,int sep)50*2fe8fb19SBen Gras __fgetstr(FILE *__restrict fp, size_t *__restrict lenp, int sep)
51*2fe8fb19SBen Gras {
52*2fe8fb19SBen Gras 	ssize_t n;
53*2fe8fb19SBen Gras 
54*2fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
55*2fe8fb19SBen Gras 	_DIAGASSERT(lenp != NULL);
56*2fe8fb19SBen Gras 
57*2fe8fb19SBen Gras 	n = __getdelim(&_EXT(fp)->_fgetstr_buf, &_EXT(fp)->_fgetstr_len, sep, fp);
58*2fe8fb19SBen Gras 	if (n == -1) {
59*2fe8fb19SBen Gras 		*lenp = 0;
60*2fe8fb19SBen Gras 		if (__sferror(fp) && errno == EOVERFLOW) /* fixup errno */
61*2fe8fb19SBen Gras 			errno = EINVAL;
62*2fe8fb19SBen Gras 		return NULL;
63*2fe8fb19SBen Gras 	}
64*2fe8fb19SBen Gras 	*lenp = n;
65*2fe8fb19SBen Gras 	return _EXT(fp)->_fgetstr_buf;
66*2fe8fb19SBen Gras }
67