summaryrefslogtreecommitdiff
path: root/sys/src/cmd/upas/qfrom/qfrom.c
blob: df06d24ce1b4571f1467f2a52e2f1ba1e28b0df1 (plain)
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
/*
 * quote from lines without messing with character encoding.
 *	(might rather just undo the character encoding and use sed.)
 */

#include <u.h>
#include <libc.h>
#include <bio.h>

void
qfrom(int fd)
{
	Biobuf b, bo;
	char *s;
	int l;

	if(Binit(&b, fd, OREAD) == -1)
		sysfatal("Binit: %r");
	if(Binit(&bo, 1, OWRITE) == -1)
		sysfatal("Binit: %r");
	
	while(s = Brdstr(&b, '\n', 0)){
		l = Blinelen(&b);
		if(l >= 5)
		if(memcmp(s, "From ", 5) == 0)
			Bputc(&bo, ' ');
		Bwrite(&bo, s, l);
		free(s);
	}
	Bterm(&b);
	Bterm(&bo);
}
	
void
usage(void)
{
	fprint(2, "usage: qfrom [files...]\n");
	exits("");
}

void
main(int argc, char **argv)
{
	int fd;

	ARGBEGIN{
	default:
		usage();
	}ARGEND
	
	if(*argv == 0){
		qfrom(0);
		exits("");
	}
	for(; *argv; argv++){
		fd = open(*argv, OREAD);
		if(fd == -1)
			sysfatal("open: %r");
		qfrom(fd);
		close(fd);
	}
	exits("");
}