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