A healthy dose of sillyness
[retty.git] / blindtty.c
blobdb322a8c9e0bbff2b21028a132e2f4d1e4ba866b
1 /*
2 * blindtty - run command in a detached terminal
4 * Usage: blindtty CMD [ARGS]...
6 * You might find it useful to run it as "setsid blindtty CMD...".
8 * Copyright (c) 2006 Petr Baudis, Jan Sembera
9 */
12 * 'But the night will be too short,' said Gandalf. 'I have come back here,
13 * for I must have a little peace, alone. You should sleep, in a bed while you
14 * still may. At the sunrise I shall take you to the Lord Denethor again. No,
15 * when the summons comes, not at sunrise. The Darkness has begun. There will
16 * be no dawn.'
19 #include <pty.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <unistd.h>
26 void
27 sigchld(int s)
29 _exit(0);
32 int
33 main(int argc, char *argv[])
35 int ptm;
36 pid_t pid;
38 if (argc < 2) {
39 fprintf(stderr, "Usage: blindtty CMD [ARG]...\n");
40 return 1;
43 pid = forkpty(&ptm, NULL, NULL, NULL);
44 if (!pid) {
45 int i; for (i=0; i<argc; i++) argv[i] = argv[i+1]; argv[i]=NULL;
46 execvp(argv[0], argv);
47 perror("execvp() failed");
48 return 2;
51 printf("%s started with pid %d\n", argv[1], pid);
53 signal(SIGCHLD, sigchld);
55 while (1) {
56 char buf[1024];
57 if (read(ptm, buf, 1024) <= 0)
58 _exit(0);
61 return 0;