File: blindtty.c

package info (click to toggle)
retty 1.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 96 kB
  • ctags: 54
  • sloc: ansic: 414; asm: 205; makefile: 42; perl: 24
file content (54 lines) | stat: -rw-r--r-- 859 bytes parent folder | download | duplicates (2)
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
/*
 * blindtty - run command in a detached terminal
 *
 * Usage: blindtty CMD [ARGS]...
 *
 * You might find it useful to run it as "setsid blindtty CMD...".
 *
 * Copyright (c) 2006  Petr Baudis, Jan Sembera
 */

#include <pty.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


void
sigchld(int s)
{
	_exit(0);
}

int
main(int argc, char *argv[])
{
	int ptm;
	pid_t pid;

	if (argc < 2) {
		fprintf(stderr, "Usage: blindtty CMD [ARG]...\n");
		return 1;
	}

	pid = forkpty(&ptm, NULL, NULL, NULL);
	if (!pid) {
		int i; for (i=0; i<argc; i++) argv[i] = argv[i+1]; argv[i]=NULL;
		execvp(argv[0], argv);
		perror("execvp() failed");
		return 2;
	}

	printf("%s started with pid %d\n", argv[1], pid);

	signal(SIGCHLD, sigchld);

	while (1) {
		char buf[1024];
		if (read(ptm, buf, 1024) <= 0)
			_exit(0);
	}

	return 0;
}