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.
13 ** We want to perform the substitution
15 ** \n(-.*)\n --> \n- \1\n
17 ** This is equivalent to the sed substitution
19 ** sed -e 's%^-%- -%' < ifile > ofile
21 ** but the routine below is faster than the pipe, fork, and exec.
27 #define output(c) if (bp >= dp) {flush(); *bp++ = c;} else *bp++ = c
28 #define flush() if ((j = bp - outbuf) && write(out, outbuf, j) != j) \
29 adios(ofile, "error writing"); \
35 cpydgst(int in, int out, char *ifile, char *ofile)
37 register int i, j, state;
38 register char *cp, *ep;
39 register char *bp, *dp;
40 char buffer[BUFSIZ], outbuf[BUFSIZ];
42 dp = (bp = outbuf) + sizeof outbuf;
43 for (state = S1; (i = read(in, buffer, sizeof buffer)) > 0;)
44 for (ep = (cp = buffer) + i; cp < ep; cp++) {
53 state = S2; /* fall */
64 adios(ifile, "error reading");