home *** CD-ROM | disk | FTP | other *** search
/ Boot Disc 8 / boot-disc-1997-04.iso / PDA_Soft / Psion / comms / p3nfs / nfsd / pty.c < prev   
C/C++ Source or Header  |  1996-04-04  |  3KB  |  184 lines

  1. #include "config.h"
  2. #ifdef __svr4__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <strings.h>
  6. #endif
  7.  
  8. #ifndef PTY
  9. void
  10. shell_feed(i)
  11.   int i;
  12. {
  13.   printf("shellfeed: psionbyte %#x (%c)\n", i, i);
  14. }
  15.  
  16. void
  17. init_pty()
  18. {
  19. }
  20. #else /* PTY */
  21.  
  22. #ifdef linux
  23. #include <sys/ioctl.h>    /* Linux ioctl */
  24. #include <stdio.h>    /* Linux printf */
  25. #endif
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <sys/termios.h>
  31.  
  32. #include "nfs_prot.h"
  33. #include "mp.h"
  34.  
  35. int masterfd = -1;
  36.  
  37. /* open a pty and start a shell in it */
  38. void
  39. init_pty()
  40. {
  41.   int fd;
  42.   char name[64], fnd[3], *s, *t;
  43.   extern char *shell;
  44.   struct termios tio;
  45.   char **newenv;
  46.   int n, nn;
  47.   extern char **environ;
  48.   char term[16];
  49.  
  50.   /* first of all check if the shell is executable */
  51.   if(access(shell, X_OK))
  52.     {
  53.       perror(shell);
  54.       return;
  55.     }
  56.  
  57.   if (tcgetattr(0, &tio))
  58.     {
  59.       perror("tcgetattr");
  60.       return;
  61.     }
  62.  
  63. #ifdef __sgi
  64.   s = _getpty(&fd, O_RDWR | O_NOCTTY, 0666, 1);
  65.   if (s == 0)
  66.     return;
  67.   strcpy(name, s);
  68. #else
  69.   fd = -1;
  70.   for(s = "pqrs"; fd < 0 && *s; s++)
  71.     for(t = "01234567890abcdef"; *t; t++)
  72.       {
  73.     sprintf(fnd, "%c%c", *s, *t);
  74.     sprintf(name, "/dev/pty%s", fnd);
  75.     if((fd = open(name, O_RDWR)) >= 0)
  76.       break;
  77.       }
  78.   sprintf(name, "/dev/tty%s", fnd);
  79. #endif
  80.  
  81.   if(fd < 0)
  82.     {
  83.       perror("open pty");
  84.       return;
  85.     }
  86.  
  87.   switch(fork())
  88.     {
  89.     case -1:
  90.       perror("fork");
  91.       close(fd);
  92.       return;
  93.     case 0:
  94.       break;
  95.     default:
  96.       masterfd = fd;
  97.       return;
  98.     }
  99.   close(fd);
  100.  
  101.   setsid();    /* create new session, disconnect from ctty */
  102.  
  103.   close(0);
  104.   if(open(name, O_RDWR) != 0)
  105.     {
  106.       perror(name);
  107.       exit(1);
  108.     }
  109.   close(1);
  110.   close(2);
  111.   dup(0);
  112.   dup(0);
  113.  
  114.   tcsetattr(0, TCSANOW, &tio);
  115.  
  116.   setgid(root_fattr.gid);
  117.   setuid(root_fattr.uid);
  118.  
  119.   /* count the environment */
  120.   for (n = 0; environ[n]; n++)
  121.     ;
  122.   if ((newenv = (char **)malloc((n + 2) * sizeof(char *))) == 0)
  123.     {
  124.       perror("env malloc");
  125.       exit(1);
  126.     }
  127.   for (n = nn = 0; environ[n]; n++)
  128.     {
  129.       if (strncmp(environ[n], "TERM=", 5) == 0)
  130.     continue;
  131.       if (strncmp(environ[n], "TERMCAP=", 8) == 0)
  132.     continue;
  133.       if (strncmp(environ[n], "LINES=", 6) == 0)
  134.     continue;
  135.       if (strncmp(environ[n], "COLUMNS=", 8) == 0)
  136.     continue;
  137.       newenv[nn++] = environ[n];
  138.     }
  139.  
  140.   sprintf(term, "TERM=vt220");
  141.   newenv[nn++] = term;
  142.   newenv[nn] = 0;
  143.  
  144.   execle(shell, shell, 0, newenv);
  145.   perror(shell);
  146.   exit(1);
  147. }
  148.  
  149. void
  150. shell_feed(i)
  151.   int i;
  152. {
  153.   static int resize = 0;
  154.   unsigned char c = i;
  155.   static struct winsize ws;
  156.  
  157.   if(debug > 1) printf("shellfeed: psionbyte %#x (%c)\n", i, i);
  158.   if(masterfd < 0)
  159.     return;
  160.  
  161.   switch(resize)
  162.     {
  163.       case 0:
  164.         if(i == 0)        /* resize prefix */
  165.       resize++;
  166.     else
  167.       write(masterfd, &c, 1);
  168.     break;
  169.       case 1:             /* receiving x */
  170.         ws.ws_xpixel = 0; ws.ws_ypixel = 0;
  171.         ws.ws_row = i;
  172.     resize++;
  173.     return;
  174.       case 2:            /* receiving y */
  175.         ws.ws_col = i;
  176.     resize = 0;
  177.         if(debug) printf("Resize to %d x %d\n", ws.ws_row, ws.ws_col);
  178.     ioctl(masterfd, TIOCSWINSZ, (char *)&ws);
  179.     return;
  180.     }
  181. }
  182.  
  183. #endif /* PTY */
  184.