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