home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / sps / part1 / inittty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.5 KB  |  129 lines

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