From 550f057dcf1713c5e1d4b55d2108ed69ca9c28d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 14 Jul 2021 19:54:17 +0200 Subject: Implement rtty in rio.c, rename to rtty.c --- Makefile | 6 ++-- rio.c | 90 ---------------------------------------------- rtty | 12 ------- rtty.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 105 deletions(-) delete mode 100644 rio.c delete mode 100755 rtty create mode 100644 rtty.c diff --git a/Makefile b/Makefile index 0554b1c..98a4a14 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -rio: rio.c +rtty: rtty.c -install: rio - install rtty rio /usr/local/bin +install: rtty + install rtty /usr/local/bin diff --git a/rio.c b/rio.c deleted file mode 100644 index a36af2b..0000000 --- a/rio.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define MAXBUF 2048 - -int -main() -{ - char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo; - fd_set rfds0, rfds1; - int c, fdin, fdout, flags, i, nin, nout, state; - struct timeval tv; - - if(!(fdin = open("/var/tmp/r.in", O_WRONLY))) - err(1, "open"); - if(!(fdout = open("/var/tmp/r.out", O_RDONLY))) - err(1, "open"); - - dprintf(fdin, "export TERM=tty43 PAGER=cat EDITOR=ed\n"); - - FD_ZERO(&rfds0); - FD_ZERO(&rfds1); - - tv.tv_sec = 0; - tv.tv_usec = 1; - - for(;;){ - /* - * User input is read from standard in and copied the - * input pipe. It is saved in bufin for later use. - */ - FD_SET(0, &rfds0); - if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ - nout = read(0, bufin, MAXBUF); - bufin[nout] = 0; - dprintf(fdin, "%s", bufin); - fflush(stdout); - } - - /* - * System output is read from the output pipe and copied - * to standard out for the user to see. - */ - FD_SET(fdout, &rfds1); - if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){ - nout = read(fdout, bufout, MAXBUF); - if(!nout) continue; - bufout[nout] = 0; - - /* - * Bufin and bufout are copied to the temporary - * pointers bi and bo. Bo is incremented in order - * to skip any potential repetition of the command - * given by the user on standard in. (It is assumed - * that the typed command fits in bufin, i.e. is - * not larger than MAXBUF.) - */ - bi = bufin; - bo = bufout; - for(;;){ - if(*bo == '\n'){ - bo++; - break; - } - if(*bo == 13){ - bo++; - continue; - } - if(*bi != *bo){ - bo = bufout; - break; - } - bi++; bo++; - } - - /* - * Bo is printed and bufin is cleared, so as not - * to perform the skip again, incorrectly. - */ - printf("%s", bo); - fflush(stdout); - bufin[0] = 0; - } - } - -} diff --git a/rtty b/rtty deleted file mode 100755 index f6b6296..0000000 --- a/rtty +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# rtty -- limited but responsive remote shell - -in=/var/tmp/r.in -out=/var/tmp/r.out -rm $in $out 2>/dev/null -mkfifo $in $out - -<$in 2>&1 >$out ssh -oBatchMode=yes -tt "$@" & -trap "kill $! 2>/dev/null" INT QUIT EXIT -rio diff --git a/rtty.c b/rtty.c new file mode 100644 index 0000000..9f64c6d --- /dev/null +++ b/rtty.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAXBUF 2048 + +int +main(int argc, char *argv[]) +{ + char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo, in[30], **nargv, out[30]; + fd_set rfds0, rfds1; + int fdin, fdout, i, n; + struct timeval tv; + + /* Create named pipes. */ + sprintf(in, "/var/tmp/rtty.in.%d", getpid()); + sprintf(out, "/var/tmp/rtty.out.%d", getpid()); + if(mkfifo(in, 0644) == -1 || mkfifo(out, 0664) == -1) + if(errno != EEXIST) + err(1, "mkfifo"); + + if(fork() == 0){ + /* Redirect standard in, out. */ + if(!(fdin = open(in, O_RDONLY))) + err(1, "open"); + if(!(fdout = open(out, O_WRONLY))) + err(1, "open"); + dup2(fdin, 0); + dup2(fdout, 1); + + /* Create new argument vector. */ + if(!(nargv = malloc(sizeof(char *)*(argc+1)))) + err(1, "malloc"); + nargv[0] = "ssh"; + nargv[1] = "-tt"; + for(i = 1; i < argc; i++) + nargv[i+1] = argv[i]; + nargv[argc+1] = NULL; + + /* Exec into ssh. */ + execvp("ssh", nargv); + err(1, "execvp"); + } + + if(!(fdin = open(in, O_WRONLY))) + err(1, "open"); + if(!(fdout = open(out, O_RDONLY))) + err(1, "open"); + + dprintf(fdin, "export TERM=tty43 PAGER=cat EDITOR=ed\n"); + + FD_ZERO(&rfds0); + FD_ZERO(&rfds1); + + tv.tv_sec = 0; + tv.tv_usec = 1; + + for(;;){ + /* + * User input is read from standard in and copied the + * input pipe. It is saved in bufin for later use. + */ + FD_SET(0, &rfds0); + if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ + n = read(0, bufin, MAXBUF); + bufin[n] = 0; + dprintf(fdin, "%s", bufin); + fflush(stdout); + } + + /* + * System output is read from the output pipe and copied + * to standard out for the user to see. + */ + FD_SET(fdout, &rfds1); + if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){ + n = read(fdout, bufout, MAXBUF); + if(!n) continue; + bufout[n] = 0; + + /* + * Bufin and bufout are copied to the temporary + * pointers bi and bo. Bo is incremented in order + * to skip any potential repetition of the command + * given by the user on standard in. (It is assumed + * that the typed command fits in bufin, i.e. is + * not larger than MAXBUF.) + */ + bi = bufin; + bo = bufout; + for(;;){ + if(*bo == '\n'){ + bo++; + break; + } + if(*bo == 13){ + bo++; + continue; + } + if(*bi != *bo){ + bo = bufout; + break; + } + bi++; bo++; + } + + /* + * Bo is printed and bufin is cleared, so as not + * to perform the skip again, incorrectly. + */ + printf("%s", bo); + fflush(stdout); + bufin[0] = 0; + } + } + +} -- cgit v1.2.3