Home | History | Annotate | Line # | Download | only in lastcomm
lastcomm.c revision 1.4
      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.4 1993/12/04 01:57:10 jtc 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 <stdlib.h>
     57 #include "pathnames.h"
     58 
     59 struct	acct buf[DEV_BSIZE / sizeof (struct acct)];
     60 
     61 time_t	expand();
     62 char	*flagbits();
     63 char	*getdev();
     64 
     65 main(argc, argv)
     66 	int argc;
     67 	char *argv[];
     68 {
     69 	extern int optind;
     70 	extern char *optarg;
     71 	register struct acct *acp;
     72 	register int bn, cc;
     73 	struct stat sb;
     74 	int ch, fd;
     75 	char *acctfile, *ctime(), *strcpy(), *user_from_uid();
     76 	long lseek();
     77 
     78 	acctfile = _PATH_ACCT;
     79 	while ((ch = getopt(argc, argv, "f:")) != EOF)
     80 		switch((char)ch) {
     81 		case 'f':
     82 			acctfile = optarg;
     83 			break;
     84 		case '?':
     85 		default:
     86 			fputs("lastcomm [ -f file ]\n", stderr);
     87 			exit(1);
     88 		}
     89 	argv += optind;
     90 
     91 	fd = open(acctfile, O_RDONLY);
     92 	if (fd < 0) {
     93 		perror(acctfile);
     94 		exit(1);
     95 	}
     96 	(void)fstat(fd, &sb);
     97 	setpassent(1);
     98 	for (bn = btodb(sb.st_size); bn >= 0; bn--) {
     99 		(void)lseek(fd, (off_t)dbtob(bn), L_SET);
    100 		cc = read(fd, buf, DEV_BSIZE);
    101 		if (cc < 0) {
    102 			perror("read");
    103 			break;
    104 		}
    105 		acp = buf + (cc / sizeof (buf[0])) - 1;
    106 		for (; acp >= buf; acp--) {
    107 			register char *cp;
    108 			time_t x;
    109 
    110 			if (acp->ac_comm[0] == '\0')
    111 				(void)strcpy(acp->ac_comm, "?");
    112 			for (cp = &acp->ac_comm[0];
    113 			     cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
    114 			     cp++)
    115 				if (!isascii(*cp) || iscntrl(*cp))
    116 					*cp = '?';
    117 			if (*argv && !ok(argv, acp))
    118 				continue;
    119 			x = expand(acp->ac_utime) + expand(acp->ac_stime);
    120 			printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
    121 				fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
    122 				acp->ac_comm, flagbits(acp->ac_flag),
    123 				UT_NAMESIZE, user_from_uid(acp->ac_uid, 0),
    124 				UT_LINESIZE, getdev(acp->ac_tty),
    125 				x / (double)AHZ, ctime(&acp->ac_btime));
    126 		}
    127 	}
    128 }
    129 
    130 time_t
    131 expand (t)
    132 	unsigned t;
    133 {
    134 	register time_t nt;
    135 
    136 	nt = t & 017777;
    137 	t >>= 13;
    138 	while (t) {
    139 		t--;
    140 		nt <<= 3;
    141 	}
    142 	return (nt);
    143 }
    144 
    145 char *
    146 flagbits(f)
    147 	register int f;
    148 {
    149 	static char flags[20];
    150 	char *p, *strcpy();
    151 
    152 #define	BIT(flag, ch)	if (f & flag) *p++ = ch;
    153 	p = strcpy(flags, "-    ");
    154 	BIT(ASU, 'S');
    155 	BIT(AFORK, 'F');
    156 	BIT(ACOMPAT, 'C');
    157 	BIT(ACORE, 'D');
    158 	BIT(AXSIG, 'X');
    159 	return (flags);
    160 }
    161 
    162 ok(argv, acp)
    163 	register char *argv[];
    164 	register struct acct *acp;
    165 {
    166 	register char *cp;
    167 	char *user_from_uid();
    168 
    169 	do {
    170 		cp = user_from_uid(acp->ac_uid, 0);
    171 		if (!strcmp(cp, *argv))
    172 			return(1);
    173 		if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
    174 			return(1);
    175 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
    176 			return(1);
    177 	} while (*++argv);
    178 	return(0);
    179 }
    180 
    181 #include <dirent.h>
    182 
    183 #define N_DEVS		43		/* hash value for device names */
    184 #define NDEVS		500		/* max number of file names in /dev */
    185 
    186 struct	devhash {
    187 	dev_t	dev_dev;
    188 	char	dev_name [UT_LINESIZE + 1];
    189 	struct	devhash * dev_nxt;
    190 };
    191 struct	devhash *dev_hash[N_DEVS];
    192 struct	devhash *dev_chain;
    193 #define HASH(d)	(((int) d) % N_DEVS)
    194 
    195 setupdevs()
    196 {
    197 	register DIR * fd;
    198 	register struct devhash * hashtab;
    199 	register ndevs = NDEVS;
    200 	struct dirent * dp;
    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