fgetln.c

Go to the documentation of this file.
00001 /* $NetBSD: fgetln.c,v 1.1.1.1 1999/04/12 07:43:21 crooksa Exp $  */
00002 
00003 /*-
00004  * Copyright (c) 1998 The NetBSD Foundation, Inc.
00005  * All rights reserved.
00006  *
00007  * This code is derived from software contributed to The NetBSD Foundation
00008  * by Christos Zoulas.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. All advertising materials mentioning features or use of this software
00019  *    must display the following acknowledgement:
00020  *        This product includes software developed by the NetBSD
00021  *        Foundation, Inc. and its contributors.
00022  * 4. Neither the name of The NetBSD Foundation nor the names of its
00023  *    contributors may be used to endorse or promote products derived
00024  *    from this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00028  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
00030  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "config.h"
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <unistd.h>
00043 #include <errno.h>
00044 #include <string.h>
00045 
00046 char *
00047 fgetln(fp, len)
00048    FILE *fp;
00049    size_t *len;
00050 {
00051    static char *buf = NULL;
00052    static size_t bufsiz = 0;
00053    char *ptr;
00054 
00055 
00056    if (buf == NULL) {
00057       bufsiz = BUFSIZ;
00058       if ((buf = malloc(bufsiz)) == NULL)
00059          return NULL;
00060    }
00061 
00062    if (fgets(buf, bufsiz, fp) == NULL)
00063       return NULL;
00064    *len = 0;
00065 
00066    while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
00067       size_t nbufsiz = bufsiz + BUFSIZ;
00068       char *nbuf = realloc(buf, nbufsiz);
00069 
00070       if (nbuf == NULL) {
00071          int oerrno = errno;
00072          free(buf);
00073          errno = oerrno;
00074          buf = NULL;
00075          return NULL;
00076       } else
00077          buf = nbuf;
00078 
00079       *len = bufsiz;
00080       if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
00081          return buf;
00082 
00083       bufsiz = nbufsiz;
00084    }
00085 
00086    *len = (ptr - buf) + 1;
00087    return buf;
00088 }

Generated on Thu Apr 16 06:27:34 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6