1*62740a49SJérôme Duval
2*62740a49SJérôme Duval #include <stdio.h>
3*62740a49SJérôme Duval #include <stdlib.h>
4*62740a49SJérôme Duval #include <stdarg.h>
5*62740a49SJérôme Duval #include <fcntl.h>
6*62740a49SJérôme Duval #include <unistd.h>
7*62740a49SJérôme Duval #include <string.h>
8*62740a49SJérôme Duval #include <errno.h>
9*62740a49SJérôme Duval #include <sys/ioctl.h>
10*62740a49SJérôme Duval #include <termios.h>
11*62740a49SJérôme Duval #include <signal.h>
12*62740a49SJérôme Duval
die(const char * ptr,...)13*62740a49SJérôme Duval void die(const char *ptr, ...)
14*62740a49SJérôme Duval {
15*62740a49SJérôme Duval va_list vlist;
16*62740a49SJérôme Duval va_start(vlist, ptr);
17*62740a49SJérôme Duval char ch;
18*62740a49SJérôme Duval
19*62740a49SJérôme Duval while ((ch = *ptr++) != '\0') {
20*62740a49SJérôme Duval if (ch == '%' && *ptr != '\0') {
21*62740a49SJérôme Duval ch = *ptr++;
22*62740a49SJérôme Duval switch(ch) {
23*62740a49SJérôme Duval case 'd': printf("%d", va_arg(vlist, int)); break;
24*62740a49SJérôme Duval case 'e': printf("%e", va_arg(vlist, double)); break;
25*62740a49SJérôme Duval case 'c': printf("%c", va_arg(vlist, int)); break;
26*62740a49SJérôme Duval case 's': printf("%s", va_arg(vlist, char *)); break;
27*62740a49SJérôme Duval default: va_end(vlist);exit(1);
28*62740a49SJérôme Duval
29*62740a49SJérôme Duval }
30*62740a49SJérôme Duval
31*62740a49SJérôme Duval } else
32*62740a49SJérôme Duval printf("%c", ch);
33*62740a49SJérôme Duval
34*62740a49SJérôme Duval
35*62740a49SJérôme Duval }
36*62740a49SJérôme Duval
37*62740a49SJérôme Duval va_end(vlist);
38*62740a49SJérôme Duval exit(1);
39*62740a49SJérôme Duval }
40*62740a49SJérôme Duval
41*62740a49SJérôme Duval
main()42*62740a49SJérôme Duval int main()
43*62740a49SJérôme Duval {
44*62740a49SJérôme Duval int pid;
45*62740a49SJérôme Duval int ptm;
46*62740a49SJérôme Duval int pts;
47*62740a49SJérôme Duval char *ptr;
48*62740a49SJérôme Duval char *program_name[2] = { (char*)"/bin/uname", NULL};
49*62740a49SJérôme Duval char buf[512];
50*62740a49SJérôme Duval int n;
51*62740a49SJérôme Duval
52*62740a49SJérôme Duval if ((ptm = posix_openpt(O_RDWR)) == -1) {
53*62740a49SJérôme Duval die("posix_openpt error: %s\n", strerror(errno));
54*62740a49SJérôme Duval }
55*62740a49SJérôme Duval if (grantpt(ptm) == -1) {
56*62740a49SJérôme Duval die("grantpt error: %s\n", strerror(errno));
57*62740a49SJérôme Duval }
58*62740a49SJérôme Duval if (unlockpt(ptm) == -1) {
59*62740a49SJérôme Duval die("unlockpt error: %s\n", strerror(errno));
60*62740a49SJérôme Duval }
61*62740a49SJérôme Duval
62*62740a49SJérôme Duval pid = fork();
63*62740a49SJérôme Duval if (pid < 0) {
64*62740a49SJérôme Duval die("fork error: %s\n", strerror(errno));
65*62740a49SJérôme Duval } else if (pid == 0) { // child
66*62740a49SJérôme Duval if (setsid() == (pid_t)-1) {
67*62740a49SJérôme Duval die("setsid() error: %s\n", strerror(errno));
68*62740a49SJérôme Duval }
69*62740a49SJérôme Duval if ((ptr = (char *) ptsname(ptm)) == NULL) {
70*62740a49SJérôme Duval die("ptsname error: %s\n", strerror(errno));
71*62740a49SJérôme Duval }
72*62740a49SJérôme Duval if ((pts = open(ptr, O_RDWR)) < 0) {
73*62740a49SJérôme Duval die("open of slave failed: %a\n", strerror(errno));
74*62740a49SJérôme Duval }
75*62740a49SJérôme Duval close(ptm);
76*62740a49SJérôme Duval
77*62740a49SJérôme Duval if (dup2(pts, STDIN_FILENO) != STDIN_FILENO
78*62740a49SJérôme Duval || dup2(pts, STDOUT_FILENO) != STDOUT_FILENO
79*62740a49SJérôme Duval || dup2(pts, STDERR_FILENO) != STDERR_FILENO) {
80*62740a49SJérôme Duval die("error doing dup's : %s\n", strerror(errno));
81*62740a49SJérôme Duval }
82*62740a49SJérôme Duval
83*62740a49SJérôme Duval if (execve(*program_name, program_name , NULL) == -1) {
84*62740a49SJérôme Duval die("execve error: %s\n", strerror(errno));
85*62740a49SJérôme Duval }
86*62740a49SJérôme Duval exit(1);
87*62740a49SJérôme Duval } else { // parent
88*62740a49SJérôme Duval if (dup2(ptm, STDIN_FILENO) != STDIN_FILENO) {
89*62740a49SJérôme Duval die("dup2 of parent failed");
90*62740a49SJérôme Duval }
91*62740a49SJérôme Duval while (1) {
92*62740a49SJérôme Duval n = read(ptm, buf, 511);
93*62740a49SJérôme Duval if (n <= 0) {
94*62740a49SJérôme Duval break;
95*62740a49SJérôme Duval }
96*62740a49SJérôme Duval write(STDOUT_FILENO, buf, n);
97*62740a49SJérôme Duval }
98*62740a49SJérôme Duval }
99*62740a49SJérôme Duval return 0;
100*62740a49SJérôme Duval }
101*62740a49SJérôme Duval
102