xref: /onnv-gate/usr/src/cmd/lastcomm/lc_pacct.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
3*0Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
4*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
7*0Sstevel@tonic-gate  * All rights reserved.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  *
10*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
11*0Sstevel@tonic-gate  * Use is subject to license terms.
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #include "lastcomm.h"
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate /*
19*0Sstevel@tonic-gate  * lc_pacct() provides the functionality of lastcomm when applied to the basic
20*0Sstevel@tonic-gate  * SVR4 accounting file, /var/adm/pacct.  Definitions for this accounting file
21*0Sstevel@tonic-gate  * format are given in <sys/acct.h>.
22*0Sstevel@tonic-gate  */
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate extern ulong_t expand(comp_t);
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate static int
ok(int argc,char * argv[],int index,struct acct * acp)27*0Sstevel@tonic-gate ok(int argc, char *argv[], int index, struct acct *acp)
28*0Sstevel@tonic-gate {
29*0Sstevel@tonic-gate 	int j;
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 	for (j = index; j < argc; j++)
32*0Sstevel@tonic-gate 		if (strcmp(getname(acp->ac_uid), argv[j]) &&
33*0Sstevel@tonic-gate 		    strcmp(getdev(acp->ac_tty), argv[j]) &&
34*0Sstevel@tonic-gate 		    strncmp(acp->ac_comm, argv[j], fldsiz(acct, ac_comm)))
35*0Sstevel@tonic-gate 			break;
36*0Sstevel@tonic-gate 	return (j == argc);
37*0Sstevel@tonic-gate }
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate int
lc_pacct(char * name,int argc,char * argv[],int index)40*0Sstevel@tonic-gate lc_pacct(char *name, int argc, char *argv[], int index)
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate 	struct acct buf[NACCT];
43*0Sstevel@tonic-gate 	int bn, cc;
44*0Sstevel@tonic-gate 	struct acct *acp;
45*0Sstevel@tonic-gate 	struct stat sb;
46*0Sstevel@tonic-gate 	time_t t;
47*0Sstevel@tonic-gate 	int fd;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	if ((fd = open(name, O_RDONLY)) < 0) {
50*0Sstevel@tonic-gate 		perror(name);
51*0Sstevel@tonic-gate 		return (1);
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	(void) fstat(fd, &sb);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	if (sb.st_size % sizeof (struct acct)) {
57*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("lastcomm: accounting file"
58*0Sstevel@tonic-gate 		    " is corrupted\n"));
59*0Sstevel@tonic-gate 		return (1);
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	for (bn = ((unsigned)sb.st_size / BUF_SIZ) + 1; bn >= 0; bn--) {
63*0Sstevel@tonic-gate 		if (lseek(fd, (unsigned)bn * BUF_SIZ, 0) == -1) {
64*0Sstevel@tonic-gate 			perror("lseek");
65*0Sstevel@tonic-gate 			return (1);
66*0Sstevel@tonic-gate 		}
67*0Sstevel@tonic-gate 		cc = read(fd, buf, BUF_SIZ);
68*0Sstevel@tonic-gate 		if (cc < 0) {
69*0Sstevel@tonic-gate 			perror("read");
70*0Sstevel@tonic-gate 			return (1);
71*0Sstevel@tonic-gate 		}
72*0Sstevel@tonic-gate 		acp = buf + (cc / sizeof (buf[0])) - 1;
73*0Sstevel@tonic-gate 		for (; acp >= buf; acp--) {
74*0Sstevel@tonic-gate 			char *cp;
75*0Sstevel@tonic-gate 			ulong_t x;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 			if (acp->ac_flag > 0100) {
78*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("lastcomm: "
79*0Sstevel@tonic-gate 				    "accounting file is corrupted\n"));
80*0Sstevel@tonic-gate 				return (1);
81*0Sstevel@tonic-gate 			}
82*0Sstevel@tonic-gate 			if (acp->ac_comm[0] == '\0')
83*0Sstevel@tonic-gate 				(void) strcpy(acp->ac_comm, "?");
84*0Sstevel@tonic-gate 			for (cp = &acp->ac_comm[0];
85*0Sstevel@tonic-gate 			    cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
86*0Sstevel@tonic-gate 			    cp++)
87*0Sstevel@tonic-gate 				if (!isascii(*cp) || iscntrl(*cp))
88*0Sstevel@tonic-gate 					*cp = '?';
89*0Sstevel@tonic-gate 			if (argc > index && !ok(argc, argv, index, acp))
90*0Sstevel@tonic-gate 				continue;
91*0Sstevel@tonic-gate 			x = expand(acp->ac_utime) + expand(acp->ac_stime);
92*0Sstevel@tonic-gate 			t = acp->ac_btime;
93*0Sstevel@tonic-gate 			(void) printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
94*0Sstevel@tonic-gate 			    fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
95*0Sstevel@tonic-gate 			    acp->ac_comm,
96*0Sstevel@tonic-gate 			    flagbits(acp->ac_flag),
97*0Sstevel@tonic-gate 			    NMAX, getname(acp->ac_uid),
98*0Sstevel@tonic-gate 			    LMAX, getdev(acp->ac_tty),
99*0Sstevel@tonic-gate 			    x / (double)HZ, ctime(&t));
100*0Sstevel@tonic-gate 		}
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 	return (0);
103*0Sstevel@tonic-gate }
104