fgetln.c File Reference

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

Include dependency graph for fgetln.c:

Go to the source code of this file.

Functions

char * fgetln (FILE *fp, size_t *len)


Function Documentation

char* fgetln ( FILE *  fp,
size_t *  len 
)

Definition at line 47 of file fgetln.c.

References buf, errno, free, malloc, NULL, and realloc.

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:31:00 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6