From 0fe28fd69dd15006ccd684d563699420aadde61a Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 3 Oct 2006 01:37:17 +0000 Subject: first working version of uuterm! - at this point it is purely experimental. only ascii characters are visible (using builtin font) although all characters are processed. - there are known bugs, including crashes. - there are major missing features. - but it works. ^_^ --- tty.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tty.c (limited to 'tty.c') diff --git a/tty.c b/tty.c new file mode 100644 index 0000000..8a2ccda --- /dev/null +++ b/tty.c @@ -0,0 +1,63 @@ +/* uuterm, Copyright (C) 2006 Rich Felker; licensed under GNU GPL v2 only */ + +#include +#include +#include +#include +#include +#include + +void uutty_resize(int fd, int w, int h) +{ +#ifdef TIOCSWINSZ + struct winsize ws = { }; + ws.ws_col = w; + ws.ws_row = h; + ioctl(fd, TIOCSWINSZ, &ws); +#endif +} + +/* #define posix_openpt(x) open("/dev/ptmx", x) */ + +int uutty_open(char **cmd, int w, int h) +{ + int ptm, pts; + struct stat st; + + if (!cmd || !cmd[0] || stat(cmd[0], &st) < 0 + || !S_ISCHR(st.st_mode) + || (ptm = open(cmd[0], O_RDWR|O_NOCTTY)) < 0 + || (isatty(ptm) ? 0 : (close(ptm),1)) ) { + + if ((ptm = posix_openpt(O_RDWR|O_NOCTTY)) < 0) + return -1; + if (grantpt(ptm) < 0 || unlockpt(ptm) < 0) { + close(ptm); + return -1; + } + + switch(fork()) { + case -1: + close(ptm); + return -1; + case 0: + setsid(); + pts = open(ptsname(ptm), O_RDWR); + close(ptm); + dup2(pts, 0); + dup2(pts, 1); + dup2(pts, 2); + if (pts > 2) close(pts); + // FIXME............................ + if (cmd) execvp(cmd[0], cmd); + else execl("/bin/sh", "-sh", (char *)0); + return 1; + } + + uutty_resize(ptm, w, h); + } + + fcntl(ptm, F_SETFL, fcntl(ptm, F_GETFL)|O_NONBLOCK); + + return ptm; +} -- cgit v1.2.1