diff options
-rw-r--r-- | Makefile | 6 | ||||
-rwxr-xr-x | rtty | 12 | ||||
-rw-r--r-- | rtty.c (renamed from rio.c) | 53 |
3 files changed, 46 insertions, 25 deletions
@@ -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 @@ -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 @@ -1,23 +1,56 @@ #include <err.h> +#include <errno.h> #include <fcntl.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/select.h> +#include <sys/stat.h> #include <unistd.h> #define MAXBUF 2048 int -main() +main(int argc, char *argv[]) { - char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo; + char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo, in[30], **nargv, out[30]; fd_set rfds0, rfds1; - int c, fdin, fdout, flags, i, nin, nout, state; + int fdin, fdout, i, n; struct timeval tv; - if(!(fdin = open("/var/tmp/r.in", O_WRONLY))) + /* 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("/var/tmp/r.out", O_RDONLY))) + if(!(fdout = open(out, O_RDONLY))) err(1, "open"); dprintf(fdin, "export TERM=tty43 PAGER=cat EDITOR=ed\n"); @@ -35,8 +68,8 @@ main() */ FD_SET(0, &rfds0); if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ - nout = read(0, bufin, MAXBUF); - bufin[nout] = 0; + n = read(0, bufin, MAXBUF); + bufin[n] = 0; dprintf(fdin, "%s", bufin); fflush(stdout); } @@ -47,9 +80,9 @@ main() */ FD_SET(fdout, &rfds1); if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){ - nout = read(fdout, bufout, MAXBUF); - if(!nout) continue; - bufout[nout] = 0; + n = read(fdout, bufout, MAXBUF); + if(!n) continue; + bufout[n] = 0; /* * Bufin and bufout are copied to the temporary |