aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/dial.c
blob: e8e56902edc7f8b48301a5888e4af4878e281a01 (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
#include <u.h>
#include <libc.h>

void
usage(void)
{
	fprint(2, "usage: dial [-e] addr\n");
	exits("usage");
}

void
killer(void *x, char *msg)
{
	USED(x);
	if(strcmp(msg, "kill") == 0)
		exits(0);
	noted(NDFLT);
}

void
main(int argc, char **argv)
{
	int fd, pid;
	char buf[8192];
	int n, waitforeof;

	notify(killer);
	waitforeof = 0;
	ARGBEGIN{
	case 'e':
		waitforeof = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc != 1)
		usage();

	if((fd = dial(argv[0], nil, nil, nil)) < 0)
		sysfatal("dial: %r");

	switch(pid = fork()){
	case -1:
		sysfatal("fork: %r");
	case 0:
		while((n = read(0, buf, sizeof buf)) > 0)
			if(write(fd, buf, n) < 0)
				break;
		if(!waitforeof)
			postnote(PNPROC, getppid(), "kill");
		exits(nil);
	}

	while((n = read(fd, buf, sizeof buf)) > 0)
		if(write(1, buf, n) < 0)
			break;

	postnote(PNPROC, pid, "kill");
	waitpid();
	exits(0);
}