Home | History | Annotate | Line # | Download | only in stdio
      1  1.12       uwe /* $NetBSD: fgetstr.c,v 1.12 2017/06/08 15:59:45 uwe 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.12       uwe __RCSID("$NetBSD: fgetstr.c,v 1.12 2017/06/08 15:59:45 uwe 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.12       uwe  * 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.9       roy 	ssize_t n;
     53   1.1  christos 
     54   1.1  christos 	_DIAGASSERT(fp != NULL);
     55   1.1  christos 	_DIAGASSERT(lenp != NULL);
     56   1.1  christos 
     57  1.11     joerg 	n = __getdelim(&_EXT(fp)->_fgetstr_buf, &_EXT(fp)->_fgetstr_len, sep, fp);
     58   1.9       roy 	if (n == -1) {
     59   1.9       roy 		*lenp = 0;
     60  1.10       roy 		if (__sferror(fp) && errno == EOVERFLOW) /* fixup errno */
     61   1.9       roy 			errno = EINVAL;
     62   1.9       roy 		return NULL;
     63   1.1  christos 	}
     64   1.9       roy 	*lenp = n;
     65  1.11     joerg 	return _EXT(fp)->_fgetstr_buf;
     66   1.1  christos }
     67