2 ** cpydgst.c -- copy from one fd to another in encapsulating mode
3 ** -- (do dashstuffing of input data).
5 ** This code is Copyright (c) 2002, by the authors of nmh. See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
15 ** We want to perform the substitution
17 ** \n(-.*)\n --> \n- \1\n
19 ** This is equivalent to the sed substitution
21 ** sed -e 's%^-%- -%' < ifile > ofile
23 ** but the routine below is faster than the pipe, fork, and exec.
29 #define output(c) if (bp >= dp) {flush(); *bp++ = c;} else *bp++ = c
30 #define flush() if ((j = bp - outbuf) && write(out, outbuf, j) != j) \
31 adios(EX_IOERR, ofile, "error writing"); \
37 cpydgst(int in, int out, char *ifile, char *ofile)
42 char buffer[BUFSIZ], outbuf[BUFSIZ];
44 dp = (bp = outbuf) + sizeof outbuf;
45 for (state = S1; (i = read(in, buffer, sizeof buffer)) > 0;)
46 for (ep = (cp = buffer) + i; cp < ep; cp++) {
55 state = S2; /* fall */
66 adios(EX_IOERR, ifile, "error reading");