2011-05-25 19:40:47 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-25 19:40:47 +00:00
|
|
|
#include "../text.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
char *
|
|
|
|
afgets(char **p, size_t *size, FILE *fp)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
size_t n, len = 0;
|
|
|
|
|
|
|
|
while(fgets(buf, sizeof buf, fp)) {
|
|
|
|
len += (n = strlen(buf));
|
|
|
|
if(len+1 > *size && !(*p = realloc(*p, len+1)))
|
|
|
|
eprintf("realloc:");
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-25 19:40:47 +00:00
|
|
|
strcpy(&(*p)[len-n], buf);
|
|
|
|
|
|
|
|
if(buf[n-1] == '\n' || feof(fp))
|
|
|
|
break;
|
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2011-05-25 19:45:01 +00:00
|
|
|
return (len > 0) ? *p : NULL;
|
2011-05-25 19:40:47 +00:00
|
|
|
}
|