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

  1. # include       "sps.h"
  2. # include       "flags.h"
  3. # include       <pwd.h>
  4. # include       <stdio.h>
  5.  
  6. /*
  7. ** INITIALISE - Called to reset the `Info' structure with new kernel
  8. ** addresses and user and tty information.
  9. */
  10. initialise ()
  11. {
  12.     register FILE           *fd ;
  13.     char                    *fileinfo ;
  14.     extern struct flags     Flg ;
  15.     extern struct info      Info ;
  16.     FILE                    *fopen() ;
  17.  
  18.     fileinfo = Flg.flg_j ? Flg.flg_j : FILE_INFO ;
  19.     /* Read kernel addresses */
  20.     initsymbols() ;                 
  21.     /* Read user names */
  22.     initusers() ;                   
  23.     (void)umask( ~0644 ) ;          
  24.     if ( !(fd = fopen( fileinfo, "w" )) )
  25.     {
  26.         fprintf( stderr, "sps - Can't create info file %s", fileinfo ) ;
  27.         sysperror() ;
  28.     }
  29.     /* Find tty addresses */
  30.     inittty() ;                     
  31.     if ( fwrite( (char*)&Info, sizeof( struct info ), 1, fd ) != 1 )
  32.     {
  33.         fprintf( stderr, "sps - Can't write info file %s", fileinfo ) ;
  34.         sysperror() ;
  35.         exit( 1 ) ;
  36.     }
  37.     (void)fclose( fd ) ;
  38.     printf( "sps is initialised\n" ) ;
  39. }
  40.  
  41. /* INITUSERS - Read the passwd file and fill in the user name arrays */
  42. initusers ()
  43. {
  44.     register struct passwd  *pw ;
  45.     register struct hashtab *hp ;
  46.     struct passwd           *getpwent() ;
  47.     char                    *strncpy() ;
  48.     struct hashtab          *hashuid(), *hashnext() ;
  49.  
  50.     while ( pw = getpwent() )
  51.     {       /* For each user in the passwd file, first see if that uid
  52.            has been already allocated in the hash table. */
  53.         if ( hp = hashuid( pw->pw_uid ) )
  54.         {
  55.             fprintf( stderr,
  56.            "sps - Names %s and %s conflict in passwd file for uid %d\n",
  57.                 hp->h_uname, pw->pw_name, pw->pw_uid ) ;
  58.             continue ;
  59.         }
  60.         /* Try to find a free slot in the hash table and fill it. */
  61.         if ( !(hp = hashnext( pw->pw_uid )) )
  62.             prexit( "sps - Too many users in passwd file\n" ) ;
  63.         hp->h_uid = pw->pw_uid ;
  64.         (void)strncpy( hp->h_uname, pw->pw_name, UNAMELEN ) ;
  65.     }
  66.     (void)endpwent() ;
  67. }
  68.