home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / sps / part2 / main.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.6 KB  |  95 lines

  1. # include       "sps.h"
  2. # include       "flags.h"
  3. # include       <h/text.h>
  4. # include       <stdio.h>
  5.  
  6. /* SPS - Show Process Status */
  7. /* J. R. Ward - Hasler AG Bern - 24 May 1985 */
  8. main ( argc,argv )
  9.  
  10. int                             argc ;
  11. char                            **argv ;
  12.  
  13. {
  14.     register struct process *plist ;
  15.     register struct process *process ;
  16.     register struct text    *text ;
  17.     int                     flinfo ;
  18.     extern struct flags     Flg ;
  19.     extern struct info      Info ;
  20.     extern int              Flmem ;
  21.     extern int              Flkmem ;
  22.     extern int              Flswap ;
  23.     char                    *getcore() ;
  24.     struct process          *needed(), *mktree() ;
  25.  
  26.     /* Renice as fast as possible for root (Suggested by Gregorio!mogul) */
  27.     if ( !getuid() )
  28.         (void)nice( -40 ) ;
  29.     /* Decode the flag arguments */
  30.     flagdecode( argc, argv ) ;      
  31.     /* Determine the terminal width */
  32.     if ( !Flg.flg_w && !Flg.flg_N && !Flg.flg_i )
  33.         termwidth() ;
  34.     /* Open the cpu physical memory, kernel virtual memory and swap device*/
  35.     if ( Flg.flg_k )
  36.     {
  37.         Flmem = openfile( Flg.flg_k ) ;
  38.         Flkmem = Flmem ;
  39.     }
  40.     else
  41.     {
  42.         Flmem = openfile( FILE_MEM ) ;
  43.         Flkmem = openfile( FILE_KMEM ) ;
  44.         if ( !Flg.flg_o )
  45.             Flswap = openfile( FILE_SWAP ) ;
  46.     }
  47.     if ( Flg.flg_i )
  48.     {       /* -i flag for info file initialisation */
  49.         initialise() ;          
  50.         exit( 0 ) ;
  51.     }
  52.     /* Read the information file */
  53.     flinfo = openfile( Flg.flg_j ? Flg.flg_j : FILE_INFO ) ;
  54.     if ( read( flinfo, (char*)&Info, sizeof( struct info ) )
  55.     != sizeof( struct info ) )
  56.     {
  57.         fprintf( stderr, "sps - Can't read info file %s",
  58.             Flg.flg_j ? Flg.flg_j : FILE_INFO ) ;
  59.         sysperror() ;
  60.     }
  61.     (void)close( flinfo ) ;
  62.     /* Find current tty status */
  63.     ttystatus() ;                   
  64.     /* Now that we know the available ttys, decode the flags */
  65.     flagsetup() ;                   
  66.     process = (struct process*)getcore(Info.i_nproc*sizeof(struct process));
  67.     text = (struct text*)getcore( Info.i_ntext * sizeof( struct text ) ) ;
  68.     do
  69.     {       /* Read current process and text status */
  70.         readstatus( process, text ) ;
  71.         /* Select those processes to be listed */
  72.         plist = needed( process, text ) ;
  73.         /* Form a tree of listed processes */
  74.         plist = mktree( process, plist ) ;
  75.         if ( !Flg.flg_N )
  76.         {       /* Print the processes */
  77.             prheader() ;
  78.             printall( plist, 0 ) ;
  79.         }
  80.         prsummary() ;
  81.         (void)fflush( stdout ) ;
  82.         if ( Flg.flg_r )        
  83.         {       /* If repeating, again get tty status */
  84.             ttystatus() ;
  85.             if ( Flg.flg_rdelay )
  86. # ifdef BSD42
  87.                 sleep( Flg.flg_rdelay ) ;
  88. # else
  89.                 sleep( (int)Flg.flg_rdelay ) ;
  90. # endif
  91.         }
  92.     } while ( Flg.flg_r ) ;
  93.     exit( 0 ) ;
  94. }
  95.