Tizen 2.0 Release
[external/libgnutls26.git] / src / cfg / platon / str / dynfgets.c
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8
9 #include <platon/str/dynfgets.h>
10
11         char *
12 PLATON_FUNC(dynamic_fgets)(fp)
13         FILE *fp;
14 {
15         char temp[DYNAMIC_FGETS_BUFSIZE];
16         register char *ptr;
17         register int i;
18
19         if ((ptr = (char *) malloc(1)) == NULL)
20                 return NULL;
21
22         for (*ptr = '\0', i = 0; ; i++) {
23                 if (fgets(temp, DYNAMIC_FGETS_BUFSIZE, fp) == NULL) {
24                         if (ferror(fp) != 0 || i == 0) {
25                                 free(ptr);
26                                 return NULL;
27                         }
28
29                         return ptr;
30                 }
31
32                 ptr = (char *) realloc(ptr, (DYNAMIC_FGETS_BUFSIZE - 1) * (i + 1) + 1);
33                 if (ptr == NULL)
34                         return NULL;
35
36                 strcat(ptr, temp);
37
38                 if (strchr(temp, '\n') != NULL) {
39                         *strchr(ptr, '\n') = '\0';
40                         return ptr;
41                 }
42         }
43 }
44