comparison sbr/cpydgst.c @ 0:bce86c4163a3

Initial revision
author kono
date Mon, 18 Apr 2005 23:46:02 +0900
parents
children 441a2190cfae
comparison
equal deleted inserted replaced
-1:000000000000 0:bce86c4163a3
1 /* cpydgst.c - copy from one fd to another in encapsulating mode */
2 #ifndef lint
3 static char ident[] = "@(#)$Id$";
4 #endif /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8
9
10 /* All we want to do is to perform the substitution
11
12 \n(-.*)\n --> \n- \1\n
13
14 we could use
15
16 sed -e 's%^-%- -%' < ifile > ofile
17
18 to do this, but the routine below is faster than the pipe, fork, and
19 exec.
20 */
21
22 #define S1 0
23 #define S2 1
24
25 #define output(c) if (bp >= dp) {flush(); *bp++ = c;} else *bp++ = c
26 #define flush() if ((j = bp - outbuf) && write (out, outbuf, j) != j) \
27 adios (ofile, "error writing"); \
28 else \
29 bp = outbuf
30
31 /* */
32
33 void cpydgst (in, out, ifile, ofile)
34 register int in,
35 out;
36 register char *ifile,
37 *ofile;
38 {
39 register int i,
40 state;
41 register char *cp,
42 *ep;
43 char buffer[BUFSIZ];
44 register int j;
45 register char *bp,
46 *dp;
47 char outbuf[BUFSIZ];
48
49 dp = (bp = outbuf) + sizeof outbuf;
50 for (state = S1; (i = read (in, buffer, sizeof buffer)) > 0;)
51 for (ep = (cp = buffer) + i; cp < ep; cp++) {
52 if (*cp == '\0')
53 continue;
54 switch (state) {
55 case S1:
56 if (*cp == '-') {
57 output ('-');
58 output (' ');
59 }
60 state = S2; /* fall */
61
62 case S2:
63 output (*cp);
64 if (*cp == '\n')
65 state = S1;
66 break;
67 }
68 }
69
70 if (i == NOTOK)
71 adios (ifile, "error reading");
72 flush ();
73 }