1: /* vfgets.c - virtual fgets */
   2: 
   3: #include "../h/mh.h"
   4: #include <stdio.h>
   5: 
   6: 
   7: #define QUOTE   '\\'
   8: 
   9: 
  10: int vfgets (in, bp)
  11: register FILE *in;
  12: register char  **bp;
  13: {
  14:     register int    toggle;
  15:     register char  *cp,
  16:                    *dp,
  17:                    *ep,
  18:            *fp;
  19:     static int  len = 0;
  20:     static char *pp = NULL;
  21: 
  22:     if (pp == NULL)
  23:     if ((pp = malloc ((unsigned) (len = BUFSIZ))) == NULL)
  24:         adios (NULLCP, "unable to allocate string storage");
  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 OK;
  31:         }
  32:         return (ferror (in) ? NOTOK : DONE);
  33:     }
  34: 
  35:     if ((dp = cp + strlen (cp) - 2) < cp || *dp != QUOTE) {
  36: wrong_guess: ;
  37:         if (cp > ++dp)
  38:         adios (NULLCP, "vfgets() botch -- you lose big");
  39:         if (*dp == '\n') {
  40:         *bp = pp;
  41:         return OK;
  42:         }
  43:         else
  44:         cp = ++dp;
  45:     }
  46:     else {
  47:         for (fp = dp - 1, toggle = 0; fp >= cp; fp--)
  48:         if (*fp != QUOTE)
  49:             break;
  50:         else
  51:             toggle = !toggle;
  52:         if (toggle)
  53:         goto wrong_guess;
  54:         if (*++dp == '\n')
  55:         *--dp = NULL, cp = dp;
  56:         else
  57:         cp = ++dp;
  58:     }
  59: 
  60:     if (cp >= ep) {
  61:         register int curlen = cp - pp;
  62: 
  63:         if ((dp = realloc (pp, (unsigned) (len += BUFSIZ))) == NULL)
  64:         adios (NULLCP, "unable to allocate string storage");
  65:         else
  66:         cp = dp + curlen, ep = (pp = dp) + len - 1;
  67:     }
  68:     }
  69: }

Defined functions

vfgets defined in line 10; never used

Defined macros

QUOTE defined in line 7; used 2 times
Last modified: 1986-04-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 571
Valid CSS Valid XHTML 1.0 Strict