home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / sps / part02 / inittty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-21  |  3.5 KB  |  130 lines

  1. # include       "sps.h"
  2. # include       <h/conf.h>
  3. # include       <h/ioctl.h>
  4. # include       <h/tty.h>
  5. # include       <sys/stat.h>
  6. # include       <stdio.h>
  7.  
  8. /* INITTTY - Initialise the tty part of the info structure */
  9. inittty ()
  10. {
  11.     register struct ttyline *lp ;
  12. # ifdef BSD42
  13.     register struct direct  *dp ;
  14.     DIR                     *dfd ;
  15. # else
  16.     struct direct           dir ;
  17.     FILE                    *dfd ;
  18. # endif
  19.     struct stat             statbuf ;
  20.     static char             filedev[] = FILE_DEV ;
  21.     extern struct info      Info ;
  22.     extern int              Flkmem ;
  23. # ifdef BSD42
  24.     DIR                     *opendir() ;
  25.     struct direct           *readdir() ;
  26. # else
  27.     FILE                    *fopen() ;
  28. # endif
  29.  
  30.     lp = Info.i_ttyline ;
  31. # ifdef BSD42
  32.     if ( !(dfd = opendir( filedev )) )
  33. # else
  34.     if ( !(dfd = fopen( filedev, "r" )) )
  35. # endif
  36.         prexit( "Can't open %s\n", filedev ) ;
  37.     if ( chdir( filedev ) < 0 )
  38.         prexit( "sps - Can't chdir to %s\n", filedev ) ;
  39. # ifdef BSD42
  40.     /* Read all entries in the device directory, looking for ttys */
  41.     while ( dp = readdir( dfd ) )
  42.     {       /* Skip entries that do not match "tty" or "console" */
  43.         if ( strncmp( "tty", dp->d_name, 3 )
  44.         &&   strcmp( "console", dp->d_name ) )
  45.             continue ;
  46.         /* Skip "tty" itself */
  47.         if ( dp->d_namlen == 3 )
  48.             continue ;
  49. # ifdef CHAOS
  50.         /* Skip chaos ttys ; they are accessed during ttystatus() */
  51.         if ( dp->d_namelen > 3 &&
  52.         dp->d_name[ sizeof( "tty" ) - 1 ] == 'C' )
  53.             continue ;
  54. # endif
  55.         if ( lp >= &Info.i_ttyline[ MAXTTYS ] )
  56.             prexit( "sps - Too many ttys in %s\n", filedev ) ;
  57.         /* Copy the tty name into the information entry */
  58.         if ( !strcmp( dp->d_name, "console" ) )
  59.         {
  60.             lp->l_name[0] = 'c' ;
  61.             lp->l_name[1] = 'o' ;
  62.         }
  63.         else
  64.         {
  65.             lp->l_name[0] = dp->d_name[3] ;
  66.             lp->l_name[1] = dp->d_name[4] ;
  67.         }
  68.         /* Ensure that this tty is actually a valid character device */
  69.         if ( stat( dp->d_name, &statbuf ) < 0 )
  70.             continue ;
  71. # else
  72.     /* Read all entries in the device directory, looking for ttys */
  73.     while ( fread( (char*)&dir, sizeof( struct direct ), 1, dfd ) == 1 )
  74.     {       /* Skip entries that do not match "tty" or "console" */
  75.         if ( strncmp( "tty", dir.d_name, 3 )
  76.         &&   strcmp( "console", dir.d_name ) )
  77.             continue ;
  78.         /* Skip "tty" itself */
  79.         if ( dir.d_name[3] == '\0' )
  80.             continue ;
  81. # ifdef CHAOS
  82.         /* Skip chaos ttys ; they are accessed during ttystatus() */
  83.         if ( dir.d_name[ sizeof( "tty" ) - 1 ] == 'C' )
  84.             continue ;
  85. # endif
  86.         if ( lp >= &Info.i_ttyline[ MAXTTYS ] )
  87.             prexit( "sps - Too many ttys in %s\n", filedev ) ;
  88.         /* Copy the tty name into the information entry */
  89.         if ( !strcmp( dir.d_name, "console" ) )
  90.         {
  91.             lp->l_name[0] = 'c' ;
  92.             lp->l_name[1] = 'o' ;
  93.         }
  94.         else
  95.         {
  96.             lp->l_name[0] = dir.d_name[3] ;
  97.             lp->l_name[1] = dir.d_name[4] ;
  98.         }
  99.         /* Ensure that this tty is actually a valid character device */
  100.         if ( stat( dir.d_name, &statbuf ) < 0 )
  101.             continue ;
  102. # endif
  103.         if ( (statbuf.st_mode & S_IFMT) != S_IFCHR )
  104.             continue ;
  105.         /* Find the device # of the tty and the address of its
  106.            associated struct tty in /dev/kmem. */
  107.         lp->l_dev = statbuf.st_rdev ;
  108.         memseek( Flkmem,
  109.              (long)&Info.i_cdevsw[ major( statbuf.st_rdev ) ].d_ttys ) ;
  110.         if ( read( Flkmem, (char*)&lp->l_addr, sizeof( lp->l_addr ) )
  111.         != sizeof( lp->l_addr ) )
  112.         {
  113.             fprintf( stderr, "sps - Can't read struct tty for %s\n",
  114. # ifdef BSD42
  115.                 dp->d_name ) ;
  116. # else
  117.                 dir.d_name ) ;
  118. # endif
  119.             continue ;
  120.         }
  121.         lp->l_addr += (int)minor( statbuf.st_rdev ) ;
  122.         lp++ ;
  123.     }
  124. # ifdef BSD42
  125.     (void)closedir( dfd ) ;
  126. # else
  127.     (void)fclose( dfd ) ;
  128. # endif
  129. }
  130.