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