1: #ifndef lint
   2: static char *sccsid = "@(#)lastcomm.c	4.3 (Berkeley) 12/14/80";
   3: #endif
   4: #
   5: 
   6: /*
   7:  * last command
   8:  */
   9: 
  10: # include <stdio.h>
  11: # include <sys/types.h>
  12: # include <sys/acct.h>
  13: # include <signal.h>
  14: # include <pwd.h>
  15: # include <stat.h>
  16: 
  17: # define N_USER     1000
  18: 
  19: struct acct acct_buff [BUFSIZ / sizeof (struct acct)];
  20: 
  21: char    yes   = 1,
  22:     no    = 0,
  23: 
  24:     user_list [N_USER][9];
  25: 
  26: time_t  expand ();
  27: 
  28: struct passwd
  29:     *passwd,
  30:     *getpwent ();
  31: 
  32: struct stat stat_buff;
  33: 
  34: main (argc, argv)
  35: char **argv;
  36: {
  37:     char    acct_desc,
  38:         *p;
  39: 
  40:     long    i,
  41:         j,
  42:         i_block,
  43:         n_blocks,
  44:         n_byte,
  45:         n_entry;
  46: 
  47:     float   x;
  48: 
  49: /*
  50:  * set up user names
  51:  */
  52:     while (passwd = getpwent ())
  53:     {
  54:         if (passwd->pw_uid > N_USER)
  55:         {
  56:             printf ("lastcomm:Too large of a user id number\n");
  57:             return;
  58:         }
  59:         if (user_list[passwd->pw_uid][0]==0)
  60:             move (passwd->pw_name, user_list [passwd->pw_uid]);
  61:     }
  62: 
  63:     acct_desc = open ("/usr/adm/acct", 0);
  64:     if (acct_desc < 0)
  65:     {
  66:         perror ("/usr/adm/acct");
  67:         return;
  68:     }
  69:     fstat (acct_desc, &stat_buff);
  70:     n_blocks = (stat_buff.st_size + BUFSIZ - 1) / BUFSIZ;
  71: 
  72:     /*
  73: 	 * read one block's worth
  74: 	 */
  75:     for (i_block = n_blocks - 1; i_block >= 0; i_block--)
  76:     {
  77:         lseek (acct_desc, i_block * BUFSIZ, 0);
  78:         n_byte = read (acct_desc, (char *)acct_buff, BUFSIZ);
  79:         n_entry = n_byte / sizeof acct_buff [0];
  80:         for (i = n_entry - 1; i >= 0; i--)
  81:         {
  82:             if (acct_buff [i].ac_uid >= N_USER)
  83:             {
  84:                 printf (
  85:                 "lastcomm:Too large of a user id number\n");
  86:                 return;
  87:             }
  88:             if (!*user_list [acct_buff [i].ac_uid]) continue;
  89:             /*
  90: 			 * get the times
  91: 			 */
  92:             x = expand (acct_buff [i].ac_utime)
  93:                 +
  94:                 expand (acct_buff [i].ac_stime);
  95:             /*
  96: 			 * null terminate the command name
  97: 			 */
  98:             acct_buff [i].ac_comm [10] = 0;
  99:             /*
 100: 			 * replace missing command names with question marks
 101: 			 */
 102:             if (!*acct_buff [i].ac_comm)
 103:             {
 104:                 move ("?", acct_buff [i].ac_comm);
 105:             }
 106:             /*
 107: 			 * replace control characters with question marks
 108: 			 */
 109:             for (p = acct_buff [i].ac_comm; *p; p++)
 110:             {
 111:                 if (*p < '!' || '~' < *p) *p = '?';
 112:             }
 113:             for (j = 1; j < argc; j++)
 114:             {
 115:                 if
 116:                 (
 117:                     equal (acct_buff [i].ac_comm, argv [j])
 118:                     ||
 119:                     equal
 120:                     (
 121:                     user_list [acct_buff [i].ac_uid],
 122:                         argv [j]
 123:                     )
 124:                 )
 125:                 {
 126:                     break;
 127:                 }
 128:             }
 129:             if (argc == 1 || j != argc)
 130:             {
 131:                 printf
 132:                 (
 133:                     "%-10s %-8s %6.2f     %.16s\n",
 134:                     acct_buff [i].ac_comm,
 135:                     user_list [acct_buff [i].ac_uid],
 136:                     x / 60.0,
 137:                     ctime (&acct_buff [i].ac_btime)
 138:                 );
 139:             }
 140:         }
 141:     }
 142: }
 143: 
 144: time_t
 145: expand (t)
 146: comp_t t;
 147: {
 148:     register time_t nt;
 149: 
 150:     nt = t & 017777;
 151:     t >>= 13;
 152:     while (t)
 153:     {
 154:         t--;
 155:         nt <<= 3;
 156:     }
 157:     return (nt);
 158: }
 159: 
 160: move (a, b)
 161: register char *a, *b;
 162: {
 163:     while (*b++ = *a++);
 164: }
 165: 
 166: equal (a, b)
 167: register char *a, *b;
 168: {
 169:     for (;; a++, b++)
 170:     {
 171:         if (*a != *b) return no;
 172:         if (!*a)      return yes;
 173:     }
 174: }

Defined functions

equal defined in line 166; used 2 times
expand defined in line 144; used 3 times
main defined in line 34; never used
move defined in line 160; used 2 times

Defined variables

acct_buff defined in line 19; used 15 times
passwd defined in line 29; used 5 times
sccsid defined in line 2; never used
stat_buff defined in line 32; used 2 times
yes defined in line 21; used 1 times

Defined macros

N_USER defined in line 17; used 3 times
Last modified: 1982-10-26
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 788
Valid CSS Valid XHTML 1.0 Strict