Line | |
---|
1 |
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <string.h>
|
---|
5 |
|
---|
6 | char *
|
---|
7 | fgetln(FILE *fp, size_t *lenp)
|
---|
8 | {
|
---|
9 | #define EXTRA 80
|
---|
10 | char *buf = NULL;
|
---|
11 | int used = 0, len = 0, remain = 0, final = 0;
|
---|
12 | while(!final) {
|
---|
13 | char *b;
|
---|
14 | int r;
|
---|
15 | if(remain < EXTRA) {
|
---|
16 | int newlen;
|
---|
17 | char *newbuf;
|
---|
18 | newlen = len + EXTRA;
|
---|
19 | if(!(newbuf = realloc(buf, newlen))) {
|
---|
20 | if(buf) free(buf);
|
---|
21 | return NULL;
|
---|
22 | }
|
---|
23 | buf = newbuf;
|
---|
24 | len = newlen;
|
---|
25 | remain += EXTRA;
|
---|
26 | }
|
---|
27 | buf[used] = '\0';
|
---|
28 | if(!fgets(buf + used, remain, fp))
|
---|
29 | break;
|
---|
30 | r = strlen(buf+used);
|
---|
31 | used += r;
|
---|
32 | remain -= r;
|
---|
33 | len += r;
|
---|
34 | }
|
---|
35 | *lenp = len;
|
---|
36 | return buf;
|
---|
37 | }
|
---|
38 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.