Home | History | Annotate | Line # | Download | only in lastcomm
lastcomm.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd char copyright[] =
     36  1.1  cgd "@(#) Copyright (c) 1980 Regents of the University of California.\n\
     37  1.1  cgd  All rights reserved.\n";
     38  1.1  cgd #endif /* not lint */
     39  1.1  cgd 
     40  1.1  cgd #ifndef lint
     41  1.1  cgd static char sccsid[] = "@(#)lastcomm.c	5.11 (Berkeley) 6/1/90";
     42  1.1  cgd #endif /* not lint */
     43  1.1  cgd 
     44  1.1  cgd /*
     45  1.1  cgd  * last command
     46  1.1  cgd  */
     47  1.1  cgd #include <sys/param.h>
     48  1.1  cgd #include <sys/acct.h>
     49  1.1  cgd #include <sys/file.h>
     50  1.1  cgd #include <sys/stat.h>
     51  1.1  cgd #include <utmp.h>
     52  1.1  cgd #include <struct.h>
     53  1.1  cgd #include <ctype.h>
     54  1.1  cgd #include <stdio.h>
     55  1.1  cgd #include "pathnames.h"
     56  1.1  cgd 
     57  1.1  cgd struct	acct buf[DEV_BSIZE / sizeof (struct acct)];
     58  1.1  cgd 
     59  1.1  cgd time_t	expand();
     60  1.1  cgd char	*flagbits();
     61  1.1  cgd char	*getdev();
     62  1.1  cgd 
     63  1.1  cgd main(argc, argv)
     64  1.1  cgd 	int argc;
     65  1.1  cgd 	char *argv[];
     66  1.1  cgd {
     67  1.1  cgd 	extern int optind;
     68  1.1  cgd 	extern char *optarg;
     69  1.1  cgd 	register struct acct *acp;
     70  1.1  cgd 	register int bn, cc;
     71  1.1  cgd 	struct stat sb;
     72  1.1  cgd 	int ch, fd;
     73  1.1  cgd 	char *acctfile, *ctime(), *strcpy(), *user_from_uid();
     74  1.1  cgd 	long lseek();
     75  1.1  cgd 
     76  1.1  cgd 	acctfile = _PATH_ACCT;
     77  1.1  cgd 	while ((ch = getopt(argc, argv, "f:")) != EOF)
     78  1.1  cgd 		switch((char)ch) {
     79  1.1  cgd 		case 'f':
     80  1.1  cgd 			acctfile = optarg;
     81  1.1  cgd 			break;
     82  1.1  cgd 		case '?':
     83  1.1  cgd 		default:
     84  1.1  cgd 			fputs("lastcomm [ -f file ]\n", stderr);
     85  1.1  cgd 			exit(1);
     86  1.1  cgd 		}
     87  1.1  cgd 	argv += optind;
     88  1.1  cgd 
     89  1.1  cgd 	fd = open(acctfile, O_RDONLY);
     90  1.1  cgd 	if (fd < 0) {
     91  1.1  cgd 		perror(acctfile);
     92  1.1  cgd 		exit(1);
     93  1.1  cgd 	}
     94  1.1  cgd 	(void)fstat(fd, &sb);
     95  1.1  cgd 	setpassent(1);
     96  1.1  cgd 	for (bn = btodb(sb.st_size); bn >= 0; bn--) {
     97  1.1  cgd 		(void)lseek(fd, (off_t)dbtob(bn), L_SET);
     98  1.1  cgd 		cc = read(fd, buf, DEV_BSIZE);
     99  1.1  cgd 		if (cc < 0) {
    100  1.1  cgd 			perror("read");
    101  1.1  cgd 			break;
    102  1.1  cgd 		}
    103  1.1  cgd 		acp = buf + (cc / sizeof (buf[0])) - 1;
    104  1.1  cgd 		for (; acp >= buf; acp--) {
    105  1.1  cgd 			register char *cp;
    106  1.1  cgd 			time_t x;
    107  1.1  cgd 
    108  1.1  cgd 			if (acp->ac_comm[0] == '\0')
    109  1.1  cgd 				(void)strcpy(acp->ac_comm, "?");
    110  1.1  cgd 			for (cp = &acp->ac_comm[0];
    111  1.1  cgd 			     cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
    112  1.1  cgd 			     cp++)
    113  1.1  cgd 				if (!isascii(*cp) || iscntrl(*cp))
    114  1.1  cgd 					*cp = '?';
    115  1.1  cgd 			if (*argv && !ok(argv, acp))
    116  1.1  cgd 				continue;
    117  1.1  cgd 			x = expand(acp->ac_utime) + expand(acp->ac_stime);
    118  1.1  cgd 			printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
    119  1.1  cgd 				fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
    120  1.1  cgd 				acp->ac_comm, flagbits(acp->ac_flag),
    121  1.1  cgd 				UT_NAMESIZE, user_from_uid(acp->ac_uid, 0),
    122  1.1  cgd 				UT_LINESIZE, getdev(acp->ac_tty),
    123  1.1  cgd 				x / (double)AHZ, ctime(&acp->ac_btime));
    124  1.1  cgd 		}
    125  1.1  cgd 	}
    126  1.1  cgd }
    127  1.1  cgd 
    128  1.1  cgd time_t
    129  1.1  cgd expand (t)
    130  1.1  cgd 	unsigned t;
    131  1.1  cgd {
    132  1.1  cgd 	register time_t nt;
    133  1.1  cgd 
    134  1.1  cgd 	nt = t & 017777;
    135  1.1  cgd 	t >>= 13;
    136  1.1  cgd 	while (t) {
    137  1.1  cgd 		t--;
    138  1.1  cgd 		nt <<= 3;
    139  1.1  cgd 	}
    140  1.1  cgd 	return (nt);
    141  1.1  cgd }
    142  1.1  cgd 
    143  1.1  cgd char *
    144  1.1  cgd flagbits(f)
    145  1.1  cgd 	register int f;
    146  1.1  cgd {
    147  1.1  cgd 	static char flags[20];
    148  1.1  cgd 	char *p, *strcpy();
    149  1.1  cgd 
    150  1.1  cgd #define	BIT(flag, ch)	if (f & flag) *p++ = ch;
    151  1.1  cgd 	p = strcpy(flags, "-    ");
    152  1.1  cgd 	BIT(ASU, 'S');
    153  1.1  cgd 	BIT(AFORK, 'F');
    154  1.1  cgd 	BIT(ACOMPAT, 'C');
    155  1.1  cgd 	BIT(ACORE, 'D');
    156  1.1  cgd 	BIT(AXSIG, 'X');
    157  1.1  cgd 	return (flags);
    158  1.1  cgd }
    159  1.1  cgd 
    160  1.1  cgd ok(argv, acp)
    161  1.1  cgd 	register char *argv[];
    162  1.1  cgd 	register struct acct *acp;
    163  1.1  cgd {
    164  1.1  cgd 	register char *cp;
    165  1.1  cgd 	char *user_from_uid();
    166  1.1  cgd 
    167  1.1  cgd 	do {
    168  1.1  cgd 		cp = user_from_uid(acp->ac_uid, 0);
    169  1.1  cgd 		if (!strcmp(cp, *argv))
    170  1.1  cgd 			return(1);
    171  1.1  cgd 		if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
    172  1.1  cgd 			return(1);
    173  1.1  cgd 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
    174  1.1  cgd 			return(1);
    175  1.1  cgd 	} while (*++argv);
    176  1.1  cgd 	return(0);
    177  1.1  cgd }
    178  1.1  cgd 
    179  1.1  cgd #include <sys/dir.h>
    180  1.1  cgd 
    181  1.1  cgd #define N_DEVS		43		/* hash value for device names */
    182  1.1  cgd #define NDEVS		500		/* max number of file names in /dev */
    183  1.1  cgd 
    184  1.1  cgd struct	devhash {
    185  1.1  cgd 	dev_t	dev_dev;
    186  1.1  cgd 	char	dev_name [UT_LINESIZE + 1];
    187  1.1  cgd 	struct	devhash * dev_nxt;
    188  1.1  cgd };
    189  1.1  cgd struct	devhash *dev_hash[N_DEVS];
    190  1.1  cgd struct	devhash *dev_chain;
    191  1.1  cgd #define HASH(d)	(((int) d) % N_DEVS)
    192  1.1  cgd 
    193  1.1  cgd setupdevs()
    194  1.1  cgd {
    195  1.1  cgd 	register DIR * fd;
    196  1.1  cgd 	register struct devhash * hashtab;
    197  1.1  cgd 	register ndevs = NDEVS;
    198  1.1  cgd 	struct direct * dp;
    199  1.1  cgd 	char *malloc();
    200  1.1  cgd 
    201  1.1  cgd 	/*NOSTRICT*/
    202  1.1  cgd 	hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
    203  1.1  cgd 	if (hashtab == (struct devhash *)0) {
    204  1.1  cgd 		fputs("No mem for dev table\n", stderr);
    205  1.1  cgd 		return;
    206  1.1  cgd 	}
    207  1.1  cgd 	if ((fd = opendir(_PATH_DEV)) == NULL) {
    208  1.1  cgd 		perror(_PATH_DEV);
    209  1.1  cgd 		return;
    210  1.1  cgd 	}
    211  1.1  cgd 	while (dp = readdir(fd)) {
    212  1.1  cgd 		if (dp->d_ino == 0)
    213  1.1  cgd 			continue;
    214  1.1  cgd 		if (dp->d_name[0] != 't' && strcmp(dp->d_name, "console"))
    215  1.1  cgd 			continue;
    216  1.1  cgd 		(void)strncpy(hashtab->dev_name, dp->d_name, UT_LINESIZE);
    217  1.1  cgd 		hashtab->dev_name[UT_LINESIZE] = 0;
    218  1.1  cgd 		hashtab->dev_nxt = dev_chain;
    219  1.1  cgd 		dev_chain = hashtab;
    220  1.1  cgd 		hashtab++;
    221  1.1  cgd 		if (--ndevs <= 0)
    222  1.1  cgd 			break;
    223  1.1  cgd 	}
    224  1.1  cgd 	closedir(fd);
    225  1.1  cgd }
    226  1.1  cgd 
    227  1.1  cgd char *
    228  1.1  cgd getdev(dev)
    229  1.1  cgd 	dev_t dev;
    230  1.1  cgd {
    231  1.1  cgd 	register struct devhash *hp, *nhp;
    232  1.1  cgd 	struct stat statb;
    233  1.1  cgd 	char name[fldsiz(devhash, dev_name) + 6];
    234  1.1  cgd 	static dev_t lastdev = (dev_t) -1;
    235  1.1  cgd 	static char *lastname;
    236  1.1  cgd 	static int init = 0;
    237  1.1  cgd 	char *strcpy(), *strcat();
    238  1.1  cgd 
    239  1.1  cgd 	if (dev == NODEV)
    240  1.1  cgd 		return ("__");
    241  1.1  cgd 	if (dev == lastdev)
    242  1.1  cgd 		return (lastname);
    243  1.1  cgd 	if (!init) {
    244  1.1  cgd 		setupdevs();
    245  1.1  cgd 		init++;
    246  1.1  cgd 	}
    247  1.1  cgd 	for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
    248  1.1  cgd 		if (hp->dev_dev == dev) {
    249  1.1  cgd 			lastdev = dev;
    250  1.1  cgd 			return (lastname = hp->dev_name);
    251  1.1  cgd 		}
    252  1.1  cgd 	for (hp = dev_chain; hp; hp = nhp) {
    253  1.1  cgd 		nhp = hp->dev_nxt;
    254  1.1  cgd 		(void)strcpy(name, _PATH_DEV);
    255  1.1  cgd 		strcat(name, hp->dev_name);
    256  1.1  cgd 		if (stat(name, &statb) < 0)	/* name truncated usually */
    257  1.1  cgd 			continue;
    258  1.1  cgd 		if ((statb.st_mode & S_IFMT) != S_IFCHR)
    259  1.1  cgd 			continue;
    260  1.1  cgd 		hp->dev_dev = statb.st_rdev;
    261  1.1  cgd 		hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
    262  1.1  cgd 		dev_hash[HASH(hp->dev_dev)] = hp;
    263  1.1  cgd 		if (hp->dev_dev == dev) {
    264  1.1  cgd 			dev_chain = nhp;
    265  1.1  cgd 			lastdev = dev;
    266  1.1  cgd 			return (lastname = hp->dev_name);
    267  1.1  cgd 		}
    268  1.1  cgd 	}
    269  1.1  cgd 	dev_chain = (struct devhash *) 0;
    270  1.1  cgd 	return ("??");
    271  1.1  cgd }
    272