X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/6c42153ad9362cc676ea66563bf400d7511b3b68..2b7a0f47ea3ecea7b50c3052e854eae0d306da80:/sbr/vfgets.c diff --git a/sbr/vfgets.c b/sbr/vfgets.c index 39cfaa27..b9f5de20 100644 --- a/sbr/vfgets.c +++ b/sbr/vfgets.c @@ -1,15 +1,14 @@ - -/* - * vfgets.c -- virtual fgets - * - * $Id$ +/* vfgets.c -- virtual fgets * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. */ -#include +#include "h/mh.h" +#include "vfgets.h" +#include "error.h" +#include "h/utils.h" #define QUOTE '\\' @@ -23,8 +22,7 @@ vfgets (FILE *in, char **bp) static char *pp = NULL; if (pp == NULL) - if (!(pp = malloc ((size_t) (len = BUFSIZ)))) - adios (NULL, "unable to allocate string storage"); + pp = mh_xmalloc ((size_t) (len = BUFSIZ)); for (ep = (cp = pp) + len - 1;;) { if (fgets (cp, ep - cp + 1, in) == NULL) { @@ -32,25 +30,23 @@ vfgets (FILE *in, char **bp) *bp = pp; return 0; } - return (ferror (in) && !feof (in) ? -1 : 1); + return ferror(in) && !feof(in) ? -1 : 1; } if ((dp = cp + strlen (cp) - 2) < cp || *dp != QUOTE) { wrong_guess: if (cp > ++dp) - adios (NULL, "vfgets() botch -- you lose big"); + die("vfgets() botch -- you lose big"); if (*dp == '\n') { *bp = pp; return 0; - } else { - cp = ++dp; } + cp = ++dp; } else { for (fp = dp - 1, toggle = 0; fp >= cp; fp--) { if (*fp != QUOTE) break; - else - toggle = !toggle; + toggle = !toggle; } if (toggle) goto wrong_guess; @@ -66,12 +62,9 @@ wrong_guess: if (cp >= ep) { int curlen = cp - pp; - if (!(dp = realloc (pp, (size_t) (len += BUFSIZ)))) { - adios (NULL, "unable to allocate string storage"); - } else { - cp = dp + curlen; - ep = (pp = dp) + len - 1; - } + dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ)); + cp = dp + curlen; + ep = (pp = dp) + len - 1; } } }