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