xref: /titanic_50/usr/src/cmd/mailwrapper/fgetln.c (revision 287247a826fa2ab8d01f6c8f276d405eb08420f8)
1*287247a8SAlexander Pyhalov /* $Id$ */
2*287247a8SAlexander Pyhalov /*	$NetBSD: fgetln.c,v 1.3 2007/08/07 02:06:58 lukem Exp $	*/
3*287247a8SAlexander Pyhalov 
4*287247a8SAlexander Pyhalov /*
5*287247a8SAlexander Pyhalov  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6*287247a8SAlexander Pyhalov  * All rights reserved.
7*287247a8SAlexander Pyhalov  *
8*287247a8SAlexander Pyhalov  * This code is derived from software contributed to The NetBSD Foundation
9*287247a8SAlexander Pyhalov  * by Christos Zoulas.
10*287247a8SAlexander Pyhalov  *
11*287247a8SAlexander Pyhalov  * Redistribution and use in source and binary forms, with or without
12*287247a8SAlexander Pyhalov  * modification, are permitted provided that the following conditions
13*287247a8SAlexander Pyhalov  * are met:
14*287247a8SAlexander Pyhalov  * 1. Redistributions of source code must retain the above copyright
15*287247a8SAlexander Pyhalov  *    notice, this list of conditions and the following disclaimer.
16*287247a8SAlexander Pyhalov  * 2. Redistributions in binary form must reproduce the above copyright
17*287247a8SAlexander Pyhalov  *    notice, this list of conditions and the following disclaimer in the
18*287247a8SAlexander Pyhalov  *    documentation and/or other materials provided with the distribution.
19*287247a8SAlexander Pyhalov  * 3. Neither the name of The NetBSD Foundation nor the names of its
20*287247a8SAlexander Pyhalov  *    contributors may be used to endorse or promote products derived
21*287247a8SAlexander Pyhalov  *    from this software without specific prior written permission.
22*287247a8SAlexander Pyhalov  *
23*287247a8SAlexander Pyhalov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24*287247a8SAlexander Pyhalov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25*287247a8SAlexander Pyhalov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*287247a8SAlexander Pyhalov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27*287247a8SAlexander Pyhalov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*287247a8SAlexander Pyhalov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*287247a8SAlexander Pyhalov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30*287247a8SAlexander Pyhalov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31*287247a8SAlexander Pyhalov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32*287247a8SAlexander Pyhalov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*287247a8SAlexander Pyhalov  * POSSIBILITY OF SUCH DAMAGE.
34*287247a8SAlexander Pyhalov  */
35*287247a8SAlexander Pyhalov 
36*287247a8SAlexander Pyhalov #include <sys/types.h>
37*287247a8SAlexander Pyhalov 
38*287247a8SAlexander Pyhalov #include <errno.h>
39*287247a8SAlexander Pyhalov #include <stdio.h>
40*287247a8SAlexander Pyhalov #include <stdlib.h>
41*287247a8SAlexander Pyhalov #include <string.h>
42*287247a8SAlexander Pyhalov 
43*287247a8SAlexander Pyhalov char *
fgetln(FILE * fp,size_t * len)44*287247a8SAlexander Pyhalov fgetln(FILE *fp, size_t *len)
45*287247a8SAlexander Pyhalov {
46*287247a8SAlexander Pyhalov 	static char *buf = NULL;
47*287247a8SAlexander Pyhalov 	static size_t bufsiz = 0;
48*287247a8SAlexander Pyhalov 	char *ptr;
49*287247a8SAlexander Pyhalov 
50*287247a8SAlexander Pyhalov 
51*287247a8SAlexander Pyhalov 	if (buf == NULL) {
52*287247a8SAlexander Pyhalov 		bufsiz = BUFSIZ;
53*287247a8SAlexander Pyhalov 		if ((buf = malloc(bufsiz)) == NULL)
54*287247a8SAlexander Pyhalov 			return (NULL);
55*287247a8SAlexander Pyhalov 	}
56*287247a8SAlexander Pyhalov 
57*287247a8SAlexander Pyhalov 	if (fgets(buf, bufsiz, fp) == NULL)
58*287247a8SAlexander Pyhalov 		return (NULL);
59*287247a8SAlexander Pyhalov 
60*287247a8SAlexander Pyhalov 	*len = 0;
61*287247a8SAlexander Pyhalov 	while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
62*287247a8SAlexander Pyhalov 		size_t nbufsiz = bufsiz + BUFSIZ;
63*287247a8SAlexander Pyhalov 		char *nbuf = realloc(buf, nbufsiz);
64*287247a8SAlexander Pyhalov 
65*287247a8SAlexander Pyhalov 		if (nbuf == NULL) {
66*287247a8SAlexander Pyhalov 			int oerrno = errno;
67*287247a8SAlexander Pyhalov 			free(buf);
68*287247a8SAlexander Pyhalov 			errno = oerrno;
69*287247a8SAlexander Pyhalov 			buf = NULL;
70*287247a8SAlexander Pyhalov 			return (NULL);
71*287247a8SAlexander Pyhalov 		} else
72*287247a8SAlexander Pyhalov 			buf = nbuf;
73*287247a8SAlexander Pyhalov 
74*287247a8SAlexander Pyhalov 		*len = bufsiz;
75*287247a8SAlexander Pyhalov 		if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
76*287247a8SAlexander Pyhalov 			return (buf);
77*287247a8SAlexander Pyhalov 
78*287247a8SAlexander Pyhalov 		bufsiz = nbufsiz;
79*287247a8SAlexander Pyhalov 	}
80*287247a8SAlexander Pyhalov 
81*287247a8SAlexander Pyhalov 	*len = (ptr - buf) + 1;
82*287247a8SAlexander Pyhalov 	return (buf);
83*287247a8SAlexander Pyhalov }
84