getline.c - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
getline.c (555B)
---
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 ssize_t
5 getline(char **lineptr, size_t *linesiz, FILE *fp)
6 {
7 ssize_t len = 0;
8 char *p = *lineptr;
9 int c = EOF;
10
11 while (c != '\n') {
12 if (len + 2 >= *linesiz) {
13 /* NOTE: POSIX requires to set an error flag FILE *fp,
14 but there is no direct access to it */
15 if (!(p = realloc(p, len + 4096)))
16 return -1;
17 *lineptr = p;
18 *linesiz += 4096;
19 }
20 if ((c = getc(fp)) == EOF)
21 break; /* EOF or error */
22 p[len++] = c;
23 }
24 if (len == 0 || ferror(fp))
25 return -1;
26
27 p[len] = '\0';
28
29 return len;
30 }