home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / programm / 4576 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.3 KB  |  102 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!agate!pasteur!cory.Berkeley.EDU!achoi
  3. From: achoi@cory.Berkeley.EDU (Andrew Choi)
  4. Subject: Problem with Pseudo TTY of SunOS 4.1.1 for Sun3
  5. Message-ID: <1992Sep9.010847.1104@pasteur.Berkeley.EDU>
  6. Sender: nntp@pasteur.Berkeley.EDU (NNTP Poster)
  7. Nntp-Posting-Host: cory
  8. Organization: University of California, at Berkeley
  9. Distribution: usa
  10. Date: Wed, 9 Sep 1992 01:08:47 GMT
  11. Lines: 89
  12.  
  13. Hi,
  14.  
  15.   When I opened a pty pair in SunOS 4.1.1 for Sun3, the master tty
  16. is not recognized as a tty, thus causing standard output buffering
  17. problem.  The result of running the following program is:
  18.  
  19. Master is not a tty
  20. Slave is a tty
  21.  
  22.   I tried the same program again under other 4.3BSD dervied Unix'es,
  23. and they give me instead:
  24.  
  25. Master is a tty
  26. Slave is a tty
  27.  
  28.   Is this a bug with SunOS, or do I need to do something special
  29. in making the master pty a tty as well?
  30.  
  31.   Thanks for your help.
  32.  
  33. XXXXXXXXXXXXXXXXXXXXXXXX CUT HERE XXXXXXXXXXXXXXXXXXXXXXXX
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40.  
  41. #define TTY_NAME        "/dev/TtyXY"
  42. #define TTY_POS_T        5
  43. #define TTY_POS_X        8
  44. #define TTY_POS_Y        9
  45.  
  46. /* Open a master pty and the corresponding slave tty.  Return 0 if */
  47. /* OK, or -1 if cannot open a valid pty pair */
  48. int
  49. OpenPty(int *master, int *slave)
  50. {
  51.   char                ttyname[] = TTY_NAME;
  52.   char                xnames[] = "pqrs";
  53.   char                ynames[] = "0123456789abcdef";
  54.   int                x, y;
  55.   
  56.   for (x = 0; xnames[x]; x++) {
  57.     ttyname[TTY_POS_X] = xnames[x];
  58.     for (y = 0; ynames[y]; y++) {
  59.       ttyname[TTY_POS_Y] = ynames[y];
  60.  
  61.       /* Open Master (control) PTY */
  62.       ttyname[TTY_POS_T] = 'p';
  63.       if ((*master = open(ttyname, O_WRONLY)) < 0)
  64.     continue;
  65.  
  66.       /* Open Slave PTY */
  67.       ttyname[TTY_POS_T] = 't';
  68.       if ((*slave = open(ttyname, O_RDONLY)) < 0)
  69.     (void) close(*master);
  70.       else 
  71.     return 0;
  72.     }
  73.   }
  74.  
  75.   return -1;
  76. }
  77.  
  78.  
  79. int
  80. main(int argc, char *argv[])
  81. {
  82.   int                master, slave;
  83.  
  84.   if (OpenPty(&master, &slave) < 0) {
  85.     fputs("Failure to open pty\n", stderr);
  86.     exit(-1);
  87.   }
  88.  
  89.   if (isatty(master))
  90.     puts("Master is a tty");
  91.   else
  92.     puts("Master is not a tty");
  93.  
  94.   if (isatty(slave))
  95.     puts("Slave is a tty");
  96.   else
  97.     puts("Slave is not a tty");
  98.  
  99.   exit(0);
  100. }
  101.  
  102.