home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume23 / sps2 / part02 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  4.1 KB  |  156 lines

  1. # ifndef lint
  2. static char SccsId[] =  "@(#)main.c    1.1\t10/1/88" ;
  3. # endif
  4.  
  5. # include       "sps.h"
  6. # include       "flags.h"
  7. # ifdef KVM
  8. # include       <kvm.h>
  9. # include       <fcntl.h>
  10. # endif KVM
  11. # ifndef SUNOS40
  12. # include       <h/text.h>
  13. # endif
  14. # include       <sys/stat.h>
  15. # include       <stdio.h>
  16.  
  17.  
  18. /* SPS - Show Process Status */
  19.  
  20. /* J. R. Ward - Hasler AG, Bern, Switzerland         - 24 May 1985 */
  21. /*                                  - 26 Nov 1986 */
  22. /* J. R. Ward - Olsen & Associates, Zuerich, Switzerland -  1 Oct 1988 */
  23. /* <robert@olsen.uucp> */
  24.  
  25. /* NFS additions and SunOS4.0 support by Alexander Dupuy
  26.    <dupuy@ncs.columbia.edu> and Charlie Kim <cck@cunixc.cc.columbia.edu>.
  27.    Ultrix 2.x support by Rob Lehman at CUCCA. */
  28.  
  29. main ( argc,argv )
  30.  
  31. int                             argc ;
  32. char                            **argv ;
  33.  
  34. {
  35.     register struct process *plist ;
  36.     register struct process *process ;
  37. # ifndef SUNOS40
  38.     register struct text    *text ;
  39. # endif
  40.     int                     flinfo ;
  41.     char            *fileinfo, *filesymbol ;
  42.     struct stat        sinfo, ssymbol ;
  43. # ifdef WARNPASSWD
  44.     struct stat        spasswd ;
  45. # endif
  46.     extern struct flags     Flg ;
  47.     extern struct info      Info ;
  48. # ifdef KVM
  49.     extern kvm_t           *Flkvm ;
  50. # else
  51.     extern int              Flmem ;
  52.     extern int              Flkmem ;
  53.     extern int              Flswap ;
  54. # endif
  55.     char                    *getcore() ;
  56.     struct process          *needed(), *mktree() ;
  57.  
  58.     /* Renice as fast as possible for root only (Suggested by Jeff Mogul,
  59.        gregorio!mogul) */
  60.     if ( !getuid() )
  61.         (void)nice( -40 ) ;
  62.     /* Decode the flag arguments */
  63.     flagdecode( argc, argv ) ;      
  64.     /* Determine the terminal width */
  65.     if ( !Flg.flg_w && !Flg.flg_N && !Flg.flg_i )
  66.         termwidth() ;
  67.     /* Open the cpu physical memory, kernel virtual memory and swap device*/
  68. # ifdef KVM
  69.     Flkvm = kvm_open( Flg.flg_s, Flg.flg_k, NULL, O_RDONLY, "sps" ) ;
  70. # else
  71.     if ( Flg.flg_k )
  72.     {
  73.         Flmem = openfile( Flg.flg_k ) ;
  74.         Flkmem = Flmem ;
  75.     }
  76.     else
  77.     {
  78.         Flmem = openfile( FILE_MEM ) ;
  79.         Flkmem = openfile( FILE_KMEM ) ;
  80.         if ( !Flg.flg_o )
  81.             Flswap = openfile( FILE_SWAP ) ;
  82.     }
  83. # endif
  84.     if ( Flg.flg_i )
  85.     {       /* -i flag for info file initialisation */
  86.         initialise() ;          
  87.         exit( 0 ) ;
  88.     }
  89.     /* Check that the information file is newer than the symbol and
  90.        password files, suggested by gregorio!mogul */
  91.     fileinfo = Flg.flg_j ? Flg.flg_j : FILE_INFO ;
  92.     filesymbol = Flg.flg_s ? Flg.flg_s : FILE_SYMBOL ;
  93.     flinfo = openfile( fileinfo ) ;
  94.     (void)fstat( flinfo, &sinfo ) ;
  95.     if ( !stat( filesymbol, &ssymbol ) &&
  96.         sinfo.st_mtime < ssymbol.st_mtime )
  97.         fprintf( stderr,
  98.            "sps - WARNING: Info file `%s' is older than symbol file `%s'\n",
  99.             fileinfo, filesymbol ) ;
  100. # ifdef WARNPASSWD
  101.     if ( !stat( FILE_PASSWD, &spasswd ) &&
  102.         sinfo.st_mtime < spasswd.st_mtime )
  103.         fprintf( stderr,
  104.            "sps - WARNING: Info file `%s' is older than passwd file `%s'\n",
  105.             fileinfo, FILE_PASSWD ) ;
  106. # endif
  107.     /* Read the information file */
  108.     if ( read( flinfo, (char*)&Info, sizeof( struct info ) )
  109.     != sizeof( struct info ) )
  110.     {
  111.         fprintf( stderr, "sps - Can't read info file `%s'", fileinfo ) ;
  112.         sysperror() ;
  113.     }
  114.     (void)close( flinfo ) ;
  115.     /* Find current tty status */
  116.     ttystatus() ;                   
  117.     /* Now that we know the available ttys, decode the flags */
  118.     flagsetup() ;                   
  119.     process = (struct process*)getcore(Info.i_nproc*sizeof(struct process));
  120. # ifndef SUNOS40
  121.     text = (struct text*)getcore( Info.i_ntext * sizeof( struct text ) ) ;
  122. # endif
  123.     do
  124.     {       /* Read current process status */
  125. # ifdef SUNOS40
  126.         readstatus( process ) ;
  127.         /* Select those processes to be listed */
  128.         plist = needed( process ) ;
  129. # else
  130.         readstatus( process, text ) ;
  131.         /* Select those processes to be listed */
  132.         plist = needed( process, text ) ;
  133. # endif
  134.         /* Form a tree of listed processes */
  135.         plist = mktree( process, plist ) ;
  136.         if ( !Flg.flg_N )
  137.         {       /* Print the processes */
  138.             prheader() ;
  139.             printall( plist, 0 ) ;
  140.         }
  141.         prsummary() ;
  142.         (void)fflush( stdout ) ;
  143.         if ( Flg.flg_r )        
  144.         {       /* If repeating, again get tty status */
  145.             ttystatus() ;
  146.             if ( Flg.flg_rdelay )
  147. # ifdef BSD42
  148.                 sleep( Flg.flg_rdelay ) ;
  149. # else
  150.                 sleep( (int)Flg.flg_rdelay ) ;
  151. # endif
  152.         }
  153.     } while ( Flg.flg_r ) ;
  154.     exit( 0 ) ;
  155. }
  156.