File: repl.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 (55 lines) | stat: -rw-r--r-- 883 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
#include "tra.h"
#include <sys/param.h>
#include <signal.h>

char*
trapath(char *name)
{
	char *home;

	if(name[0] == '/'
	|| (name[0] == '.' && name[1] == '/')
	|| (name[0] == '.' && name[2] == '.' && name[3] == '/'))
		return estrdup(name);

	home = getenv("HOME");
	if(home == nil)
		sysfatal("cannot read user name");

	return esmprint("%s/.tra/%s", home, name);
}

Replica*
dialreplica(char *name)
{
	char *bin;
	int p[2], q[2];

	bin = trapath(name);
	if(access(bin, 1) < 0)
		sysfatal("access %s: %r", bin);

	if(pipe(p) < 0 || pipe(q) < 0)
		sysfatal("pipe: %r");

	signal(SIGCHLD, SIG_IGN);
	switch(fork()){
	case -1:
		sysfatal("rfork dialreplica: %r");

	case 0:
		close(p[0]);
		close(q[1]);
		dup2(q[0], 0);
		dup2(p[1], 1);
		execl(bin, name, nil);
		sysfatal("exec %s: %r", bin);

	default:
		close(q[0]);
		close(p[1]);
		break;
	}
	return fd2replica(p[0], q[1]);
}