Fixed a possible uninitialized variable access.
[mmh] / sbr / vfgets.c
1 /*
2 ** vfgets.c -- virtual fgets
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 #define QUOTE '\\'
13
14
15 int
16 vfgets(FILE *in, char **bp)
17 {
18         int toggle;
19         char *cp, *dp, *ep, *fp;
20         static int len = 0;
21         static char *pp = NULL;
22
23         if (pp == NULL)
24                 pp = mh_xmalloc((size_t) (len = BUFSIZ));
25
26         for (ep = (cp = pp) + len - 1;;) {
27                 if (fgets(cp, ep - cp + 1, in) == NULL) {
28                         if (cp != pp) {
29                                 *bp = pp;
30                                 return 0;
31                         }
32                         return (ferror(in) && !feof(in) ? -1 : 1);
33                 }
34
35                 if ((dp = cp + strlen(cp) - 2) < cp || *dp != QUOTE) {
36 wrong_guess:
37                         if (cp > ++dp)
38                                 adios(NULL, "vfgets() botch -- you lose big");
39                         if (*dp == '\n') {
40                                 *bp = pp;
41                                 return 0;
42                         } else {
43                                 cp = ++dp;
44                         }
45                 } else {
46                         for (fp = dp - 1, toggle = 0; fp >= cp; fp--) {
47                                 if (*fp != QUOTE)
48                                         break;
49                                 else
50                                         toggle = !toggle;
51                         }
52                         if (toggle)
53                                 goto wrong_guess;
54
55                         if (*++dp == '\n') {
56                                 *--dp = 0;
57                                 cp = dp;
58                         } else {
59                                 cp = ++dp;
60                         }
61                 }
62
63                 if (cp >= ep) {
64                         int curlen = cp - pp;
65
66                         dp = mh_xrealloc(pp, (size_t) (len += BUFSIZ));
67                         cp = dp + curlen;
68                         ep = (pp = dp) + len - 1;
69                 }
70         }
71 }