[alt.sources.patches] papif won't eat pipes comp.protocols.appletalk

jeff@tc.fluke.COM (Jeff Stearns) (01/06/90)

Archive-name: papif/wont-eat-pipes
Original-posting-by: jeff@tc.fluke.COM (Jeff Stearns)
Original-subject: Re: papif won't eat pipes
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)

[This is a patch to papif from comp.protocols.appletalk.]


In article <1990Jan2.200830.22744@athena.mit.edu> kenton@space.mit.edu (Kenton C. Phillips) writes:
> I have CAP 5.0 running on a Sun (OS 3.5).  I have found that papif works
> fine in the context
>
> papif <file
>
> but only gets the first several bytes when fed through a pipe...

--- I remember this bug; here's an excerpt from the RCS log of papif.c:

* Version 1.4  89/04/07  16:16:27  jeff
* Fix two major bugs, both in the passalong() function.
* Worst: it didn't work.  It read from stdin but, due to missing braces
* around a while() loop, it never called write().  This prevented papif
* from working when its input was taken from a non-seekable device.
*
* Second bug, discovered while inspecting code: the "dangerous" loop
* which maps CR to LF doesn't increment all its pointers.  Fixed that,
* but didn't test it.

--- My updated passalong() routine extracted from papif.c:

/*
 * duplicate stdin with prefixed buffer
 *
*/
passalong(prebuf, precnt, wantcnt)
char *prebuf;
int precnt;
int wantcnt;
{
  register int cnt;
  register char *p;
  register int i;
  char buf[BUFSIZ];


  if (precnt < 0)
    exit(lpd_ERRORS);
  if (map_crtolf) {
    for (i = 0; i < precnt; i++)
      if (prebuf[i] == '\r')
        prebuf[i] = '\n';
  }
  if (write(fileno(stdout), prebuf, precnt) < 0)
    exit(lpd_ERRORS);
  if (precnt < wantcnt)
    exit(lpd_OK);
  /*
   *  The distributed code contained TWO separate bugs in this while loop.
   *  Check carefully; there might be more!  jeff@tc.fluke.com  7-Apr-89.
   */
  while ((cnt = read(fileno(stdin), buf, sizeof(buf))) > 0) {
    /* dangerous */
    if (map_crtolf) {
      for (i = 0, p = buf; i < cnt; i++, p++) {
        if (*p  == '\r')
          *p = '\n';
      }  
    }
    if (write(fileno(stdout), buf, cnt) < 0)
      exit(lpd_ERRORS);
  }
  exit(cnt < 0 ? lpd_ERRORS : lpd_OK);
}


-- 
    Jeff Stearns        John Fluke Mfg. Co, Inc.               (206) 356-5064
    jeff@tc.fluke.COM   {uw-beaver,microsoft,sun}!fluke!jeff