xref: /titanic_50/usr/src/cmd/acct/acctcom.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.37 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <time.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
39*7c478bd9Sstevel@tonic-gate #include <grp.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/acct.h>
41*7c478bd9Sstevel@tonic-gate #include <pwd.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
43*7c478bd9Sstevel@tonic-gate #include <locale.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate struct	acct ab;
46*7c478bd9Sstevel@tonic-gate char	command_name[16];
47*7c478bd9Sstevel@tonic-gate char	obuf[BUFSIZ];
48*7c478bd9Sstevel@tonic-gate static char	time_buf[50];
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate double	cpucut,
51*7c478bd9Sstevel@tonic-gate 	syscut,
52*7c478bd9Sstevel@tonic-gate 	hogcut,
53*7c478bd9Sstevel@tonic-gate 	iocut,
54*7c478bd9Sstevel@tonic-gate 	realtot,
55*7c478bd9Sstevel@tonic-gate 	cputot,
56*7c478bd9Sstevel@tonic-gate 	usertot,
57*7c478bd9Sstevel@tonic-gate 	systot,
58*7c478bd9Sstevel@tonic-gate 	kcoretot,
59*7c478bd9Sstevel@tonic-gate 	iotot,
60*7c478bd9Sstevel@tonic-gate 	rwtot;
61*7c478bd9Sstevel@tonic-gate extern long	timezone;
62*7c478bd9Sstevel@tonic-gate extern int	daylight;	/* daylight savings time if set */
63*7c478bd9Sstevel@tonic-gate long	daydiff,
64*7c478bd9Sstevel@tonic-gate 	offset = -2,
65*7c478bd9Sstevel@tonic-gate 	cmdcount;
66*7c478bd9Sstevel@tonic-gate ulong_t	elapsed,
67*7c478bd9Sstevel@tonic-gate 	sys,
68*7c478bd9Sstevel@tonic-gate 	user,
69*7c478bd9Sstevel@tonic-gate 	cpu,
70*7c478bd9Sstevel@tonic-gate 	io,
71*7c478bd9Sstevel@tonic-gate 	rw,
72*7c478bd9Sstevel@tonic-gate 	mem,
73*7c478bd9Sstevel@tonic-gate 	etime;
74*7c478bd9Sstevel@tonic-gate time_t	tstrt_b,
75*7c478bd9Sstevel@tonic-gate 	tstrt_a,
76*7c478bd9Sstevel@tonic-gate 	tend_b,
77*7c478bd9Sstevel@tonic-gate 	tend_a;
78*7c478bd9Sstevel@tonic-gate int	backward,
79*7c478bd9Sstevel@tonic-gate 	flag_field,
80*7c478bd9Sstevel@tonic-gate 	average,
81*7c478bd9Sstevel@tonic-gate 	quiet,
82*7c478bd9Sstevel@tonic-gate 	option,
83*7c478bd9Sstevel@tonic-gate 	verbose = 1,
84*7c478bd9Sstevel@tonic-gate 	uidflag,
85*7c478bd9Sstevel@tonic-gate 	gidflag,
86*7c478bd9Sstevel@tonic-gate 	unkid,	/*user doesn't have login on this machine*/
87*7c478bd9Sstevel@tonic-gate 	errflg,
88*7c478bd9Sstevel@tonic-gate 	su_user,
89*7c478bd9Sstevel@tonic-gate 	fileout = 0,
90*7c478bd9Sstevel@tonic-gate 	stdinflg,
91*7c478bd9Sstevel@tonic-gate 	nfiles;
92*7c478bd9Sstevel@tonic-gate static int	eflg = 0,
93*7c478bd9Sstevel@tonic-gate 	Eflg = 0,
94*7c478bd9Sstevel@tonic-gate 	sflg = 0,
95*7c478bd9Sstevel@tonic-gate 	Sflg = 0;
96*7c478bd9Sstevel@tonic-gate #ifdef uts
97*7c478bd9Sstevel@tonic-gate dev_t   linedev = 0xffff;  /* changed from -1, as dev_t is now ushort */
98*7c478bd9Sstevel@tonic-gate #else
99*7c478bd9Sstevel@tonic-gate dev_t	linedev = (dev_t)-1;
100*7c478bd9Sstevel@tonic-gate #endif
101*7c478bd9Sstevel@tonic-gate uid_t	uidval;
102*7c478bd9Sstevel@tonic-gate gid_t	gidval;
103*7c478bd9Sstevel@tonic-gate char	*cname = NULL; /* command name pattern to match*/
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate struct passwd *getpwnam(), *getpwuid(), *pw;
106*7c478bd9Sstevel@tonic-gate struct group *getgrnam(),*grp;
107*7c478bd9Sstevel@tonic-gate long	convtime();
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate #ifdef uts
110*7c478bd9Sstevel@tonic-gate float   expand();
111*7c478bd9Sstevel@tonic-gate #else
112*7c478bd9Sstevel@tonic-gate ulong_t	expand();
113*7c478bd9Sstevel@tonic-gate #endif
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate char	*ofile,
116*7c478bd9Sstevel@tonic-gate 	*devtolin(),
117*7c478bd9Sstevel@tonic-gate 	*uidtonam();
118*7c478bd9Sstevel@tonic-gate dev_t	lintodev();
119*7c478bd9Sstevel@tonic-gate FILE	*ostrm;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate main(argc, argv)
122*7c478bd9Sstevel@tonic-gate char **argv;
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	register int	c;
125*7c478bd9Sstevel@tonic-gate 	extern int	optind;
126*7c478bd9Sstevel@tonic-gate 	extern char	*optarg;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
129*7c478bd9Sstevel@tonic-gate 	setbuf(stdout,obuf);
130*7c478bd9Sstevel@tonic-gate 	while((c = getopt(argc, argv,
131*7c478bd9Sstevel@tonic-gate 		"C:E:H:I:O:S:abe:fg:hikl:mn:o:qrs:tu:v")) != EOF) {
132*7c478bd9Sstevel@tonic-gate 		switch(c) {
133*7c478bd9Sstevel@tonic-gate 		case 'C':
134*7c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&cpucut);
135*7c478bd9Sstevel@tonic-gate 			continue;
136*7c478bd9Sstevel@tonic-gate 		case 'O':
137*7c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&syscut);
138*7c478bd9Sstevel@tonic-gate 			continue;
139*7c478bd9Sstevel@tonic-gate 		case 'H':
140*7c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&hogcut);
141*7c478bd9Sstevel@tonic-gate 			continue;
142*7c478bd9Sstevel@tonic-gate 		case 'I':
143*7c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&iocut);
144*7c478bd9Sstevel@tonic-gate 			continue;
145*7c478bd9Sstevel@tonic-gate 		case 'a':
146*7c478bd9Sstevel@tonic-gate 			average++;
147*7c478bd9Sstevel@tonic-gate 			continue;
148*7c478bd9Sstevel@tonic-gate 		case 'b':
149*7c478bd9Sstevel@tonic-gate 			backward++;
150*7c478bd9Sstevel@tonic-gate 			continue;
151*7c478bd9Sstevel@tonic-gate 		case 'g':
152*7c478bd9Sstevel@tonic-gate 			if(sscanf(optarg,"%ld",&gidval) == 1) {
153*7c478bd9Sstevel@tonic-gate 				if (getgrgid(gidval) == NULL)
154*7c478bd9Sstevel@tonic-gate 					fatal("Unknown group", optarg);
155*7c478bd9Sstevel@tonic-gate 			} else if((grp=getgrnam(optarg)) == NULL)
156*7c478bd9Sstevel@tonic-gate 				fatal("Unknown group", optarg);
157*7c478bd9Sstevel@tonic-gate 			else
158*7c478bd9Sstevel@tonic-gate 				gidval=grp->gr_gid;
159*7c478bd9Sstevel@tonic-gate 			gidflag++;
160*7c478bd9Sstevel@tonic-gate 			continue;
161*7c478bd9Sstevel@tonic-gate 		case 'h':
162*7c478bd9Sstevel@tonic-gate 			option |= HOGFACTOR;
163*7c478bd9Sstevel@tonic-gate 			continue;
164*7c478bd9Sstevel@tonic-gate 		case 'i':
165*7c478bd9Sstevel@tonic-gate 			option |= IORW;
166*7c478bd9Sstevel@tonic-gate 			continue;
167*7c478bd9Sstevel@tonic-gate 		case 'k':
168*7c478bd9Sstevel@tonic-gate 			option |= KCOREMIN;
169*7c478bd9Sstevel@tonic-gate 			continue;
170*7c478bd9Sstevel@tonic-gate 		case 'm':
171*7c478bd9Sstevel@tonic-gate 			option |= MEANSIZE;
172*7c478bd9Sstevel@tonic-gate 			continue;
173*7c478bd9Sstevel@tonic-gate 		case 'n':
174*7c478bd9Sstevel@tonic-gate 			cname=(char *)cmset(optarg);
175*7c478bd9Sstevel@tonic-gate 			continue;
176*7c478bd9Sstevel@tonic-gate 		case 't':
177*7c478bd9Sstevel@tonic-gate 			option |= SEPTIME;
178*7c478bd9Sstevel@tonic-gate 			continue;
179*7c478bd9Sstevel@tonic-gate 		case 'r':
180*7c478bd9Sstevel@tonic-gate 			option |= CPUFACTOR;
181*7c478bd9Sstevel@tonic-gate 			continue;
182*7c478bd9Sstevel@tonic-gate 		case 'v':
183*7c478bd9Sstevel@tonic-gate 			verbose=0;
184*7c478bd9Sstevel@tonic-gate 			continue;
185*7c478bd9Sstevel@tonic-gate 		case 'l':
186*7c478bd9Sstevel@tonic-gate 			linedev = lintodev(optarg);
187*7c478bd9Sstevel@tonic-gate 			continue;
188*7c478bd9Sstevel@tonic-gate 		case 'u':
189*7c478bd9Sstevel@tonic-gate 			if(*optarg == '?') {
190*7c478bd9Sstevel@tonic-gate 				unkid++;
191*7c478bd9Sstevel@tonic-gate 				continue;
192*7c478bd9Sstevel@tonic-gate 			}
193*7c478bd9Sstevel@tonic-gate 			if(*optarg == '#') {
194*7c478bd9Sstevel@tonic-gate 				su_user++;
195*7c478bd9Sstevel@tonic-gate 				uidval = 0;
196*7c478bd9Sstevel@tonic-gate 				uidflag++;
197*7c478bd9Sstevel@tonic-gate 				continue;
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 			if((pw = getpwnam(optarg)) == NULL) {
200*7c478bd9Sstevel@tonic-gate 				uidval = (uid_t)atoi(optarg);
201*7c478bd9Sstevel@tonic-gate 				/* atoi will return 0 in abnormal situation */
202*7c478bd9Sstevel@tonic-gate 				if (uidval == 0 && strcmp(optarg, "0") != 0) {
203*7c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%s: Unknown user %s\n", argv[0], optarg);
204*7c478bd9Sstevel@tonic-gate 					exit(1);
205*7c478bd9Sstevel@tonic-gate 				}
206*7c478bd9Sstevel@tonic-gate  				if ((pw = getpwuid(uidval)) == NULL) {
207*7c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%s: Unknown user %s\n", argv[0], optarg);
208*7c478bd9Sstevel@tonic-gate 					exit(1);
209*7c478bd9Sstevel@tonic-gate 				}
210*7c478bd9Sstevel@tonic-gate 				uidflag++;
211*7c478bd9Sstevel@tonic-gate 			} else {
212*7c478bd9Sstevel@tonic-gate 				uidval = pw->pw_uid;
213*7c478bd9Sstevel@tonic-gate 				uidflag++;
214*7c478bd9Sstevel@tonic-gate 			}
215*7c478bd9Sstevel@tonic-gate 			continue;
216*7c478bd9Sstevel@tonic-gate 		case 'q':
217*7c478bd9Sstevel@tonic-gate 			quiet++;
218*7c478bd9Sstevel@tonic-gate 			verbose=0;
219*7c478bd9Sstevel@tonic-gate 			average++;
220*7c478bd9Sstevel@tonic-gate 			continue;
221*7c478bd9Sstevel@tonic-gate 		case 's':
222*7c478bd9Sstevel@tonic-gate 			sflg = 1;
223*7c478bd9Sstevel@tonic-gate 			tend_a = convtime(optarg);
224*7c478bd9Sstevel@tonic-gate 			continue;
225*7c478bd9Sstevel@tonic-gate 		case 'S':
226*7c478bd9Sstevel@tonic-gate 			Sflg = 1;
227*7c478bd9Sstevel@tonic-gate 			tstrt_a = convtime(optarg);
228*7c478bd9Sstevel@tonic-gate 			continue;
229*7c478bd9Sstevel@tonic-gate 		case 'f':
230*7c478bd9Sstevel@tonic-gate 			flag_field++;
231*7c478bd9Sstevel@tonic-gate 			continue;
232*7c478bd9Sstevel@tonic-gate 		case 'e':
233*7c478bd9Sstevel@tonic-gate 			eflg = 1;
234*7c478bd9Sstevel@tonic-gate 			tstrt_b = convtime(optarg);
235*7c478bd9Sstevel@tonic-gate 			continue;
236*7c478bd9Sstevel@tonic-gate 		case 'E':
237*7c478bd9Sstevel@tonic-gate 			Eflg = 1;
238*7c478bd9Sstevel@tonic-gate 			tend_b = convtime(optarg);
239*7c478bd9Sstevel@tonic-gate 			continue;
240*7c478bd9Sstevel@tonic-gate 		case 'o':
241*7c478bd9Sstevel@tonic-gate 			ofile = optarg;
242*7c478bd9Sstevel@tonic-gate 			fileout++;
243*7c478bd9Sstevel@tonic-gate 			if((ostrm = fopen(ofile, "w")) == NULL) {
244*7c478bd9Sstevel@tonic-gate 				perror("open error on output file");
245*7c478bd9Sstevel@tonic-gate 				errflg++;
246*7c478bd9Sstevel@tonic-gate 			}
247*7c478bd9Sstevel@tonic-gate 			continue;
248*7c478bd9Sstevel@tonic-gate 		case '?':
249*7c478bd9Sstevel@tonic-gate 			errflg++;
250*7c478bd9Sstevel@tonic-gate 			continue;
251*7c478bd9Sstevel@tonic-gate 		}
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	if(errflg) {
255*7c478bd9Sstevel@tonic-gate 		usage();
256*7c478bd9Sstevel@tonic-gate 		exit(1);
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
261*7c478bd9Sstevel@tonic-gate 	while(optind++ < argc) {
262*7c478bd9Sstevel@tonic-gate 		dofile(*argv++);    /* change from *argv */
263*7c478bd9Sstevel@tonic-gate 		nfiles++;
264*7c478bd9Sstevel@tonic-gate 	}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	if(nfiles==0) {
267*7c478bd9Sstevel@tonic-gate 		if(isatty(0) || isdevnull())
268*7c478bd9Sstevel@tonic-gate 			dofile(PACCT);
269*7c478bd9Sstevel@tonic-gate 		else {
270*7c478bd9Sstevel@tonic-gate 			stdinflg = 1;
271*7c478bd9Sstevel@tonic-gate 			backward = offset = 0;
272*7c478bd9Sstevel@tonic-gate 			dofile(NULL);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 	doexit(0);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate dofile(fname)
279*7c478bd9Sstevel@tonic-gate char *fname;
280*7c478bd9Sstevel@tonic-gate {
281*7c478bd9Sstevel@tonic-gate 	register struct acct *a = &ab;
282*7c478bd9Sstevel@tonic-gate 	struct tm *t;
283*7c478bd9Sstevel@tonic-gate 	time_t curtime;
284*7c478bd9Sstevel@tonic-gate 	time_t	ts_a = 0,
285*7c478bd9Sstevel@tonic-gate 		ts_b = 0,
286*7c478bd9Sstevel@tonic-gate 		te_a = 0,
287*7c478bd9Sstevel@tonic-gate 		te_b = 0;
288*7c478bd9Sstevel@tonic-gate 	long	daystart;
289*7c478bd9Sstevel@tonic-gate 	long	nsize;
290*7c478bd9Sstevel@tonic-gate 	int	ver;	/* version of acct structure */
291*7c478bd9Sstevel@tonic-gate 	int	dst_secs;	/* number of seconds to adjust
292*7c478bd9Sstevel@tonic-gate 				   for daylight savings time */
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if(fname != NULL)
295*7c478bd9Sstevel@tonic-gate 		if(freopen(fname, "r", stdin) == NULL) {
296*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctcom: cannot open %s\n", fname);
297*7c478bd9Sstevel@tonic-gate 			return;
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1)
301*7c478bd9Sstevel@tonic-gate 		return;
302*7c478bd9Sstevel@tonic-gate 	else if (ab.ac_flag & AEXPND)
303*7c478bd9Sstevel@tonic-gate 		ver = 2;	/* 4.0 acct structure */
304*7c478bd9Sstevel@tonic-gate 	else
305*7c478bd9Sstevel@tonic-gate 		ver = 1;	/* 3.x acct structure */
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	rewind(stdin);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	if(backward) {
311*7c478bd9Sstevel@tonic-gate 		if (ver == 2)
312*7c478bd9Sstevel@tonic-gate 			nsize = sizeof(struct acct);	/* make sure offset is signed */
313*7c478bd9Sstevel@tonic-gate 		else
314*7c478bd9Sstevel@tonic-gate 			nsize = sizeof(struct o_acct);	/* make sure offset is signed */
315*7c478bd9Sstevel@tonic-gate 		fseek(stdin, (long)(-nsize), 2);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 	tzset();
318*7c478bd9Sstevel@tonic-gate 	daydiff = a->ac_btime - (a->ac_btime % SECSINDAY);
319*7c478bd9Sstevel@tonic-gate 	time(&curtime);
320*7c478bd9Sstevel@tonic-gate 	t = localtime(&curtime);
321*7c478bd9Sstevel@tonic-gate 	if (daydiff < (curtime - (curtime % SECSINDAY))) {
322*7c478bd9Sstevel@tonic-gate 		time_t t;
323*7c478bd9Sstevel@tonic-gate 		/*
324*7c478bd9Sstevel@tonic-gate 		 * it is older than today
325*7c478bd9Sstevel@tonic-gate 		 */
326*7c478bd9Sstevel@tonic-gate 		t = (time_t)a->ac_btime;
327*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &t);
328*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\nACCOUNTING RECORDS FROM:  %s", time_buf);
329*7c478bd9Sstevel@tonic-gate 	}
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/* adjust time by one hour for daylight savings time */
332*7c478bd9Sstevel@tonic-gate 	if (daylight && t->tm_isdst != 0)
333*7c478bd9Sstevel@tonic-gate 		dst_secs = 3600;
334*7c478bd9Sstevel@tonic-gate 	else
335*7c478bd9Sstevel@tonic-gate 		dst_secs = 0;
336*7c478bd9Sstevel@tonic-gate 	daystart = (a->ac_btime - timezone + dst_secs) -
337*7c478bd9Sstevel@tonic-gate 	    ((a->ac_btime - timezone + dst_secs) % SECSINDAY);
338*7c478bd9Sstevel@tonic-gate 	if (Sflg) {
339*7c478bd9Sstevel@tonic-gate 		ts_a = tstrt_a + daystart - dst_secs;
340*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &ts_a);
341*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "START AFT: %s", time_buf);
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 	if (eflg) {
344*7c478bd9Sstevel@tonic-gate 		ts_b = tstrt_b + daystart - dst_secs;
345*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &ts_b);
346*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "START BEF: %s", time_buf);
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 	if (sflg) {
349*7c478bd9Sstevel@tonic-gate 		te_a = tend_a + daystart - dst_secs;
350*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &te_a);
351*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "END AFTER: %s", time_buf);
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 	if (Eflg) {
354*7c478bd9Sstevel@tonic-gate 		te_b = tend_b + daystart - dst_secs;
355*7c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &te_b);
356*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "END BEFOR: %s", time_buf);
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 	if(ts_a) {
359*7c478bd9Sstevel@tonic-gate 		if (te_b && ts_a > te_b) te_b += SECSINDAY;
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	while(aread(ver) != 0) {
363*7c478bd9Sstevel@tonic-gate 		elapsed = expand(a->ac_etime);
364*7c478bd9Sstevel@tonic-gate 		etime = (ulong_t)a->ac_btime + (ulong_t)SECS(elapsed);
365*7c478bd9Sstevel@tonic-gate 		if(ts_a || ts_b || te_a || te_b) {
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 			if(te_a && (etime < te_a)) {
368*7c478bd9Sstevel@tonic-gate 				if(backward) return;
369*7c478bd9Sstevel@tonic-gate 				else continue;
370*7c478bd9Sstevel@tonic-gate 			}
371*7c478bd9Sstevel@tonic-gate 			if(te_b && (etime > te_b)) {
372*7c478bd9Sstevel@tonic-gate 				if(backward) continue;
373*7c478bd9Sstevel@tonic-gate 				else return;
374*7c478bd9Sstevel@tonic-gate 			}
375*7c478bd9Sstevel@tonic-gate 			if(ts_a && (a->ac_btime < ts_a))
376*7c478bd9Sstevel@tonic-gate 				continue;
377*7c478bd9Sstevel@tonic-gate 			if(ts_b && (a->ac_btime > ts_b))
378*7c478bd9Sstevel@tonic-gate 				continue;
379*7c478bd9Sstevel@tonic-gate 		}
380*7c478bd9Sstevel@tonic-gate 		if(!MYKIND(a->ac_flag))
381*7c478bd9Sstevel@tonic-gate 			continue;
382*7c478bd9Sstevel@tonic-gate 		if(su_user && !SU(a->ac_flag))
383*7c478bd9Sstevel@tonic-gate 			continue;
384*7c478bd9Sstevel@tonic-gate 		sys = expand(a->ac_stime);
385*7c478bd9Sstevel@tonic-gate 		user = expand(a->ac_utime);
386*7c478bd9Sstevel@tonic-gate 		cpu = sys + user;
387*7c478bd9Sstevel@tonic-gate 		if(cpu == 0)
388*7c478bd9Sstevel@tonic-gate 			cpu = 1;
389*7c478bd9Sstevel@tonic-gate 		mem = expand(a->ac_mem);
390*7c478bd9Sstevel@tonic-gate 		(void) strncpy(command_name, a->ac_comm, 8);
391*7c478bd9Sstevel@tonic-gate 		io=expand(a->ac_io);
392*7c478bd9Sstevel@tonic-gate 		rw=expand(a->ac_rw);
393*7c478bd9Sstevel@tonic-gate 		if(cpucut && cpucut >= SECS(cpu))
394*7c478bd9Sstevel@tonic-gate 			continue;
395*7c478bd9Sstevel@tonic-gate 		if(syscut && syscut >= SECS(sys))
396*7c478bd9Sstevel@tonic-gate 			continue;
397*7c478bd9Sstevel@tonic-gate #ifdef uts
398*7c478bd9Sstevel@tonic-gate 		if(linedev != 0xffff && a->ac_tty != linedev)
399*7c478bd9Sstevel@tonic-gate 			continue;
400*7c478bd9Sstevel@tonic-gate #else
401*7c478bd9Sstevel@tonic-gate 		if(linedev != (dev_t)-1 && a->ac_tty != linedev)
402*7c478bd9Sstevel@tonic-gate 			continue;
403*7c478bd9Sstevel@tonic-gate #endif
404*7c478bd9Sstevel@tonic-gate 		if(uidflag && a->ac_uid != uidval)
405*7c478bd9Sstevel@tonic-gate 			continue;
406*7c478bd9Sstevel@tonic-gate 		if(gidflag && a->ac_gid != gidval)
407*7c478bd9Sstevel@tonic-gate 			continue;
408*7c478bd9Sstevel@tonic-gate 		if(cname && !cmatch(a->ac_comm,cname))
409*7c478bd9Sstevel@tonic-gate 			continue;
410*7c478bd9Sstevel@tonic-gate 		if(iocut && iocut > io)
411*7c478bd9Sstevel@tonic-gate 			continue;
412*7c478bd9Sstevel@tonic-gate 		if(unkid && uidtonam(a->ac_uid)[0] != '?')
413*7c478bd9Sstevel@tonic-gate 			continue;
414*7c478bd9Sstevel@tonic-gate 		if(verbose && (fileout == 0)) {
415*7c478bd9Sstevel@tonic-gate 			printhd();
416*7c478bd9Sstevel@tonic-gate 			verbose = 0;
417*7c478bd9Sstevel@tonic-gate 		}
418*7c478bd9Sstevel@tonic-gate 		if(elapsed == 0)
419*7c478bd9Sstevel@tonic-gate 			elapsed++;
420*7c478bd9Sstevel@tonic-gate 		if(hogcut && hogcut >= (double)cpu/(double)elapsed)
421*7c478bd9Sstevel@tonic-gate 			continue;
422*7c478bd9Sstevel@tonic-gate 		if(fileout)
423*7c478bd9Sstevel@tonic-gate 			fwrite(&ab, sizeof(ab), 1, ostrm);
424*7c478bd9Sstevel@tonic-gate 		else
425*7c478bd9Sstevel@tonic-gate 			println();
426*7c478bd9Sstevel@tonic-gate 		if(average) {
427*7c478bd9Sstevel@tonic-gate 			cmdcount++;
428*7c478bd9Sstevel@tonic-gate 			realtot += (double)elapsed;
429*7c478bd9Sstevel@tonic-gate 			usertot += (double)user;
430*7c478bd9Sstevel@tonic-gate 			systot += (double)sys;
431*7c478bd9Sstevel@tonic-gate 			kcoretot += (double)mem;
432*7c478bd9Sstevel@tonic-gate 			iotot += (double)io;
433*7c478bd9Sstevel@tonic-gate 			rwtot += (double)rw;
434*7c478bd9Sstevel@tonic-gate 		};
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate aread(ver)
439*7c478bd9Sstevel@tonic-gate int ver;
440*7c478bd9Sstevel@tonic-gate {
441*7c478bd9Sstevel@tonic-gate 	static	 ok = 1;
442*7c478bd9Sstevel@tonic-gate 	struct o_acct oab;
443*7c478bd9Sstevel@tonic-gate 	int ret;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	if (ver != 2) {
446*7c478bd9Sstevel@tonic-gate 		if ((ret = fread((char *)&oab, sizeof(struct o_acct), 1, stdin)) == 1){
447*7c478bd9Sstevel@tonic-gate 			/* copy SVR3 acct struct to SVR4 acct struct */
448*7c478bd9Sstevel@tonic-gate 			ab.ac_flag = oab.ac_flag | AEXPND;
449*7c478bd9Sstevel@tonic-gate 			ab.ac_stat = oab.ac_stat;
450*7c478bd9Sstevel@tonic-gate 			ab.ac_uid = (uid_t) oab.ac_uid;
451*7c478bd9Sstevel@tonic-gate 			ab.ac_gid = (gid_t) oab.ac_gid;
452*7c478bd9Sstevel@tonic-gate 			ab.ac_tty = (dev_t) oab.ac_tty;
453*7c478bd9Sstevel@tonic-gate 			ab.ac_btime = oab.ac_btime;
454*7c478bd9Sstevel@tonic-gate 			ab.ac_utime = oab.ac_utime;
455*7c478bd9Sstevel@tonic-gate 			ab.ac_stime = oab.ac_stime;
456*7c478bd9Sstevel@tonic-gate 			ab.ac_mem = oab.ac_mem;
457*7c478bd9Sstevel@tonic-gate 			ab.ac_io = oab.ac_io;
458*7c478bd9Sstevel@tonic-gate 			ab.ac_rw = oab.ac_rw;
459*7c478bd9Sstevel@tonic-gate 			strcpy(ab.ac_comm, oab.ac_comm);
460*7c478bd9Sstevel@tonic-gate 		}
461*7c478bd9Sstevel@tonic-gate 	} else
462*7c478bd9Sstevel@tonic-gate 		ret = fread((char *)&ab, sizeof(struct acct), 1, stdin);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if(backward) {
466*7c478bd9Sstevel@tonic-gate 		if(ok) {
467*7c478bd9Sstevel@tonic-gate 			if(fseek(stdin,
468*7c478bd9Sstevel@tonic-gate 				(long)(offset*(ver == 2 ? sizeof(struct acct) :
469*7c478bd9Sstevel@tonic-gate 					sizeof(struct o_acct))), 1) != 0) {
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 					rewind(stdin);	/* get 1st record */
472*7c478bd9Sstevel@tonic-gate 					ok = 0;
473*7c478bd9Sstevel@tonic-gate 			}
474*7c478bd9Sstevel@tonic-gate 		} else
475*7c478bd9Sstevel@tonic-gate 			ret = 0;
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 	return(ret != 1 ? 0 : 1);
478*7c478bd9Sstevel@tonic-gate }
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate printhd()
481*7c478bd9Sstevel@tonic-gate {
482*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "COMMAND                           START    END          REAL");
483*7c478bd9Sstevel@tonic-gate 	ps("CPU");
484*7c478bd9Sstevel@tonic-gate 	if(option & SEPTIME)
485*7c478bd9Sstevel@tonic-gate 		ps("(SECS)");
486*7c478bd9Sstevel@tonic-gate 	if(option & IORW){
487*7c478bd9Sstevel@tonic-gate 		ps("CHARS");
488*7c478bd9Sstevel@tonic-gate 		ps("BLOCKS");
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
491*7c478bd9Sstevel@tonic-gate 		ps("CPU");
492*7c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
493*7c478bd9Sstevel@tonic-gate 		ps("HOG");
494*7c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
495*7c478bd9Sstevel@tonic-gate 		ps("MEAN");
496*7c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
497*7c478bd9Sstevel@tonic-gate 		ps("KCORE");
498*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
499*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "NAME       USER     TTYNAME       TIME     TIME       (SECS)");
500*7c478bd9Sstevel@tonic-gate 	if(option & SEPTIME) {
501*7c478bd9Sstevel@tonic-gate 		ps("SYS");
502*7c478bd9Sstevel@tonic-gate 		ps("USER");
503*7c478bd9Sstevel@tonic-gate 	} else
504*7c478bd9Sstevel@tonic-gate 		ps("(SECS)");
505*7c478bd9Sstevel@tonic-gate 	if(option & IORW) {
506*7c478bd9Sstevel@tonic-gate 		ps("TRNSFD");
507*7c478bd9Sstevel@tonic-gate 		ps("READ");
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
510*7c478bd9Sstevel@tonic-gate 		ps("FACTOR");
511*7c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
512*7c478bd9Sstevel@tonic-gate 		ps("FACTOR");
513*7c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
514*7c478bd9Sstevel@tonic-gate 		ps("SIZE(K)");
515*7c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
516*7c478bd9Sstevel@tonic-gate 		ps("MIN");
517*7c478bd9Sstevel@tonic-gate 	if(flag_field)
518*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  F STAT");
519*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
520*7c478bd9Sstevel@tonic-gate 	fflush(stdout);
521*7c478bd9Sstevel@tonic-gate }
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate println()
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate 	char name[32];
526*7c478bd9Sstevel@tonic-gate 	register struct acct *a = &ab;
527*7c478bd9Sstevel@tonic-gate 	time_t t;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	if(quiet)
530*7c478bd9Sstevel@tonic-gate 		return;
531*7c478bd9Sstevel@tonic-gate 	if(!SU(a->ac_flag))
532*7c478bd9Sstevel@tonic-gate 		strcpy(name,command_name);
533*7c478bd9Sstevel@tonic-gate 	else {
534*7c478bd9Sstevel@tonic-gate 		strcpy(name,"#");
535*7c478bd9Sstevel@tonic-gate 		strcat(name,command_name);
536*7c478bd9Sstevel@tonic-gate 	}
537*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%-*.*s", (OUTPUT_NSZ + 1),
538*7c478bd9Sstevel@tonic-gate 	    (OUTPUT_NSZ + 1), name);
539*7c478bd9Sstevel@tonic-gate 	strcpy(name,uidtonam(a->ac_uid));
540*7c478bd9Sstevel@tonic-gate 	if(*name != '?')
541*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %-*.*s", (OUTPUT_NSZ + 1),
542*7c478bd9Sstevel@tonic-gate 		    (OUTPUT_NSZ + 1), name);
543*7c478bd9Sstevel@tonic-gate 	else
544*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %-9d",a->ac_uid);
545*7c478bd9Sstevel@tonic-gate #ifdef uts
546*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, " %-*.*s", OUTPUT_LSZ, OUTPUT_LSZ,
547*7c478bd9Sstevel@tonic-gate 	    a->ac_tty != 0xffff? devtolin(a->ac_tty):"?");
548*7c478bd9Sstevel@tonic-gate #else
549*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, " %-*.*s", OUTPUT_LSZ, OUTPUT_LSZ,
550*7c478bd9Sstevel@tonic-gate 	    a->ac_tty != (dev_t)-1? devtolin(a->ac_tty):"?");
551*7c478bd9Sstevel@tonic-gate #endif
552*7c478bd9Sstevel@tonic-gate 	t = a->ac_btime;
553*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT1, &t);
554*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%.9s", time_buf);
555*7c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT1, (time_t *)&etime);
556*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%.9s ", time_buf);
557*7c478bd9Sstevel@tonic-gate 	pf((double)SECS(elapsed));
558*7c478bd9Sstevel@tonic-gate 	if(option & SEPTIME) {
559*7c478bd9Sstevel@tonic-gate 		pf((double)sys / HZ);
560*7c478bd9Sstevel@tonic-gate 		pf((double)user / HZ);
561*7c478bd9Sstevel@tonic-gate 	} else
562*7c478bd9Sstevel@tonic-gate 		pf((double)cpu / HZ);
563*7c478bd9Sstevel@tonic-gate 	if(option & IORW)
564*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, io < 100000000 ? "%8ld%8ld" : "%12ld%8ld",io,rw);
565*7c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
566*7c478bd9Sstevel@tonic-gate 		pf((double)user / cpu);
567*7c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
568*7c478bd9Sstevel@tonic-gate 		pf((double)cpu / elapsed);
569*7c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
570*7c478bd9Sstevel@tonic-gate 		pf(KCORE(mem / cpu));
571*7c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
572*7c478bd9Sstevel@tonic-gate 		pf(MINT(KCORE(mem)));
573*7c478bd9Sstevel@tonic-gate 	if(flag_field)
574*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %1o %3o", (unsigned char) a->ac_flag,
575*7c478bd9Sstevel@tonic-gate 						(unsigned char) a->ac_stat);
576*7c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
577*7c478bd9Sstevel@tonic-gate }
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate /*
580*7c478bd9Sstevel@tonic-gate  * convtime converts time arg to internal value
581*7c478bd9Sstevel@tonic-gate  * arg has form hr:min:sec, min or sec are assumed to be 0 if omitted
582*7c478bd9Sstevel@tonic-gate  */
583*7c478bd9Sstevel@tonic-gate long
584*7c478bd9Sstevel@tonic-gate convtime(str)
585*7c478bd9Sstevel@tonic-gate char *str;
586*7c478bd9Sstevel@tonic-gate {
587*7c478bd9Sstevel@tonic-gate 	long	hr, min, sec;
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 	min = sec = 0;
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	if(sscanf(str, "%ld:%ld:%ld", &hr, &min, &sec) < 1) {
592*7c478bd9Sstevel@tonic-gate 		fatal("acctcom: bad time:", str);
593*7c478bd9Sstevel@tonic-gate 	}
594*7c478bd9Sstevel@tonic-gate 	tzset();
595*7c478bd9Sstevel@tonic-gate 	sec += (min*60);
596*7c478bd9Sstevel@tonic-gate 	sec += (hr*3600);
597*7c478bd9Sstevel@tonic-gate 	return(sec + timezone);
598*7c478bd9Sstevel@tonic-gate }
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate cmatch(comm, cstr)
601*7c478bd9Sstevel@tonic-gate register char	*comm, *cstr;
602*7c478bd9Sstevel@tonic-gate {
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	char	xcomm[9];
605*7c478bd9Sstevel@tonic-gate 	register i;
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate 	for(i=0;i<8;i++){
608*7c478bd9Sstevel@tonic-gate 		if(comm[i]==' '||comm[i]=='\0')
609*7c478bd9Sstevel@tonic-gate 			break;
610*7c478bd9Sstevel@tonic-gate 		xcomm[i] = comm[i];
611*7c478bd9Sstevel@tonic-gate 	}
612*7c478bd9Sstevel@tonic-gate 	xcomm[i] = '\0';
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	return(regex(cstr,xcomm));
615*7c478bd9Sstevel@tonic-gate }
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate cmset(pattern)
618*7c478bd9Sstevel@tonic-gate register char	*pattern;
619*7c478bd9Sstevel@tonic-gate {
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	if((pattern=(char *)regcmp(pattern,(char *)0))==NULL){
622*7c478bd9Sstevel@tonic-gate 		fatal("pattern syntax", NULL);
623*7c478bd9Sstevel@tonic-gate 	}
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	return((unsigned)pattern);
626*7c478bd9Sstevel@tonic-gate }
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate doexit(status)
629*7c478bd9Sstevel@tonic-gate {
630*7c478bd9Sstevel@tonic-gate 	if(!average)
631*7c478bd9Sstevel@tonic-gate 		exit(status);
632*7c478bd9Sstevel@tonic-gate 	if(cmdcount) {
633*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "cmds=%ld ",cmdcount);
634*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Real=%-6.2f ",SECS(realtot)/cmdcount);
635*7c478bd9Sstevel@tonic-gate 		cputot = systot + usertot;
636*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "CPU=%-6.2f ",SECS(cputot)/cmdcount);
637*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "USER=%-6.2f ",SECS(usertot)/cmdcount);
638*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "SYS=%-6.2f ",SECS(systot)/cmdcount);
639*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "CHAR=%-8.2f ",iotot/cmdcount);
640*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "BLK=%-8.2f ",rwtot/cmdcount);
641*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "USR/TOT=%-4.2f ",usertot/cputot);
642*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "HOG=%-4.2f ",cputot/realtot);
643*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\n");
644*7c478bd9Sstevel@tonic-gate 	}
645*7c478bd9Sstevel@tonic-gate 	else
646*7c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\nNo commands matched\n");
647*7c478bd9Sstevel@tonic-gate 	exit(status);
648*7c478bd9Sstevel@tonic-gate }
649*7c478bd9Sstevel@tonic-gate isdevnull()
650*7c478bd9Sstevel@tonic-gate {
651*7c478bd9Sstevel@tonic-gate 	struct stat	filearg;
652*7c478bd9Sstevel@tonic-gate 	struct stat	devnull;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	if(fstat(0,&filearg) == -1) {
655*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,"acctcom: cannot stat stdin\n");
656*7c478bd9Sstevel@tonic-gate 		return(NULL);
657*7c478bd9Sstevel@tonic-gate 	}
658*7c478bd9Sstevel@tonic-gate 	if(stat("/dev/null",&devnull) == -1) {
659*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,"acctcom: cannot stat /dev/null\n");
660*7c478bd9Sstevel@tonic-gate 		return(NULL);
661*7c478bd9Sstevel@tonic-gate 	}
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 	if(filearg.st_rdev == devnull.st_rdev) return(1);
664*7c478bd9Sstevel@tonic-gate 	else return(NULL);
665*7c478bd9Sstevel@tonic-gate }
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate fatal(s1, s2)
668*7c478bd9Sstevel@tonic-gate char *s1, *s2;
669*7c478bd9Sstevel@tonic-gate {
670*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,"acctcom: %s %s\n", s1, (s2 ? s2 : ""));
671*7c478bd9Sstevel@tonic-gate 	exit(1);
672*7c478bd9Sstevel@tonic-gate }
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate usage()
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage: acctcom [options] [files]\n");
677*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\nWhere options can be:\n");
678*7c478bd9Sstevel@tonic-gate 	diag("-b	read backwards through file");
679*7c478bd9Sstevel@tonic-gate 	diag("-f	print the fork/exec flag and exit status");
680*7c478bd9Sstevel@tonic-gate 	diag("-h	print hog factor (total-CPU-time/elapsed-time)");
681*7c478bd9Sstevel@tonic-gate 	diag("-i	print I/O counts");
682*7c478bd9Sstevel@tonic-gate 	diag("-k	show total Kcore minutes instead of memory size");
683*7c478bd9Sstevel@tonic-gate 	diag("-m	show mean memory size");
684*7c478bd9Sstevel@tonic-gate 	diag("-r	show CPU factor (user-time/(sys-time + user-time))");
685*7c478bd9Sstevel@tonic-gate 	diag("-t	show separate system and user CPU times");
686*7c478bd9Sstevel@tonic-gate 	diag("-v	don't print column headings");
687*7c478bd9Sstevel@tonic-gate 	diag("-a	print average statistics of selected commands");
688*7c478bd9Sstevel@tonic-gate 	diag("-q	print average statistics only");
689*7c478bd9Sstevel@tonic-gate 	diag("-l line	\tshow processes belonging to terminal /dev/line");
690*7c478bd9Sstevel@tonic-gate 	diag("-u user	\tshow processes belonging to user name or user ID");
691*7c478bd9Sstevel@tonic-gate 	diag("-u #	\tshow processes executed by super-user");
692*7c478bd9Sstevel@tonic-gate 	diag("-u ?	\tshow processes executed by unknown UID's");
693*7c478bd9Sstevel@tonic-gate 	diag("-g group	show processes belonging to group name of group ID");
694*7c478bd9Sstevel@tonic-gate 	diag("-s time	\tshow processes ending after time (hh[:mm[:ss]])");
695*7c478bd9Sstevel@tonic-gate 	diag("-e time	\tshow processes starting before time");
696*7c478bd9Sstevel@tonic-gate 	diag("-S time	\tshow processes starting after time");
697*7c478bd9Sstevel@tonic-gate 	diag("-E time	\tshow processes ending before time");
698*7c478bd9Sstevel@tonic-gate 	diag("-n regex	select commands matching the ed(1) regular expression");
699*7c478bd9Sstevel@tonic-gate 	diag("-o file	\tdo not print, put selected pacct records into file");
700*7c478bd9Sstevel@tonic-gate 	diag("-H factor	show processes that exceed hog factor");
701*7c478bd9Sstevel@tonic-gate 	diag("-O sec	\tshow processes that exceed CPU system time sec");
702*7c478bd9Sstevel@tonic-gate 	diag("-C sec	\tshow processes that exceed total CPU time sec");
703*7c478bd9Sstevel@tonic-gate 	diag("-I chars	show processes that transfer more than char chars");
704*7c478bd9Sstevel@tonic-gate }
705