]> diplodocus.org Git - nmh/blob - sbr/cpydgst.c
new.c: Order two return statements to match comment.
[nmh] / sbr / cpydgst.c
1 /* cpydgst.c -- copy from one fd to another in encapsulating mode
2 * -- (do dashstuffing of input data).
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 /*
12 * We want to perform the substitution
13 *
14 * \n(-.*)\n --> \n- \1\n
15 *
16 * This is equivalent to the sed substitution
17 *
18 * sed -e 's%^-%- -%' < ifile > ofile
19 *
20 * but the routine below is faster than the pipe, fork, and exec.
21 */
22
23 #define S1 0
24 #define S2 1
25
26 #define output(c) if (bp >= dp) {flush(); *bp++ = c;} else *bp++ = c
27 #define flush() if ((j = bp - outbuf) && write (out, outbuf, j) != j) \
28 adios (ofile, "error writing"); \
29 else \
30 bp = outbuf
31
32
33 void
34 cpydgst (int in, int out, char *ifile, char *ofile)
35 {
36 int i, j, state;
37 char *cp, *ep;
38 char *bp, *dp;
39 char buffer[BUFSIZ], outbuf[BUFSIZ];
40
41 dp = (bp = outbuf) + sizeof outbuf;
42 for (state = S1; (i = read (in, buffer, sizeof buffer)) > 0;)
43 for (ep = (cp = buffer) + i; cp < ep; cp++) {
44 if (*cp == '\0')
45 continue;
46 switch (state) {
47 case S1:
48 if (*cp == '-') {
49 output ('-');
50 output (' ');
51 }
52 state = S2;
53 /* FALLTHRU */
54 case S2:
55 output (*cp);
56 if (*cp == '\n')
57 state = S1;
58 break;
59 }
60 }
61
62 if (i == -1)
63 adios (ifile, "error reading");
64 flush();
65 }