Home | History | Annotate | Line # | Download | only in lastcomm
lastcomm.c revision 1.4
      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.3  mycroft /*static char sccsid[] = "from: @(#)lastcomm.c	5.11 (Berkeley) 6/1/90";*/
     42  1.4      jtc static char rcsid[] = "$Id: lastcomm.c,v 1.4 1993/12/04 01:57:10 jtc Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /*
     46  1.1      cgd  * last command
     47  1.1      cgd  */
     48  1.1      cgd #include <sys/param.h>
     49  1.1      cgd #include <sys/acct.h>
     50  1.1      cgd #include <sys/file.h>
     51  1.1      cgd #include <sys/stat.h>
     52  1.1      cgd #include <utmp.h>
     53  1.1      cgd #include <struct.h>
     54  1.1      cgd #include <ctype.h>
     55  1.1      cgd #include <stdio.h>
     56  1.4      jtc #include <stdlib.h>
     57  1.1      cgd #include "pathnames.h"
     58  1.1      cgd 
     59  1.1      cgd struct	acct buf[DEV_BSIZE / sizeof (struct acct)];
     60  1.1      cgd 
     61  1.1      cgd time_t	expand();
     62  1.1      cgd char	*flagbits();
     63  1.1      cgd char	*getdev();
     64  1.1      cgd 
     65  1.1      cgd main(argc, argv)
     66  1.1      cgd 	int argc;
     67  1.1      cgd 	char *argv[];
     68  1.1      cgd {
     69  1.1      cgd 	extern int optind;
     70  1.1      cgd 	extern char *optarg;
     71  1.1      cgd 	register struct acct *acp;
     72  1.1      cgd 	register int bn, cc;
     73  1.1      cgd 	struct stat sb;
     74  1.1      cgd 	int ch, fd;
     75  1.1      cgd 	char *acctfile, *ctime(), *strcpy(), *user_from_uid();
     76  1.1      cgd 	long lseek();
     77  1.1      cgd 
     78  1.1      cgd 	acctfile = _PATH_ACCT;
     79  1.1      cgd 	while ((ch = getopt(argc, argv, "f:")) != EOF)
     80  1.1      cgd 		switch((char)ch) {
     81  1.1      cgd 		case 'f':
     82  1.1      cgd 			acctfile = optarg;
     83  1.1      cgd 			break;
     84  1.1      cgd 		case '?':
     85  1.1      cgd 		default:
     86  1.1      cgd 			fputs("lastcomm [ -f file ]\n", stderr);
     87  1.1      cgd 			exit(1);
     88  1.1      cgd 		}
     89  1.1      cgd 	argv += optind;
     90  1.1      cgd 
     91  1.1      cgd 	fd = open(acctfile, O_RDONLY);
     92  1.1      cgd 	if (fd < 0) {
     93  1.1      cgd 		perror(acctfile);
     94  1.1      cgd 		exit(1);
     95  1.1      cgd 	}
     96  1.1      cgd 	(void)fstat(fd, &sb);
     97  1.1      cgd 	setpassent(1);
     98  1.1      cgd 	for (bn = btodb(sb.st_size); bn >= 0; bn--) {
     99  1.1      cgd 		(void)lseek(fd, (off_t)dbtob(bn), L_SET);
    100  1.1      cgd 		cc = read(fd, buf, DEV_BSIZE);
    101  1.1      cgd 		if (cc < 0) {
    102  1.1      cgd 			perror("read");
    103  1.1      cgd 			break;
    104  1.1      cgd 		}
    105  1.1      cgd 		acp = buf + (cc / sizeof (buf[0])) - 1;
    106  1.1      cgd 		for (; acp >= buf; acp--) {
    107  1.1      cgd 			register char *cp;
    108  1.1      cgd 			time_t x;
    109  1.1      cgd 
    110  1.1      cgd 			if (acp->ac_comm[0] == '\0')
    111  1.1      cgd 				(void)strcpy(acp->ac_comm, "?");
    112  1.1      cgd 			for (cp = &acp->ac_comm[0];
    113  1.1      cgd 			     cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
    114  1.1      cgd 			     cp++)
    115  1.1      cgd 				if (!isascii(*cp) || iscntrl(*cp))
    116  1.1      cgd 					*cp = '?';
    117  1.1      cgd 			if (*argv && !ok(argv, acp))
    118  1.1      cgd 				continue;
    119  1.1      cgd 			x = expand(acp->ac_utime) + expand(acp->ac_stime);
    120  1.1      cgd 			printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
    121  1.1      cgd 				fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
    122  1.1      cgd 				acp->ac_comm, flagbits(acp->ac_flag),
    123  1.1      cgd 				UT_NAMESIZE, user_from_uid(acp->ac_uid, 0),
    124  1.1      cgd 				UT_LINESIZE, getdev(acp->ac_tty),
    125  1.1      cgd 				x / (double)AHZ, ctime(&acp->ac_btime));
    126  1.1      cgd 		}
    127  1.1      cgd 	}
    128  1.1      cgd }
    129  1.1      cgd 
    130  1.1      cgd time_t
    131  1.1      cgd expand (t)
    132  1.1      cgd 	unsigned t;
    133  1.1      cgd {
    134  1.1      cgd 	register time_t nt;
    135  1.1      cgd 
    136  1.1      cgd 	nt = t & 017777;
    137  1.1      cgd 	t >>= 13;
    138  1.1      cgd 	while (t) {
    139  1.1      cgd 		t--;
    140  1.1      cgd 		nt <<= 3;
    141  1.1      cgd 	}
    142  1.1      cgd 	return (nt);
    143  1.1      cgd }
    144  1.1      cgd 
    145  1.1      cgd char *
    146  1.1      cgd flagbits(f)
    147  1.1      cgd 	register int f;
    148  1.1      cgd {
    149  1.1      cgd 	static char flags[20];
    150  1.1      cgd 	char *p, *strcpy();
    151  1.1      cgd 
    152  1.1      cgd #define	BIT(flag, ch)	if (f & flag) *p++ = ch;
    153  1.1      cgd 	p = strcpy(flags, "-    ");
    154  1.1      cgd 	BIT(ASU, 'S');
    155  1.1      cgd 	BIT(AFORK, 'F');
    156  1.1      cgd 	BIT(ACOMPAT, 'C');
    157  1.1      cgd 	BIT(ACORE, 'D');
    158  1.1      cgd 	BIT(AXSIG, 'X');
    159  1.1      cgd 	return (flags);
    160  1.1      cgd }
    161  1.1      cgd 
    162  1.1      cgd ok(argv, acp)
    163  1.1      cgd 	register char *argv[];
    164  1.1      cgd 	register struct acct *acp;
    165  1.1      cgd {
    166  1.1      cgd 	register char *cp;
    167  1.1      cgd 	char *user_from_uid();
    168  1.1      cgd 
    169  1.1      cgd 	do {
    170  1.1      cgd 		cp = user_from_uid(acp->ac_uid, 0);
    171  1.1      cgd 		if (!strcmp(cp, *argv))
    172  1.1      cgd 			return(1);
    173  1.1      cgd 		if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
    174  1.1      cgd 			return(1);
    175  1.1      cgd 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
    176  1.1      cgd 			return(1);
    177  1.1      cgd 	} while (*++argv);
    178  1.1      cgd 	return(0);
    179  1.1      cgd }
    180  1.1      cgd 
    181  1.4      jtc #include <dirent.h>
    182  1.1      cgd 
    183  1.1      cgd #define N_DEVS		43		/* hash value for device names */
    184  1.1      cgd #define NDEVS		500		/* max number of file names in /dev */
    185  1.1      cgd 
    186  1.1      cgd struct	devhash {
    187  1.1      cgd 	dev_t	dev_dev;
    188  1.1      cgd 	char	dev_name [UT_LINESIZE + 1];
    189  1.1      cgd 	struct	devhash * dev_nxt;
    190  1.1      cgd };
    191  1.1      cgd struct	devhash *dev_hash[N_DEVS];
    192  1.1      cgd struct	devhash *dev_chain;
    193  1.1      cgd #define HASH(d)	(((int) d) % N_DEVS)
    194  1.1      cgd 
    195  1.1      cgd setupdevs()
    196  1.1      cgd {
    197  1.1      cgd 	register DIR * fd;
    198  1.1      cgd 	register struct devhash * hashtab;
    199  1.1      cgd 	register ndevs = NDEVS;
    200  1.4      jtc 	struct dirent * dp;
    201  1.1      cgd 
    202  1.1      cgd 	/*NOSTRICT*/
    203  1.1      cgd 	hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
    204  1.1      cgd 	if (hashtab == (struct devhash *)0) {
    205  1.1      cgd 		fputs("No mem for dev table\n", stderr);
    206  1.1      cgd 		return;
    207  1.1      cgd 	}
    208  1.1      cgd 	if ((fd = opendir(_PATH_DEV)) == NULL) {
    209  1.1      cgd 		perror(_PATH_DEV);
    210  1.1      cgd 		return;
    211  1.1      cgd 	}
    212  1.1      cgd 	while (dp = readdir(fd)) {
    213  1.1      cgd 		if (dp->d_ino == 0)
    214  1.1      cgd 			continue;
    215  1.2      cgd 		if (dp->d_name[0] != 't' && /* XXX (cgd) followin is a hack! */
    216  1.2      cgd 		    (strcmp(dp->d_name, "console") && strcmp(dp->d_name, "vga")))
    217  1.1      cgd 			continue;
    218  1.1      cgd 		(void)strncpy(hashtab->dev_name, dp->d_name, UT_LINESIZE);
    219  1.1      cgd 		hashtab->dev_name[UT_LINESIZE] = 0;
    220  1.1      cgd 		hashtab->dev_nxt = dev_chain;
    221  1.1      cgd 		dev_chain = hashtab;
    222  1.1      cgd 		hashtab++;
    223  1.1      cgd 		if (--ndevs <= 0)
    224  1.1      cgd 			break;
    225  1.1      cgd 	}
    226  1.1      cgd 	closedir(fd);
    227  1.1      cgd }
    228  1.1      cgd 
    229  1.1      cgd char *
    230  1.1      cgd getdev(dev)
    231  1.1      cgd 	dev_t dev;
    232  1.1      cgd {
    233  1.1      cgd 	register struct devhash *hp, *nhp;
    234  1.1      cgd 	struct stat statb;
    235  1.1      cgd 	char name[fldsiz(devhash, dev_name) + 6];
    236  1.1      cgd 	static dev_t lastdev = (dev_t) -1;
    237  1.1      cgd 	static char *lastname;
    238  1.1      cgd 	static int init = 0;
    239  1.1      cgd 	char *strcpy(), *strcat();
    240  1.1      cgd 
    241  1.1      cgd 	if (dev == NODEV)
    242  1.1      cgd 		return ("__");
    243  1.1      cgd 	if (dev == lastdev)
    244  1.1      cgd 		return (lastname);
    245  1.1      cgd 	if (!init) {
    246  1.1      cgd 		setupdevs();
    247  1.1      cgd 		init++;
    248  1.1      cgd 	}
    249  1.1      cgd 	for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
    250  1.1      cgd 		if (hp->dev_dev == dev) {
    251  1.1      cgd 			lastdev = dev;
    252  1.1      cgd 			return (lastname = hp->dev_name);
    253  1.1      cgd 		}
    254  1.1      cgd 	for (hp = dev_chain; hp; hp = nhp) {
    255  1.1      cgd 		nhp = hp->dev_nxt;
    256  1.1      cgd 		(void)strcpy(name, _PATH_DEV);
    257  1.1      cgd 		strcat(name, hp->dev_name);
    258  1.1      cgd 		if (stat(name, &statb) < 0)	/* name truncated usually */
    259  1.1      cgd 			continue;
    260  1.1      cgd 		if ((statb.st_mode & S_IFMT) != S_IFCHR)
    261  1.1      cgd 			continue;
    262  1.1      cgd 		hp->dev_dev = statb.st_rdev;
    263  1.1      cgd 		hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
    264  1.1      cgd 		dev_hash[HASH(hp->dev_dev)] = hp;
    265  1.1      cgd 		if (hp->dev_dev == dev) {
    266  1.1      cgd 			dev_chain = nhp;
    267  1.1      cgd 			lastdev = dev;
    268  1.1      cgd 			return (lastname = hp->dev_name);
    269  1.1      cgd 		}
    270  1.1      cgd 	}
    271  1.1      cgd 	dev_chain = (struct devhash *) 0;
    272  1.1      cgd 	return ("??");
    273  1.1      cgd }
    274