From 7f055d321cae3c718eb6e239f67be823c8f93da5 Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Tue, 5 Apr 1983 04:34:38 -0800 Subject: [PATCH] rewrite SCCS-vsn: usr.bin/lastcomm/lastcomm.c 4.5 --- usr/src/usr.bin/lastcomm/lastcomm.c | 270 +++++++++++----------------- 1 file changed, 101 insertions(+), 169 deletions(-) diff --git a/usr/src/usr.bin/lastcomm/lastcomm.c b/usr/src/usr.bin/lastcomm/lastcomm.c index bb25565568..776d20f735 100644 --- a/usr/src/usr.bin/lastcomm/lastcomm.c +++ b/usr/src/usr.bin/lastcomm/lastcomm.c @@ -1,206 +1,129 @@ -static char *sccsid = "@(#)lastcomm.c 4.4 (Berkeley) 82/07/17"; +#ifndef lint +static char *sccsid = "@(#)lastcomm.c 4.5 (Berkeley) 83/04/04"; +#endif /* * last command */ - -# include -# include -# include -# include -# include -# include -# include -# include -# include - -# define N_USER 4000 /* highest alloc user # */ -# define N_DEVS 43 /* hash value for device names */ -# define NDEVS 500 /* max number of file names in /dev */ - -struct acct acct_buff [BUFSIZ / sizeof (struct acct)]; - -char user_list [N_USER][fldsiz(utmp, ut_name) + 1]; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define N_USER 4000 /* highest alloc user # */ +#define N_DEVS 43 /* hash value for device names */ +#define NDEVS 500 /* max number of file names in /dev */ + +struct acct acct_buff[BUFSIZ / sizeof (struct acct)]; +char user_list[N_USER][fldsiz(utmp, ut_name) + 1]; struct devhash { dev_t dev_dev; char dev_name [fldsiz(utmp, ut_line) + 1]; struct devhash * dev_nxt; -} - * dev_hash [ N_DEVS ], - * dev_chain ; -# define HASH(d) (((int) d) % N_DEVS) +}; +struct devhash *dev_hash[N_DEVS]; +struct devhash *dev_chain ; +#define HASH(d) (((int) d) % N_DEVS) time_t expand (); -char * flagbits(); -char * tername(); - -struct passwd - *passwd, - *getpwent (); +char *flagbits(); +char *ttyname(); +struct passwd *passwd, *getpwent (); struct stat stat_buff; -# define equal(a, b) (strcmp(a, b) == 0) - -main (argc, argv) -char **argv; +main(argc, argv) + char *argv[]; { - char acct_desc, - *p; - - long i, - j, - i_block, - n_blocks, - n_byte, - n_entry; - - float x; + char acct_desc, *p; + long i, j, i_block, n_blocks, n_byte, n_entry, x; + register struct acct *acp; -/* - * set up user names - */ - while (passwd = getpwent ()) - { - if (user_list[passwd->pw_uid][0]==0) - move (passwd->pw_name, user_list [passwd->pw_uid]); - } - - /* - * find dev numbers corresponding to names in /dev - */ + /* + * Set up user names + */ + while (passwd = getpwent()) + if (user_list[passwd->pw_uid][0] == 0) + strcpy(user_list[passwd->pw_uid], passwd->pw_name); + /* + * Find dev numbers corresponding to names in /dev + */ setupdevs(); - - acct_desc = open ("/usr/adm/acct", 0); - if (acct_desc < 0) - { - perror ("/usr/adm/acct"); - return; + acct_desc = open("/usr/adm/acct", 0); + if (acct_desc < 0) { + perror("/usr/adm/acct"); + exit(1); } - fstat (acct_desc, &stat_buff); + fstat(acct_desc, &stat_buff); n_blocks = (stat_buff.st_size + BUFSIZ - 1) / BUFSIZ; /* - * read one block's worth + * Read one block's worth */ - for (i_block = n_blocks - 1; i_block >= 0; i_block--) - { - lseek (acct_desc, i_block * BUFSIZ, 0); - n_byte = read (acct_desc, acct_buff, BUFSIZ); - n_entry = n_byte / sizeof acct_buff [0]; - for (i = n_entry - 1; i >= 0; i--) - { - if (!*user_list [acct_buff [i].ac_uid]) + for (i_block = n_blocks - 1; i_block >= 0; i_block--) { + lseek(acct_desc, i_block * BUFSIZ, 0); + n_byte = read(acct_desc, acct_buff, BUFSIZ); + n_entry = n_byte / sizeof acct_buff[0]; + for (acp = acct_buff + n_entry - 1; acp >= acct_buff; acp--) { + if (*user_list[acp->ac_uid] == '\0') continue; - /* - * get the times - */ - x = expand (acct_buff [i].ac_utime) - + - expand (acct_buff [i].ac_stime); - /* - * null terminate the command name - */ - acct_buff [i].ac_comm [10] = 0; - /* - * replace missing command names with question marks - */ - if (!*acct_buff [i].ac_comm) - { - move ("?", acct_buff [i].ac_comm); - } - /* - * replace control characters with question marks - */ - for (p = acct_buff [i].ac_comm; *p; p++) - { - if (*p < '!' || '~' < *p) + x = expand(acp->ac_utime) + expand(acp->ac_stime); + acp->ac_comm[10] = '\0'; + if (*acp->ac_comm == '\0') + strcpy(acp->ac_comm, "?"); + for (p = acp->ac_comm; *p; p++) + if (iscntrl(*p)) *p = '?'; - } - for (j = 1; j < argc; j++) - { - if - ( - equal (acct_buff [i].ac_comm, argv [j]) - || - equal - ( - user_list[acct_buff[i].ac_uid], - argv [j] - ) - || - equal - ( - tername(acct_buff[i].ac_tty), - argv[j] - ) - ) - { - break; - } - } - if (argc == 1 || j != argc) - { - printf - ( - "%-*s %s %-*s %-*s %6.2f %.16s\n" - , fldsiz(acct, ac_comm) - , acct_buff [i].ac_comm - , flagbits(acct_buff [i].ac_flag) - , fldsiz(utmp, ut_name) - , user_list [acct_buff [i].ac_uid] - , fldsiz(utmp, ut_line) - , tername(acct_buff [i].ac_tty) - , x / 60.0 - , ctime (&acct_buff [i].ac_btime) - ); - } + if (!ok(argc, argv, acp) && argc != 1) + continue; + printf("%-*s %s %-*s %-*s %4d sec%s %.16s\n", + fldsiz(acct, ac_comm), acp->ac_comm, + flagbits(acp->ac_flag), + fldsiz(utmp, ut_name), user_list[acp->ac_uid], + fldsiz(utmp, ut_line), ttyname(acp->ac_tty), + x, x > 1 || x == 0 ? "s" : " ", + ctime(&acp->ac_btime)); } } } time_t expand (t) -unsigned t; + unsigned t; { register time_t nt; nt = t & 017777; t >>= 13; - while (t) - { + while (t) { t--; nt <<= 3; } return (nt); } -move (a, b) -char *a, *b; -{ - while (*b++ = *a++) - ; -} - char * flagbits(f) -register int f; + register int f; { register int i = 0; static char flags[20]; -# define BIT(flag, ch) flags[i++] = ( f & flag ) ? ch : ' ' - - BIT( ASU, 'S'); - BIT( AFORK, 'F'); - BIT( ACOMPAT, 'C'); - BIT( ACORE, 'D'); - BIT( AXSIG, 'X'); - +#define BIT(flag, ch) flags[i++] = (f & flag) ? ch : ' ' + BIT(ASU, 'S'); + BIT(AFORK, 'F'); + BIT(ACOMPAT, 'C'); + BIT(ACORE, 'D'); + BIT(AXSIG, 'X'); flags[i] = '\0'; - - return(flags); + return (flags); } setupdevs() @@ -214,13 +137,11 @@ setupdevs() perror("/dev"); return; } - if ((hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash))) == (struct devhash *) 0) { fprintf(stderr, "No mem for dev table\n"); return; } - while (dp = readdir(fd)) { if (dp->d_ino == 0) continue; @@ -240,27 +161,24 @@ setupdevs() } char * -tername(dev) -dev_t dev; +ttyname(dev) + dev_t dev; { register struct devhash *hp, *nhp; struct stat statb; - char name [fldsiz(devhash, dev_name) + 6]; + char name[fldsiz(devhash, dev_name) + 6]; static dev_t lastdev = (dev_t) -1; static char *lastname; if (dev == NODEV) - return("__"); - + return ("__"); if (dev == lastdev) - return(lastname); - + return (lastname); for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt) if (hp->dev_dev == dev) { lastdev = dev; - return(lastname = hp->dev_name); + return (lastname = hp->dev_name); } - for (hp = dev_chain; hp; hp = nhp) { nhp = hp->dev_nxt; strcpy(name, "/dev/"); @@ -275,10 +193,24 @@ dev_t dev; if (hp->dev_dev == dev) { dev_chain = nhp; lastdev = dev; - return(lastname = hp->dev_name); + return (lastname = hp->dev_name); } } - dev_chain = (struct devhash *) 0; - return("??"); + return ("??"); +} + +ok(argc, argv, acp) + register int argc; + register char *argv[]; + register struct acct *acp; +{ + register int j; + + for (j = 1; j < argc; j++) + if (strcmp(user_list[acp->ac_uid], argv[j]) && + strcmp(ttyname(acp->ac_tty), argv[j]) && + strcmp(acp->ac_comm, argv[j])) + break; + return (j == argc); } -- 2.20.1