home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / sps2 / part01 / printproc.c < prev    next >
C/C++ Source or Header  |  1991-01-08  |  7KB  |  256 lines

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