aboutsummaryrefslogtreecommitdiffstats
path: root/taketty.c
blob: 31fb25e34c1b8ca95cca58610824a2e34d1b0f75 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>

static void set_raw_mode(int fd) {
    struct termios tios;

    if (tcgetattr(fd, &tios) < 0) {
        perror("tcgetattr");
        exit(1);
    }

    tios.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
    tios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
    tios.c_cflag &= ~(CSIZE | PARENB);
    tios.c_cflag |= CS8;
    tios.c_oflag &= ~(OPOST);

    tios.c_cc[VMIN] = 1;
    tios.c_cc[VTIME] = 0;

    if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
        perror("tcsetattr");
        exit(1);
    }
}

static void forward_io(int master_fd) {
    fd_set rfds;
    char buf[4096];
    ssize_t n;

    while (1) {
        FD_ZERO(&rfds);
        FD_SET(STDIN_FILENO, &rfds);
        FD_SET(master_fd, &rfds);

        int maxfd = (master_fd > STDIN_FILENO) ? master_fd : STDIN_FILENO;

        if (select(maxfd + 1, &rfds, NULL, NULL, NULL) < 0) {
            if (errno == EINTR) continue;
            perror("select");
            exit(1);
        }

        if (FD_ISSET(STDIN_FILENO, &rfds)) {
            n = read(STDIN_FILENO, buf, sizeof(buf));
            if (n <= 0) {
                if (n < 0) perror("read stdin");
                break;
            }
            if (write(master_fd, buf, n) != n) {
                perror("write to master");
                exit(1);
            }
        }

        if (FD_ISSET(master_fd, &rfds)) {
            n = read(master_fd, buf, sizeof(buf));
            if (n <= 0) {
                if (n < 0 && errno != EIO) perror("read master");
                break;
            }
            if (write(STDOUT_FILENO, buf, n) != n) {
                perror("write to stdout");
                exit(1);
            }
        }
    }
}

int main(int argc, char *argv[]) {
    int master_fd;
    char *slave_name;
    struct winsize ws;

    master_fd = posix_openpt(O_RDWR | O_NOCTTY);
    if (master_fd < 0) {
        perror("posix_openpt");
        exit(1);
    }

    if (grantpt(master_fd) < 0) {
        perror("grantpt");
        exit(1);
    }

    if (unlockpt(master_fd) < 0) {
        perror("unlockpt");
        exit(1);
    }

    slave_name = ptsname(master_fd);
    if (!slave_name) {
        perror("ptsname");
        exit(1);
    }

    if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) {
        ioctl(master_fd, TIOCSWINSZ, &ws);
    }

    fprintf(stderr, "TAKETTY_PTY=%s\n", slave_name);
    fflush(stderr);

    set_raw_mode(STDIN_FILENO);

    forward_io(master_fd);

    close(master_fd);
    return 0;
}