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

  1. # include       "sps.h"
  2. # include       "flags.h"
  3. # include       <h/text.h>
  4.  
  5. /* PRINTPROC - Pretty print a process according to the switches. */
  6. printproc ( p, md )
  7.  
  8. register struct process         *p ;            
  9. int                             md ;            
  10.  
  11. {
  12.     register char           *chp ;
  13.     register struct text    *tp ;
  14.     register struct hashtab *hp ;
  15.     char                    chbuf[10] ;
  16.     time_t                  time ;
  17.     time_t                  chtime ;
  18. # ifdef BSD42
  19.     time_t                  utime ;
  20.     time_t                  uchtime ;
  21. # endif
  22.     extern short            Lastuid, Lastpgrp ;
  23.     extern struct flags     Flg ;
  24.     char                    *waitingfor() ;
  25.     struct hashtab          *hashuid() ;
  26.     double                  percentmem() ;
  27.  
  28.     /* List tty name and foreground/background/detached information */
  29.     printf( "%2.2s%c", p->pr_tty->l_name,
  30.         !p->pr_p.p_pgrp ? ' ' :
  31. # ifdef SDETACH
  32.         p->pr_p.p_flag & SDETACH ? '_' :
  33. # endif
  34.         p->pr_p.p_pgrp == p->pr_tty->l_pgrp ? '.' : ' ' ) ;
  35.     hp = hashuid( (int)p->pr_p.p_uid ) ;
  36.     if ( !md  )                             
  37.     {       /* If a top-level process, list the user name */
  38.         if ( hp )
  39.             printf( "%-8.8s ", hp->h_uname ) ;
  40.         else
  41.             printf( "user%-4.4d ", p->pr_p.p_uid ) ;
  42.     }
  43.     else                                    
  44.     {       /* Usually list an asterisk for a child process */
  45.         md = md > 8 ? 8 : md ;
  46.         printf( "%*s%c", md, "",
  47.             p->pr_p.p_pgrp == Lastpgrp ? '|' : '*' ) ;      
  48.         /* But beware of setuid processes */
  49.         md = 8 - md ;
  50.         if ( p->pr_p.p_uid == Lastuid )
  51.             printf( "%-*.*s", md, md, "" ) ;
  52.         else if ( hp )
  53.             printf( "%-*.*s", md, md, hp->h_uname ) ;
  54.         else
  55.         {
  56.             md -= 4 ;
  57.             printf( "user%-*.*d", md, md, p->pr_p.p_uid ) ;
  58.         }
  59.     }
  60.     Lastuid = p->pr_p.p_uid ;
  61.     Lastpgrp = p->pr_p.p_pgrp ;
  62.     if ( Flg.flg_d )                        
  63.     {       /* List disc I/O and paging information */
  64.         if ( !p->pr_upag || p->pr_p.p_stat == SZOMB )
  65.         {
  66.             prcmd( p, 49, -63 ) ;
  67.             return ;
  68.         }
  69.         printf( "%2d %8d+%8d %4d %8d %8D ",
  70.             p->pr_files,
  71. # ifdef BSD42
  72.             p->pr_rself.ru_majflt,
  73.             p->pr_rself.ru_minflt,
  74.             p->pr_rself.ru_nswap,
  75.             p->pr_rself.ru_inblock + p->pr_rself.ru_oublock,
  76.             KBYTES( p->pr_rself.ru_idrss + p->pr_rself.ru_isrss
  77.                 + p->pr_rself.ru_ixrss ) ) ;
  78. # else
  79.             p->pr_vself.vm_majflt,
  80.             p->pr_vself.vm_minflt,
  81.             p->pr_vself.vm_nswap,
  82.             p->pr_vself.vm_inblk + p->pr_vself.vm_oublk,
  83.             KBYTES( (p->pr_vself.vm_idsrss
  84.                 + p->pr_vself.vm_ixrss) / Info.i_hz ) ) ;
  85. # endif
  86.         prcmd( p, 5, -63 ) ;
  87.         return ;
  88.     }
  89.     if ( !Flg.flg_v )                       
  90.     {       /* Not verbose so just list command arguments */
  91.         prcmd( p, 5, -19 ) ;
  92.         return ;
  93.     }
  94.     /* Arrive here if being verbose ; list cpu information */
  95.     switch ( p->pr_p.p_stat )               
  96.     {                                       
  97.         case SSLEEP :
  98.         case SWAIT :
  99.         case SIDL :
  100.             /* Determine why a process should be in a wait state */
  101.             chp = waitingfor( p ) ;
  102.             break ;
  103.         case SRUN :
  104.             chp = "run" ;
  105.             break ;
  106.         case SZOMB :
  107.             chp = "exit" ;
  108.             break ;
  109.         case SSTOP :
  110.             chp = "stop" ;
  111.             break ;
  112.     }
  113.     /* If the process is loaded, list the status information in capitals */
  114.     printf( "%-6.6s ", p->pr_p.p_flag & SLOAD ?
  115.         (capitals( chp, chbuf ), chbuf) : chp ) ;
  116.     /* List process flags */
  117.     printf( "%c%c%c", p->pr_p.p_flag & SSYS ? 'U' :
  118.         p->pr_p.p_flag & STRC ? 'T' : ' ',
  119.         p->pr_p.p_flag & SVFORK ? 'V' :
  120.         p->pr_p.p_flag & SPHYSIO ? 'I' : ' ',
  121.         p->pr_p.p_flag & SUANOM ? 'A' : ' ' ) ;
  122.     /* List process niceness */
  123.     if ( p->pr_p.p_nice != NZERO )          
  124.         printf( "%3d ", p->pr_p.p_nice - NZERO ) ;
  125.     else
  126.         printf( "    " ) ;
  127.     if ( p->pr_p.p_stat == SZOMB )
  128.     {
  129.         prcmd( p, 41, -69 ) ;
  130.         return ;
  131.     }                                       
  132.     /* List process and text virtual sizes */
  133.     printf( "%4d", KBYTES( p->pr_p.p_dsize + p->pr_p.p_ssize ) ) ;
  134.     if ( tp = p->pr_p.p_textp )
  135.         printf( "+%3d ", KBYTES( tp->x_size ) ) ;
  136.     else
  137.         printf( "     " ) ;
  138.     /* List process and text real sizes */
  139.     printf( "%4d", KBYTES( p->pr_p.p_rssize ) ) ;
  140.     if ( tp )
  141.         printf( "+%3d %2.0f ", KBYTES( tp->x_rssize ),
  142.             percentmem( p ) );
  143.     else
  144.         printf( "        " ) ;
  145.     /* List information obtained from the upage. This includes the process
  146.        times and command arguments. */
  147.     if ( !p->pr_upag )
  148.     {
  149.         prcmd( p, 20, -69 ) ;
  150.         return ;
  151.     }                                       
  152.     /* List process time information */
  153. # ifdef BSD42
  154.     time   = Flg.flg_q ? p->pr_rself.ru_utime.tv_sec :
  155.          p->pr_rself.ru_utime.tv_sec + p->pr_rself.ru_stime.tv_sec ;
  156.     utime  = Flg.flg_q ? p->pr_rself.ru_utime.tv_usec :
  157.          p->pr_rself.ru_utime.tv_usec + p->pr_rself.ru_stime.tv_usec ;
  158.     chtime = Flg.flg_q ? p->pr_rchild.ru_utime.tv_sec :
  159.          p->pr_rchild.ru_utime.tv_sec + p->pr_rchild.ru_stime.tv_sec ;
  160.     uchtime = Flg.flg_q ? p->pr_rchild.ru_utime.tv_usec :
  161.          p->pr_rchild.ru_utime.tv_usec + p->pr_rchild.ru_stime.tv_usec ;
  162.     prcpu( time, utime ) ;
  163.     if ( chtime != 0L )
  164.     {
  165.         printf( "+" ) ;
  166.         prcpu( chtime, uchtime ) ;
  167.     }
  168. # else
  169.     time   = Flg.flg_q ? p->pr_vself.vm_utime :
  170.          p->pr_vself.vm_utime + p->pr_vself.vm_stime ;
  171.     chtime = Flg.flg_q ? p->pr_vchild.vm_utime :
  172.          p->pr_vchild.vm_utime + p->pr_vchild.vm_stime ;
  173.     prcpu( time ) ;
  174.     if ( chtime != 0L )
  175.     {
  176.         printf( "+" ) ;
  177.         prcpu( chtime ) ;
  178.     }
  179. # endif
  180.     else
  181.         printf( "      " ) ;
  182. # ifdef BSD42
  183.     if ( time || utime )
  184. # else
  185.     if ( time )
  186. # endif
  187. # ifdef SUN
  188.         printf( " %2.0f ", (double)p->pr_p.p_pctcpu ) ;
  189. # else
  190.         printf( " %2.0f ", p->pr_p.p_pctcpu * 100.0 ) ;
  191. # endif
  192.     else
  193.         printf( "    " ) ;
  194.     /* Finally, list the process command arguments. */
  195.     prcmd( p, 5, -69 ) ;                    
  196. }
  197.  
  198. /* CAPITALS - Converts letters in `chp' to upper-case in buffer `buf'. */
  199. capitals ( chp, buf )
  200.  
  201. register char                   *chp ;
  202. register char                   *buf ;
  203.  
  204. {
  205.     while ( *buf = *chp++ )
  206.     {
  207.         if ( 'a' <= *buf && *buf <= 'z' )
  208.             *buf -= 'a' - 'A' ;
  209.         buf++ ;
  210.     }
  211. }
  212.