File: stdmerge.c

package info (click to toggle)
tra 20020816-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,696 kB
  • ctags: 2,623
  • sloc: ansic: 22,519; makefile: 406; asm: 269
file content (123 lines) | stat: -rw-r--r-- 1,739 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>

#define nil ((void*) 0)

enum
{
	BUFSZ = (1<<15)
};

static void
fatal(char *s)
{
	fprintf(stderr, "%s: %s\n", s, strerror(errno));
	exit(1);
}

static int
writen(int fd, char *buf, int n)
{
	int m, k = 0;

	while(n > 0){
		if((m = write(fd, buf, n)) < 0){
			if(errno == EINTR)
				continue;
			fatal("write");
		}
		buf += m;
		n -= m;
		k += m;
	}

	return k;
}

static int
copy(int from, int to, int w)
{
	int n;
	char tmp[6];
	static char buf[BUFSZ+5];

	if((n = read(from, buf+5, BUFSZ)) > 0){
		sprintf(tmp, "%c%.4X", w?'e':'o', n);
		memmove(buf, tmp, 5);
		if(writen(to, buf, n+5) != n+5)
			fatal("writen");
	}

	return n;
}

static void
merge(int a, int b)
{
	int ao=1, bo=1, m = 1 + ((a>b)?a:b);
	fd_set rdset;

	while(ao || bo){
		FD_ZERO(&rdset);
		if(ao)
			FD_SET(a, &rdset);
		if(bo)
			FD_SET(b, &rdset);

		if(select(m, &rdset, nil, nil, nil) == -1){
			if(errno == EINTR)
				continue;
			fatal("select");
		}
		if(ao && FD_ISSET(a, &rdset))
			ao = (copy(a, 1, 0) != 0);
		if(bo && FD_ISSET(b, &rdset))
			bo = (copy(b, 1, 1) != 0);
	}

	return;
}

static void
usage(void)
{
	fprintf(stderr, "usage: stdmerge cmd ...\n");
}

int
main(int argc, char *argv[])
{
	int ps[2], pe[2];

	if(argc <= 1){
		usage();
		return 1;
	}

	if((pipe(ps) < 0) || (pipe(pe) < 0))
		fatal("pipe");

	switch(fork()){
	case -1:
		fatal("fork");

	case 0:
		close(ps[0]); close(pe[0]);
		if(dup2(ps[1], 1) < 0 || dup2(pe[1], 2) < 0)
			fatal("dup2");
		execvp(argv[1], argv+1);
		fatal("exec");

	default:
		close(ps[1]); close(pe[1]);
		merge(ps[0], pe[0]);
	}
	
	return 0;
}