home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / pcomm2 / part07 / x_extrnl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  1.5 KB  |  75 lines

  1. /*
  2.  * Spawn a shell with the stdin and stdout swapped with the remote
  3.  * system.  An undocumented feature:  The external protocol gateway
  4.  * can be used to pipe the output of a normal Unix command to the
  5.  * remote system.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include <curses.h>
  11. #include <fcntl.h>
  12. #include "config.h"
  13.  
  14. void
  15. extrnl(cmd)
  16. char *cmd;
  17. {
  18.     extern int fd;
  19.     WINDOW *xt_win, *newwin();
  20.     int (*istat)(), (*qstat)(), status, epid, w;
  21.     char *shell, *shellpath, *getenv(), *strrchr(), buf[40], *ttyname();
  22.     char *strcpy();
  23.     unsigned int sleep();
  24.     void _exit(), input_off();
  25.  
  26.     input_off();
  27.                     /* a full window */
  28.     xt_win = newwin(LINES, COLS, 0, 0);
  29.     touchwin(xt_win);
  30.     wrefresh(xt_win);
  31.                     /* out of curses mode */
  32.     resetterm();
  33.  
  34.     shellpath = getenv("SHELL");
  35.     if (shellpath == NULL || *shellpath == NULL)
  36.         shellpath = "/bin/sh";
  37.  
  38.     shell = strrchr(shellpath, '/') + 1;
  39.  
  40.     if (!(epid = fork())) {
  41.                         /* recreate the device name */
  42.         strcpy(buf, ttyname(fd));
  43.         close(fd);
  44.                         /* swap the stdin */
  45.         close(0);
  46.         open(buf, O_RDONLY);
  47.                         /* swap the stdout */
  48.         close(1);
  49.         open(buf, O_WRONLY);
  50. #ifdef SETUGID
  51.         setgid(getgid());
  52.         setuid(getuid());
  53. #endif /* SETUGID */
  54.         execl(shellpath, shell, "-c", cmd, (char *) 0);
  55.         _exit(1);
  56.     }
  57.     istat = signal(SIGINT, SIG_IGN);
  58.     qstat = signal(SIGQUIT, SIG_IGN);
  59.  
  60.     while ((w = wait(&status)) != epid && w != -1)
  61.         ;
  62.  
  63.     signal(SIGINT, istat);
  64.     signal(SIGQUIT, qstat);
  65.                     /* back to curses mode */
  66.     sleep(1);
  67.     fixterm();
  68.  
  69.     clearok(curscr, TRUE);
  70.     werase(xt_win);
  71.     wrefresh(xt_win);
  72.     delwin(xt_win);
  73.     return;
  74. }
  75.