Home | History | Annotate | Line # | Download | only in lastcomm
lastcomm.c revision 1.14
      1 /*	$NetBSD: lastcomm.c,v 1.14 1998/04/02 10:22:03 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)lastcomm.c	8.2 (Berkeley) 4/29/95";
     45 #endif
     46 __RCSID("$NetBSD: lastcomm.c,v 1.14 1998/04/02 10:22:03 kleink Exp $");
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/stat.h>
     51 #include <sys/acct.h>
     52 
     53 #include <ctype.h>
     54 #include <err.h>
     55 #include <fcntl.h>
     56 #include <math.h>
     57 #include <pwd.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <struct.h>
     62 #include <time.h>
     63 #include <tzfile.h>
     64 #include <unistd.h>
     65 #include <utmp.h>
     66 #include "pathnames.h"
     67 
     68 time_t	 expand __P((u_int));
     69 char	*flagbits __P((int));
     70 char	*getdev __P((dev_t));
     71 int	 main __P((int, char **));
     72 int	 requested __P((char *[], struct acct *));
     73 void	 usage __P((void));
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char *argv[];
     79 {
     80 	char *p;
     81 	struct acct ab;
     82 	struct stat sb;
     83 	FILE *fp;
     84 	off_t size;
     85 	time_t t;
     86 	double delta;
     87 	int ch;
     88 	char *acctfile;
     89 
     90 	acctfile = _PATH_ACCT;
     91 	while ((ch = getopt(argc, argv, "f:")) != -1)
     92 		switch((char)ch) {
     93 		case 'f':
     94 			acctfile = optarg;
     95 			break;
     96 		case '?':
     97 		default:
     98 			usage();
     99 		}
    100 	argc -= optind;
    101 	argv += optind;
    102 
    103 	/* Open the file. */
    104 	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
    105 		err(1, "%s", acctfile);
    106 
    107 	/*
    108 	 * Round off to integral number of accounting records, probably
    109 	 * not necessary, but it doesn't hurt.
    110 	 */
    111 	size = sb.st_size - sb.st_size % sizeof(struct acct);
    112 
    113 	/* Check if any records to display. */
    114 	if (size < sizeof(struct acct))
    115 		exit(0);
    116 
    117 	/*
    118 	 * Seek to before the last entry in the file; use lseek(2) in case
    119 	 * the file is bigger than a "long".
    120 	 */
    121 	size -= sizeof(struct acct);
    122 	if (lseek(fileno(fp), size, SEEK_SET) == -1)
    123 		err(1, "%s", acctfile);
    124 
    125 	for (;;) {
    126 		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
    127 			err(1, "%s", acctfile);
    128 
    129 		if (ab.ac_comm[0] == '\0') {
    130 			ab.ac_comm[0] = '?';
    131 			ab.ac_comm[1] = '\0';
    132 		} else
    133 			for (p = &ab.ac_comm[0];
    134 			    p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
    135 				if (!isprint(*p))
    136 					*p = '?';
    137 		if (!*argv || requested(argv, &ab)) {
    138 
    139 			t = expand(ab.ac_utime) + expand(ab.ac_stime);
    140 			(void)printf(
    141 			    "%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
    142 			     (int)fldsiz(acct, ac_comm),
    143 			     (int)fldsiz(acct, ac_comm),
    144 			     ab.ac_comm, flagbits(ab.ac_flag),
    145 			     UT_NAMESIZE, UT_NAMESIZE,
    146 			     user_from_uid(ab.ac_uid, 0), UT_LINESIZE,
    147 			     UT_LINESIZE, getdev(ab.ac_tty),
    148 			     t / (double)AHZ, ctime(&ab.ac_btime));
    149 			delta = expand(ab.ac_etime) / (double)AHZ;
    150 			printf(" (%1.0f:%02.0f:%05.2f)\n",
    151 			       delta / SECSPERHOUR,
    152 			       fmod(delta, SECSPERHOUR) / SECSPERMIN,
    153 			       fmod(delta, SECSPERMIN));
    154 		}
    155 		/* are we at the beginning of the file yet? */
    156 		if (size == 0)
    157 			break;
    158 		/* seek backward over the one we read and the next to read */
    159 		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
    160 			err(1, "%s", acctfile);
    161 		/* and account for its size */
    162 		size -= sizeof(struct acct);
    163 	}
    164 	exit(0);
    165 }
    166 
    167 time_t
    168 expand(t)
    169 	u_int t;
    170 {
    171 	time_t nt;
    172 
    173 	nt = t & 017777;
    174 	t >>= 13;
    175 	while (t) {
    176 		t--;
    177 		nt <<= 3;
    178 	}
    179 	return (nt);
    180 }
    181 
    182 char *
    183 flagbits(f)
    184 	int f;
    185 {
    186 	static char flags[20] = "-";
    187 	char *p;
    188 
    189 #define	BIT(flag, ch)	if (f & flag) *p++ = ch
    190 
    191 	p = flags + 1;
    192 	BIT(ASU, 'S');
    193 	BIT(AFORK, 'F');
    194 	BIT(ACOMPAT, 'C');
    195 	BIT(ACORE, 'D');
    196 	BIT(AXSIG, 'X');
    197 	*p = '\0';
    198 	return (flags);
    199 }
    200 
    201 int
    202 requested(argv, acp)
    203 	char *argv[];
    204 	struct acct *acp;
    205 {
    206 	do {
    207 		if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
    208 			return (1);
    209 		if (!strcmp(getdev(acp->ac_tty), *argv))
    210 			return (1);
    211 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
    212 			return (1);
    213 	} while (*++argv);
    214 	return (0);
    215 }
    216 
    217 char *
    218 getdev(dev)
    219 	dev_t dev;
    220 {
    221 	static dev_t lastdev = (dev_t)-1;
    222 	static char *lastname;
    223 
    224 	if (dev == NODEV)			/* Special case. */
    225 		return ("__");
    226 	if (dev == lastdev)			/* One-element cache. */
    227 		return (lastname);
    228 	lastdev = dev;
    229 	if ((lastname = devname(dev, S_IFCHR)) == NULL)
    230 		lastname = "??";
    231 	return (lastname);
    232 }
    233 
    234 void
    235 usage()
    236 {
    237 	(void)fprintf(stderr,
    238 	    "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
    239 	exit(1);
    240 }
    241