xref: /illumos-gate/usr/src/cmd/acct/acctcom.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <time.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include "acctdef.h"
367c478bd9Sstevel@tonic-gate #include <grp.h>
377c478bd9Sstevel@tonic-gate #include <sys/acct.h>
387c478bd9Sstevel@tonic-gate #include <pwd.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <locale.h>
41414388d7Ssl #include <stdlib.h>
42414388d7Ssl #include <libgen.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct	acct ab;
457c478bd9Sstevel@tonic-gate char	command_name[16];
467c478bd9Sstevel@tonic-gate char	obuf[BUFSIZ];
477c478bd9Sstevel@tonic-gate static char	time_buf[50];
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate double	cpucut,
507c478bd9Sstevel@tonic-gate 	syscut,
517c478bd9Sstevel@tonic-gate 	hogcut,
527c478bd9Sstevel@tonic-gate 	iocut,
537c478bd9Sstevel@tonic-gate 	realtot,
547c478bd9Sstevel@tonic-gate 	cputot,
557c478bd9Sstevel@tonic-gate 	usertot,
567c478bd9Sstevel@tonic-gate 	systot,
577c478bd9Sstevel@tonic-gate 	kcoretot,
587c478bd9Sstevel@tonic-gate 	iotot,
597c478bd9Sstevel@tonic-gate 	rwtot;
607c478bd9Sstevel@tonic-gate extern long	timezone;
617c478bd9Sstevel@tonic-gate extern int	daylight;	/* daylight savings time if set */
627c478bd9Sstevel@tonic-gate long	daydiff,
637c478bd9Sstevel@tonic-gate 	offset = -2,
647c478bd9Sstevel@tonic-gate 	cmdcount;
657c478bd9Sstevel@tonic-gate ulong_t	elapsed,
667c478bd9Sstevel@tonic-gate 	sys,
677c478bd9Sstevel@tonic-gate 	user,
687c478bd9Sstevel@tonic-gate 	cpu,
697c478bd9Sstevel@tonic-gate 	io,
707c478bd9Sstevel@tonic-gate 	rw,
717c478bd9Sstevel@tonic-gate 	mem,
727c478bd9Sstevel@tonic-gate 	etime;
737c478bd9Sstevel@tonic-gate time_t	tstrt_b,
747c478bd9Sstevel@tonic-gate 	tstrt_a,
757c478bd9Sstevel@tonic-gate 	tend_b,
767c478bd9Sstevel@tonic-gate 	tend_a;
777c478bd9Sstevel@tonic-gate int	backward,
787c478bd9Sstevel@tonic-gate 	flag_field,
797c478bd9Sstevel@tonic-gate 	average,
807c478bd9Sstevel@tonic-gate 	quiet,
817c478bd9Sstevel@tonic-gate 	option,
827c478bd9Sstevel@tonic-gate 	verbose = 1,
837c478bd9Sstevel@tonic-gate 	uidflag,
847c478bd9Sstevel@tonic-gate 	gidflag,
857c478bd9Sstevel@tonic-gate 	unkid,	/*user doesn't have login on this machine*/
867c478bd9Sstevel@tonic-gate 	errflg,
877c478bd9Sstevel@tonic-gate 	su_user,
887c478bd9Sstevel@tonic-gate 	fileout = 0,
897c478bd9Sstevel@tonic-gate 	stdinflg,
907c478bd9Sstevel@tonic-gate 	nfiles;
917c478bd9Sstevel@tonic-gate static int	eflg = 0,
927c478bd9Sstevel@tonic-gate 	Eflg = 0,
937c478bd9Sstevel@tonic-gate 	sflg = 0,
947c478bd9Sstevel@tonic-gate 	Sflg = 0;
957c478bd9Sstevel@tonic-gate #ifdef uts
967c478bd9Sstevel@tonic-gate dev_t   linedev = 0xffff;  /* changed from -1, as dev_t is now ushort */
977c478bd9Sstevel@tonic-gate #else
987c478bd9Sstevel@tonic-gate dev_t	linedev = (dev_t)-1;
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate uid_t	uidval;
1017c478bd9Sstevel@tonic-gate gid_t	gidval;
1027c478bd9Sstevel@tonic-gate char	*cname = NULL; /* command name pattern to match*/
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate struct passwd *getpwnam(), *getpwuid(), *pw;
1057c478bd9Sstevel@tonic-gate struct group *getgrnam(),*grp;
1067c478bd9Sstevel@tonic-gate long	convtime();
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifdef uts
1097c478bd9Sstevel@tonic-gate float   expand();
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate ulong_t	expand();
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate char	*ofile,
1157c478bd9Sstevel@tonic-gate 	*devtolin(),
1167c478bd9Sstevel@tonic-gate 	*uidtonam();
1177c478bd9Sstevel@tonic-gate dev_t	lintodev();
118414388d7Ssl 
119414388d7Ssl void dofile(char *);
120414388d7Ssl void doexit(int) __NORETURN;
121414388d7Ssl void usage(void);
122414388d7Ssl void fatal(char *, char *);
123414388d7Ssl void println(void);
124414388d7Ssl void printhd(void);
125414388d7Ssl char *cmset(char *);
126414388d7Ssl 
1277c478bd9Sstevel@tonic-gate FILE	*ostrm;
1287c478bd9Sstevel@tonic-gate 
129414388d7Ssl int
main(int argc,char ** argv)130414388d7Ssl main(int argc, char **argv)
1317c478bd9Sstevel@tonic-gate {
132414388d7Ssl 	int	c;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
1357c478bd9Sstevel@tonic-gate 	setbuf(stdout,obuf);
1367c478bd9Sstevel@tonic-gate 	while((c = getopt(argc, argv,
1377c478bd9Sstevel@tonic-gate 		"C:E:H:I:O:S:abe:fg:hikl:mn:o:qrs:tu:v")) != EOF) {
1387c478bd9Sstevel@tonic-gate 		switch(c) {
1397c478bd9Sstevel@tonic-gate 		case 'C':
1407c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&cpucut);
1417c478bd9Sstevel@tonic-gate 			continue;
1427c478bd9Sstevel@tonic-gate 		case 'O':
1437c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&syscut);
1447c478bd9Sstevel@tonic-gate 			continue;
1457c478bd9Sstevel@tonic-gate 		case 'H':
1467c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&hogcut);
1477c478bd9Sstevel@tonic-gate 			continue;
1487c478bd9Sstevel@tonic-gate 		case 'I':
1497c478bd9Sstevel@tonic-gate 			sscanf(optarg,"%lf",&iocut);
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 		case 'a':
1527c478bd9Sstevel@tonic-gate 			average++;
1537c478bd9Sstevel@tonic-gate 			continue;
1547c478bd9Sstevel@tonic-gate 		case 'b':
1557c478bd9Sstevel@tonic-gate 			backward++;
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		case 'g':
1587c478bd9Sstevel@tonic-gate 			if(sscanf(optarg,"%ld",&gidval) == 1) {
1597c478bd9Sstevel@tonic-gate 				if (getgrgid(gidval) == NULL)
1607c478bd9Sstevel@tonic-gate 					fatal("Unknown group", optarg);
1617c478bd9Sstevel@tonic-gate 			} else if((grp=getgrnam(optarg)) == NULL)
1627c478bd9Sstevel@tonic-gate 				fatal("Unknown group", optarg);
1637c478bd9Sstevel@tonic-gate 			else
1647c478bd9Sstevel@tonic-gate 				gidval=grp->gr_gid;
1657c478bd9Sstevel@tonic-gate 			gidflag++;
1667c478bd9Sstevel@tonic-gate 			continue;
1677c478bd9Sstevel@tonic-gate 		case 'h':
1687c478bd9Sstevel@tonic-gate 			option |= HOGFACTOR;
1697c478bd9Sstevel@tonic-gate 			continue;
1707c478bd9Sstevel@tonic-gate 		case 'i':
1717c478bd9Sstevel@tonic-gate 			option |= IORW;
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 		case 'k':
1747c478bd9Sstevel@tonic-gate 			option |= KCOREMIN;
1757c478bd9Sstevel@tonic-gate 			continue;
1767c478bd9Sstevel@tonic-gate 		case 'm':
1777c478bd9Sstevel@tonic-gate 			option |= MEANSIZE;
1787c478bd9Sstevel@tonic-gate 			continue;
1797c478bd9Sstevel@tonic-gate 		case 'n':
180414388d7Ssl 			cname=cmset(optarg);
1817c478bd9Sstevel@tonic-gate 			continue;
1827c478bd9Sstevel@tonic-gate 		case 't':
1837c478bd9Sstevel@tonic-gate 			option |= SEPTIME;
1847c478bd9Sstevel@tonic-gate 			continue;
1857c478bd9Sstevel@tonic-gate 		case 'r':
1867c478bd9Sstevel@tonic-gate 			option |= CPUFACTOR;
1877c478bd9Sstevel@tonic-gate 			continue;
1887c478bd9Sstevel@tonic-gate 		case 'v':
1897c478bd9Sstevel@tonic-gate 			verbose=0;
1907c478bd9Sstevel@tonic-gate 			continue;
1917c478bd9Sstevel@tonic-gate 		case 'l':
1927c478bd9Sstevel@tonic-gate 			linedev = lintodev(optarg);
1937c478bd9Sstevel@tonic-gate 			continue;
1947c478bd9Sstevel@tonic-gate 		case 'u':
1957c478bd9Sstevel@tonic-gate 			if(*optarg == '?') {
1967c478bd9Sstevel@tonic-gate 				unkid++;
1977c478bd9Sstevel@tonic-gate 				continue;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 			if(*optarg == '#') {
2007c478bd9Sstevel@tonic-gate 				su_user++;
2017c478bd9Sstevel@tonic-gate 				uidval = 0;
2027c478bd9Sstevel@tonic-gate 				uidflag++;
2037c478bd9Sstevel@tonic-gate 				continue;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			if((pw = getpwnam(optarg)) == NULL) {
2067c478bd9Sstevel@tonic-gate 				uidval = (uid_t)atoi(optarg);
2077c478bd9Sstevel@tonic-gate 				/* atoi will return 0 in abnormal situation */
2087c478bd9Sstevel@tonic-gate 				if (uidval == 0 && strcmp(optarg, "0") != 0) {
2097c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%s: Unknown user %s\n", argv[0], optarg);
2107c478bd9Sstevel@tonic-gate 					exit(1);
2117c478bd9Sstevel@tonic-gate 				}
2127c478bd9Sstevel@tonic-gate  				if ((pw = getpwuid(uidval)) == NULL) {
2137c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%s: Unknown user %s\n", argv[0], optarg);
2147c478bd9Sstevel@tonic-gate 					exit(1);
2157c478bd9Sstevel@tonic-gate 				}
2167c478bd9Sstevel@tonic-gate 				uidflag++;
2177c478bd9Sstevel@tonic-gate 			} else {
2187c478bd9Sstevel@tonic-gate 				uidval = pw->pw_uid;
2197c478bd9Sstevel@tonic-gate 				uidflag++;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 		case 'q':
2237c478bd9Sstevel@tonic-gate 			quiet++;
2247c478bd9Sstevel@tonic-gate 			verbose=0;
2257c478bd9Sstevel@tonic-gate 			average++;
2267c478bd9Sstevel@tonic-gate 			continue;
2277c478bd9Sstevel@tonic-gate 		case 's':
2287c478bd9Sstevel@tonic-gate 			sflg = 1;
2297c478bd9Sstevel@tonic-gate 			tend_a = convtime(optarg);
2307c478bd9Sstevel@tonic-gate 			continue;
2317c478bd9Sstevel@tonic-gate 		case 'S':
2327c478bd9Sstevel@tonic-gate 			Sflg = 1;
2337c478bd9Sstevel@tonic-gate 			tstrt_a = convtime(optarg);
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 		case 'f':
2367c478bd9Sstevel@tonic-gate 			flag_field++;
2377c478bd9Sstevel@tonic-gate 			continue;
2387c478bd9Sstevel@tonic-gate 		case 'e':
2397c478bd9Sstevel@tonic-gate 			eflg = 1;
2407c478bd9Sstevel@tonic-gate 			tstrt_b = convtime(optarg);
2417c478bd9Sstevel@tonic-gate 			continue;
2427c478bd9Sstevel@tonic-gate 		case 'E':
2437c478bd9Sstevel@tonic-gate 			Eflg = 1;
2447c478bd9Sstevel@tonic-gate 			tend_b = convtime(optarg);
2457c478bd9Sstevel@tonic-gate 			continue;
2467c478bd9Sstevel@tonic-gate 		case 'o':
2477c478bd9Sstevel@tonic-gate 			ofile = optarg;
2487c478bd9Sstevel@tonic-gate 			fileout++;
2497c478bd9Sstevel@tonic-gate 			if((ostrm = fopen(ofile, "w")) == NULL) {
2507c478bd9Sstevel@tonic-gate 				perror("open error on output file");
2517c478bd9Sstevel@tonic-gate 				errflg++;
2527c478bd9Sstevel@tonic-gate 			}
2537c478bd9Sstevel@tonic-gate 			continue;
2547c478bd9Sstevel@tonic-gate 		case '?':
2557c478bd9Sstevel@tonic-gate 			errflg++;
2567c478bd9Sstevel@tonic-gate 			continue;
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if(errflg) {
2617c478bd9Sstevel@tonic-gate 		usage();
2627c478bd9Sstevel@tonic-gate 		exit(1);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
2677c478bd9Sstevel@tonic-gate 	while(optind++ < argc) {
2687c478bd9Sstevel@tonic-gate 		dofile(*argv++);    /* change from *argv */
2697c478bd9Sstevel@tonic-gate 		nfiles++;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if(nfiles==0) {
2737c478bd9Sstevel@tonic-gate 		if(isatty(0) || isdevnull())
2747c478bd9Sstevel@tonic-gate 			dofile(PACCT);
2757c478bd9Sstevel@tonic-gate 		else {
2767c478bd9Sstevel@tonic-gate 			stdinflg = 1;
2777c478bd9Sstevel@tonic-gate 			backward = offset = 0;
2787c478bd9Sstevel@tonic-gate 			dofile(NULL);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	doexit(0);
282414388d7Ssl 	/* NOTREACHED */
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
285414388d7Ssl void
dofile(char * fname)286414388d7Ssl dofile(char *fname)
2877c478bd9Sstevel@tonic-gate {
288414388d7Ssl 	struct acct *a = &ab;
2897c478bd9Sstevel@tonic-gate 	struct tm *t;
2907c478bd9Sstevel@tonic-gate 	time_t curtime;
2917c478bd9Sstevel@tonic-gate 	time_t	ts_a = 0,
2927c478bd9Sstevel@tonic-gate 		ts_b = 0,
2937c478bd9Sstevel@tonic-gate 		te_a = 0,
2947c478bd9Sstevel@tonic-gate 		te_b = 0;
2957c478bd9Sstevel@tonic-gate 	long	daystart;
2967c478bd9Sstevel@tonic-gate 	long	nsize;
2977c478bd9Sstevel@tonic-gate 	int	ver;	/* version of acct structure */
2987c478bd9Sstevel@tonic-gate 	int	dst_secs;	/* number of seconds to adjust
2997c478bd9Sstevel@tonic-gate 				   for daylight savings time */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if(fname != NULL)
3027c478bd9Sstevel@tonic-gate 		if(freopen(fname, "r", stdin) == NULL) {
3037c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctcom: cannot open %s\n", fname);
3047c478bd9Sstevel@tonic-gate 			return;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1)
3087c478bd9Sstevel@tonic-gate 		return;
3097c478bd9Sstevel@tonic-gate 	else if (ab.ac_flag & AEXPND)
3107c478bd9Sstevel@tonic-gate 		ver = 2;	/* 4.0 acct structure */
311*2a8bcb4eSToomas Soome 	else
3127c478bd9Sstevel@tonic-gate 		ver = 1;	/* 3.x acct structure */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	rewind(stdin);
315*2a8bcb4eSToomas Soome 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if(backward) {
3187c478bd9Sstevel@tonic-gate 		if (ver == 2)
3197c478bd9Sstevel@tonic-gate 			nsize = sizeof(struct acct);	/* make sure offset is signed */
3207c478bd9Sstevel@tonic-gate 		else
3217c478bd9Sstevel@tonic-gate 			nsize = sizeof(struct o_acct);	/* make sure offset is signed */
3227c478bd9Sstevel@tonic-gate 		fseek(stdin, (long)(-nsize), 2);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	tzset();
3257c478bd9Sstevel@tonic-gate 	daydiff = a->ac_btime - (a->ac_btime % SECSINDAY);
3267c478bd9Sstevel@tonic-gate 	time(&curtime);
3277c478bd9Sstevel@tonic-gate 	t = localtime(&curtime);
3287c478bd9Sstevel@tonic-gate 	if (daydiff < (curtime - (curtime % SECSINDAY))) {
3297c478bd9Sstevel@tonic-gate 		time_t t;
3307c478bd9Sstevel@tonic-gate 		/*
3317c478bd9Sstevel@tonic-gate 		 * it is older than today
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		t = (time_t)a->ac_btime;
3347c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &t);
3357c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\nACCOUNTING RECORDS FROM:  %s", time_buf);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* adjust time by one hour for daylight savings time */
3397c478bd9Sstevel@tonic-gate 	if (daylight && t->tm_isdst != 0)
3407c478bd9Sstevel@tonic-gate 		dst_secs = 3600;
3417c478bd9Sstevel@tonic-gate 	else
3427c478bd9Sstevel@tonic-gate 		dst_secs = 0;
343*2a8bcb4eSToomas Soome 	daystart = (a->ac_btime - timezone + dst_secs) -
3447c478bd9Sstevel@tonic-gate 	    ((a->ac_btime - timezone + dst_secs) % SECSINDAY);
3457c478bd9Sstevel@tonic-gate 	if (Sflg) {
3467c478bd9Sstevel@tonic-gate 		ts_a = tstrt_a + daystart - dst_secs;
3477c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &ts_a);
3487c478bd9Sstevel@tonic-gate 		fprintf(stdout, "START AFT: %s", time_buf);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	if (eflg) {
3517c478bd9Sstevel@tonic-gate 		ts_b = tstrt_b + daystart - dst_secs;
3527c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &ts_b);
3537c478bd9Sstevel@tonic-gate 		fprintf(stdout, "START BEF: %s", time_buf);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	if (sflg) {
3567c478bd9Sstevel@tonic-gate 		te_a = tend_a + daystart - dst_secs;
3577c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &te_a);
3587c478bd9Sstevel@tonic-gate 		fprintf(stdout, "END AFTER: %s", time_buf);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	if (Eflg) {
3617c478bd9Sstevel@tonic-gate 		te_b = tend_b + daystart - dst_secs;
3627c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &te_b);
3637c478bd9Sstevel@tonic-gate 		fprintf(stdout, "END BEFOR: %s", time_buf);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	if(ts_a) {
3667c478bd9Sstevel@tonic-gate 		if (te_b && ts_a > te_b) te_b += SECSINDAY;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	while(aread(ver) != 0) {
3707c478bd9Sstevel@tonic-gate 		elapsed = expand(a->ac_etime);
3717c478bd9Sstevel@tonic-gate 		etime = (ulong_t)a->ac_btime + (ulong_t)SECS(elapsed);
3727c478bd9Sstevel@tonic-gate 		if(ts_a || ts_b || te_a || te_b) {
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 			if(te_a && (etime < te_a)) {
3757c478bd9Sstevel@tonic-gate 				if(backward) return;
3767c478bd9Sstevel@tonic-gate 				else continue;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			if(te_b && (etime > te_b)) {
3797c478bd9Sstevel@tonic-gate 				if(backward) continue;
3807c478bd9Sstevel@tonic-gate 				else return;
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			if(ts_a && (a->ac_btime < ts_a))
3837c478bd9Sstevel@tonic-gate 				continue;
3847c478bd9Sstevel@tonic-gate 			if(ts_b && (a->ac_btime > ts_b))
3857c478bd9Sstevel@tonic-gate 				continue;
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		if(!MYKIND(a->ac_flag))
3887c478bd9Sstevel@tonic-gate 			continue;
3897c478bd9Sstevel@tonic-gate 		if(su_user && !SU(a->ac_flag))
3907c478bd9Sstevel@tonic-gate 			continue;
3917c478bd9Sstevel@tonic-gate 		sys = expand(a->ac_stime);
3927c478bd9Sstevel@tonic-gate 		user = expand(a->ac_utime);
3937c478bd9Sstevel@tonic-gate 		cpu = sys + user;
3947c478bd9Sstevel@tonic-gate 		if(cpu == 0)
3957c478bd9Sstevel@tonic-gate 			cpu = 1;
3967c478bd9Sstevel@tonic-gate 		mem = expand(a->ac_mem);
3977c478bd9Sstevel@tonic-gate 		(void) strncpy(command_name, a->ac_comm, 8);
3987c478bd9Sstevel@tonic-gate 		io=expand(a->ac_io);
3997c478bd9Sstevel@tonic-gate 		rw=expand(a->ac_rw);
4007c478bd9Sstevel@tonic-gate 		if(cpucut && cpucut >= SECS(cpu))
4017c478bd9Sstevel@tonic-gate 			continue;
4027c478bd9Sstevel@tonic-gate 		if(syscut && syscut >= SECS(sys))
4037c478bd9Sstevel@tonic-gate 			continue;
4047c478bd9Sstevel@tonic-gate #ifdef uts
4057c478bd9Sstevel@tonic-gate 		if(linedev != 0xffff && a->ac_tty != linedev)
4067c478bd9Sstevel@tonic-gate 			continue;
4077c478bd9Sstevel@tonic-gate #else
4087c478bd9Sstevel@tonic-gate 		if(linedev != (dev_t)-1 && a->ac_tty != linedev)
4097c478bd9Sstevel@tonic-gate 			continue;
4107c478bd9Sstevel@tonic-gate #endif
4117c478bd9Sstevel@tonic-gate 		if(uidflag && a->ac_uid != uidval)
4127c478bd9Sstevel@tonic-gate 			continue;
4137c478bd9Sstevel@tonic-gate 		if(gidflag && a->ac_gid != gidval)
4147c478bd9Sstevel@tonic-gate 			continue;
4157c478bd9Sstevel@tonic-gate 		if(cname && !cmatch(a->ac_comm,cname))
4167c478bd9Sstevel@tonic-gate 			continue;
4177c478bd9Sstevel@tonic-gate 		if(iocut && iocut > io)
4187c478bd9Sstevel@tonic-gate 			continue;
4197c478bd9Sstevel@tonic-gate 		if(unkid && uidtonam(a->ac_uid)[0] != '?')
4207c478bd9Sstevel@tonic-gate 			continue;
4217c478bd9Sstevel@tonic-gate 		if(verbose && (fileout == 0)) {
4227c478bd9Sstevel@tonic-gate 			printhd();
4237c478bd9Sstevel@tonic-gate 			verbose = 0;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 		if(elapsed == 0)
4267c478bd9Sstevel@tonic-gate 			elapsed++;
4277c478bd9Sstevel@tonic-gate 		if(hogcut && hogcut >= (double)cpu/(double)elapsed)
4287c478bd9Sstevel@tonic-gate 			continue;
4297c478bd9Sstevel@tonic-gate 		if(fileout)
4307c478bd9Sstevel@tonic-gate 			fwrite(&ab, sizeof(ab), 1, ostrm);
4317c478bd9Sstevel@tonic-gate 		else
4327c478bd9Sstevel@tonic-gate 			println();
4337c478bd9Sstevel@tonic-gate 		if(average) {
4347c478bd9Sstevel@tonic-gate 			cmdcount++;
4357c478bd9Sstevel@tonic-gate 			realtot += (double)elapsed;
4367c478bd9Sstevel@tonic-gate 			usertot += (double)user;
4377c478bd9Sstevel@tonic-gate 			systot += (double)sys;
4387c478bd9Sstevel@tonic-gate 			kcoretot += (double)mem;
4397c478bd9Sstevel@tonic-gate 			iotot += (double)io;
4407c478bd9Sstevel@tonic-gate 			rwtot += (double)rw;
4417c478bd9Sstevel@tonic-gate 		};
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
445414388d7Ssl int
aread(int ver)446414388d7Ssl aread(int ver)
4477c478bd9Sstevel@tonic-gate {
448414388d7Ssl 	static int ok = 1;
4497c478bd9Sstevel@tonic-gate 	struct o_acct oab;
4507c478bd9Sstevel@tonic-gate 	int ret;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (ver != 2) {
4537c478bd9Sstevel@tonic-gate 		if ((ret = fread((char *)&oab, sizeof(struct o_acct), 1, stdin)) == 1){
4547c478bd9Sstevel@tonic-gate 			/* copy SVR3 acct struct to SVR4 acct struct */
4557c478bd9Sstevel@tonic-gate 			ab.ac_flag = oab.ac_flag | AEXPND;
4567c478bd9Sstevel@tonic-gate 			ab.ac_stat = oab.ac_stat;
4577c478bd9Sstevel@tonic-gate 			ab.ac_uid = (uid_t) oab.ac_uid;
4587c478bd9Sstevel@tonic-gate 			ab.ac_gid = (gid_t) oab.ac_gid;
4597c478bd9Sstevel@tonic-gate 			ab.ac_tty = (dev_t) oab.ac_tty;
4607c478bd9Sstevel@tonic-gate 			ab.ac_btime = oab.ac_btime;
4617c478bd9Sstevel@tonic-gate 			ab.ac_utime = oab.ac_utime;
4627c478bd9Sstevel@tonic-gate 			ab.ac_stime = oab.ac_stime;
4637c478bd9Sstevel@tonic-gate 			ab.ac_mem = oab.ac_mem;
4647c478bd9Sstevel@tonic-gate 			ab.ac_io = oab.ac_io;
4657c478bd9Sstevel@tonic-gate 			ab.ac_rw = oab.ac_rw;
4667c478bd9Sstevel@tonic-gate 			strcpy(ab.ac_comm, oab.ac_comm);
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 	} else
4697c478bd9Sstevel@tonic-gate 		ret = fread((char *)&ab, sizeof(struct acct), 1, stdin);
470*2a8bcb4eSToomas Soome 
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if(backward) {
4737c478bd9Sstevel@tonic-gate 		if(ok) {
4747c478bd9Sstevel@tonic-gate 			if(fseek(stdin,
4757c478bd9Sstevel@tonic-gate 				(long)(offset*(ver == 2 ? sizeof(struct acct) :
4767c478bd9Sstevel@tonic-gate 					sizeof(struct o_acct))), 1) != 0) {
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 					rewind(stdin);	/* get 1st record */
4797c478bd9Sstevel@tonic-gate 					ok = 0;
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		} else
4827c478bd9Sstevel@tonic-gate 			ret = 0;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	return(ret != 1 ? 0 : 1);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
487414388d7Ssl void
printhd(void)488414388d7Ssl printhd(void)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	fprintf(stdout, "COMMAND                           START    END          REAL");
4917c478bd9Sstevel@tonic-gate 	ps("CPU");
4927c478bd9Sstevel@tonic-gate 	if(option & SEPTIME)
4937c478bd9Sstevel@tonic-gate 		ps("(SECS)");
4947c478bd9Sstevel@tonic-gate 	if(option & IORW){
4957c478bd9Sstevel@tonic-gate 		ps("CHARS");
4967c478bd9Sstevel@tonic-gate 		ps("BLOCKS");
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
4997c478bd9Sstevel@tonic-gate 		ps("CPU");
5007c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
5017c478bd9Sstevel@tonic-gate 		ps("HOG");
5027c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
5037c478bd9Sstevel@tonic-gate 		ps("MEAN");
5047c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
5057c478bd9Sstevel@tonic-gate 		ps("KCORE");
5067c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
5077c478bd9Sstevel@tonic-gate 	fprintf(stdout, "NAME       USER     TTYNAME       TIME     TIME       (SECS)");
5087c478bd9Sstevel@tonic-gate 	if(option & SEPTIME) {
5097c478bd9Sstevel@tonic-gate 		ps("SYS");
5107c478bd9Sstevel@tonic-gate 		ps("USER");
5117c478bd9Sstevel@tonic-gate 	} else
5127c478bd9Sstevel@tonic-gate 		ps("(SECS)");
5137c478bd9Sstevel@tonic-gate 	if(option & IORW) {
5147c478bd9Sstevel@tonic-gate 		ps("TRNSFD");
5157c478bd9Sstevel@tonic-gate 		ps("READ");
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
5187c478bd9Sstevel@tonic-gate 		ps("FACTOR");
5197c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
5207c478bd9Sstevel@tonic-gate 		ps("FACTOR");
5217c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
5227c478bd9Sstevel@tonic-gate 		ps("SIZE(K)");
5237c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
5247c478bd9Sstevel@tonic-gate 		ps("MIN");
5257c478bd9Sstevel@tonic-gate 	if(flag_field)
5267c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  F STAT");
5277c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
5287c478bd9Sstevel@tonic-gate 	fflush(stdout);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
531414388d7Ssl void
println(void)532414388d7Ssl println(void)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	char name[32];
535414388d7Ssl 	struct acct *a = &ab;
5367c478bd9Sstevel@tonic-gate 	time_t t;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if(quiet)
5397c478bd9Sstevel@tonic-gate 		return;
5407c478bd9Sstevel@tonic-gate 	if(!SU(a->ac_flag))
5417c478bd9Sstevel@tonic-gate 		strcpy(name,command_name);
5427c478bd9Sstevel@tonic-gate 	else {
5437c478bd9Sstevel@tonic-gate 		strcpy(name,"#");
5447c478bd9Sstevel@tonic-gate 		strcat(name,command_name);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%-*.*s", (OUTPUT_NSZ + 1),
5477c478bd9Sstevel@tonic-gate 	    (OUTPUT_NSZ + 1), name);
5487c478bd9Sstevel@tonic-gate 	strcpy(name,uidtonam(a->ac_uid));
5497c478bd9Sstevel@tonic-gate 	if(*name != '?')
5507c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %-*.*s", (OUTPUT_NSZ + 1),
5517c478bd9Sstevel@tonic-gate 		    (OUTPUT_NSZ + 1), name);
5527c478bd9Sstevel@tonic-gate 	else
5537c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %-9d",a->ac_uid);
5547c478bd9Sstevel@tonic-gate #ifdef uts
5557c478bd9Sstevel@tonic-gate 	fprintf(stdout, " %-*.*s", OUTPUT_LSZ, OUTPUT_LSZ,
5567c478bd9Sstevel@tonic-gate 	    a->ac_tty != 0xffff? devtolin(a->ac_tty):"?");
5577c478bd9Sstevel@tonic-gate #else
5587c478bd9Sstevel@tonic-gate 	fprintf(stdout, " %-*.*s", OUTPUT_LSZ, OUTPUT_LSZ,
5597c478bd9Sstevel@tonic-gate 	    a->ac_tty != (dev_t)-1? devtolin(a->ac_tty):"?");
5607c478bd9Sstevel@tonic-gate #endif
5617c478bd9Sstevel@tonic-gate 	t = a->ac_btime;
5627c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT1, &t);
5637c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%.9s", time_buf);
5647c478bd9Sstevel@tonic-gate 	cftime(time_buf, DATE_FMT1, (time_t *)&etime);
5657c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%.9s ", time_buf);
5667c478bd9Sstevel@tonic-gate 	pf((double)SECS(elapsed));
5677c478bd9Sstevel@tonic-gate 	if(option & SEPTIME) {
5687c478bd9Sstevel@tonic-gate 		pf((double)sys / HZ);
5697c478bd9Sstevel@tonic-gate 		pf((double)user / HZ);
5707c478bd9Sstevel@tonic-gate 	} else
5717c478bd9Sstevel@tonic-gate 		pf((double)cpu / HZ);
5727c478bd9Sstevel@tonic-gate 	if(option & IORW)
5737c478bd9Sstevel@tonic-gate 		fprintf(stdout, io < 100000000 ? "%8ld%8ld" : "%12ld%8ld",io,rw);
5747c478bd9Sstevel@tonic-gate 	if(option & CPUFACTOR)
5757c478bd9Sstevel@tonic-gate 		pf((double)user / cpu);
5767c478bd9Sstevel@tonic-gate 	if(option & HOGFACTOR)
5777c478bd9Sstevel@tonic-gate 		pf((double)cpu / elapsed);
5787c478bd9Sstevel@tonic-gate 	if(!option || (option & MEANSIZE))
5797c478bd9Sstevel@tonic-gate 		pf(KCORE(mem / cpu));
5807c478bd9Sstevel@tonic-gate 	if(option & KCOREMIN)
5817c478bd9Sstevel@tonic-gate 		pf(MINT(KCORE(mem)));
5827c478bd9Sstevel@tonic-gate 	if(flag_field)
5837c478bd9Sstevel@tonic-gate 		fprintf(stdout, "  %1o %3o", (unsigned char) a->ac_flag,
5847c478bd9Sstevel@tonic-gate 						(unsigned char) a->ac_stat);
5857c478bd9Sstevel@tonic-gate 	fprintf(stdout, "\n");
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * convtime converts time arg to internal value
5907c478bd9Sstevel@tonic-gate  * arg has form hr:min:sec, min or sec are assumed to be 0 if omitted
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate long
convtime(str)5937c478bd9Sstevel@tonic-gate convtime(str)
5947c478bd9Sstevel@tonic-gate char *str;
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	long	hr, min, sec;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	min = sec = 0;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if(sscanf(str, "%ld:%ld:%ld", &hr, &min, &sec) < 1) {
6017c478bd9Sstevel@tonic-gate 		fatal("acctcom: bad time:", str);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	tzset();
6047c478bd9Sstevel@tonic-gate 	sec += (min*60);
6057c478bd9Sstevel@tonic-gate 	sec += (hr*3600);
6067c478bd9Sstevel@tonic-gate 	return(sec + timezone);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
609414388d7Ssl int
cmatch(char * comm,char * cstr)610414388d7Ssl cmatch(char *comm, char *cstr)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	char	xcomm[9];
614414388d7Ssl 	int i;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	for(i=0;i<8;i++){
6177c478bd9Sstevel@tonic-gate 		if(comm[i]==' '||comm[i]=='\0')
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		xcomm[i] = comm[i];
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	xcomm[i] = '\0';
622*2a8bcb4eSToomas Soome 
623414388d7Ssl 	return (regex(cstr,xcomm) ? 1 : 0);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
626414388d7Ssl char *
cmset(char * pattern)627414388d7Ssl cmset(char *pattern)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if((pattern=(char *)regcmp(pattern,(char *)0))==NULL){
6317c478bd9Sstevel@tonic-gate 		fatal("pattern syntax", NULL);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
634414388d7Ssl 	return (pattern);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
637414388d7Ssl void
doexit(int status)638414388d7Ssl doexit(int status)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	if(!average)
6417c478bd9Sstevel@tonic-gate 		exit(status);
6427c478bd9Sstevel@tonic-gate 	if(cmdcount) {
6437c478bd9Sstevel@tonic-gate 		fprintf(stdout, "cmds=%ld ",cmdcount);
6447c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Real=%-6.2f ",SECS(realtot)/cmdcount);
6457c478bd9Sstevel@tonic-gate 		cputot = systot + usertot;
6467c478bd9Sstevel@tonic-gate 		fprintf(stdout, "CPU=%-6.2f ",SECS(cputot)/cmdcount);
6477c478bd9Sstevel@tonic-gate 		fprintf(stdout, "USER=%-6.2f ",SECS(usertot)/cmdcount);
6487c478bd9Sstevel@tonic-gate 		fprintf(stdout, "SYS=%-6.2f ",SECS(systot)/cmdcount);
6497c478bd9Sstevel@tonic-gate 		fprintf(stdout, "CHAR=%-8.2f ",iotot/cmdcount);
6507c478bd9Sstevel@tonic-gate 		fprintf(stdout, "BLK=%-8.2f ",rwtot/cmdcount);
6517c478bd9Sstevel@tonic-gate 		fprintf(stdout, "USR/TOT=%-4.2f ",usertot/cputot);
6527c478bd9Sstevel@tonic-gate 		fprintf(stdout, "HOG=%-4.2f ",cputot/realtot);
6537c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\n");
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	else
6567c478bd9Sstevel@tonic-gate 		fprintf(stdout, "\nNo commands matched\n");
6577c478bd9Sstevel@tonic-gate 	exit(status);
6587c478bd9Sstevel@tonic-gate }
659414388d7Ssl 
660414388d7Ssl int
isdevnull(void)661414388d7Ssl isdevnull(void)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	struct stat	filearg;
6647c478bd9Sstevel@tonic-gate 	struct stat	devnull;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if(fstat(0,&filearg) == -1) {
6677c478bd9Sstevel@tonic-gate 		fprintf(stderr,"acctcom: cannot stat stdin\n");
668414388d7Ssl 		return (0);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	if(stat("/dev/null",&devnull) == -1) {
6717c478bd9Sstevel@tonic-gate 		fprintf(stderr,"acctcom: cannot stat /dev/null\n");
672414388d7Ssl 		return (0);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
675414388d7Ssl 	if (filearg.st_rdev == devnull.st_rdev)
676414388d7Ssl 		return (1);
677414388d7Ssl 	else
678414388d7Ssl 		return (0);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
681414388d7Ssl void
fatal(char * s1,char * s2)682414388d7Ssl fatal(char *s1, char *s2)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	fprintf(stderr,"acctcom: %s %s\n", s1, (s2 ? s2 : ""));
6857c478bd9Sstevel@tonic-gate 	exit(1);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
688414388d7Ssl void
usage(void)689414388d7Ssl usage(void)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage: acctcom [options] [files]\n");
6927c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\nWhere options can be:\n");
6937c478bd9Sstevel@tonic-gate 	diag("-b	read backwards through file");
6947c478bd9Sstevel@tonic-gate 	diag("-f	print the fork/exec flag and exit status");
6957c478bd9Sstevel@tonic-gate 	diag("-h	print hog factor (total-CPU-time/elapsed-time)");
6967c478bd9Sstevel@tonic-gate 	diag("-i	print I/O counts");
6977c478bd9Sstevel@tonic-gate 	diag("-k	show total Kcore minutes instead of memory size");
6987c478bd9Sstevel@tonic-gate 	diag("-m	show mean memory size");
6997c478bd9Sstevel@tonic-gate 	diag("-r	show CPU factor (user-time/(sys-time + user-time))");
7007c478bd9Sstevel@tonic-gate 	diag("-t	show separate system and user CPU times");
7017c478bd9Sstevel@tonic-gate 	diag("-v	don't print column headings");
7027c478bd9Sstevel@tonic-gate 	diag("-a	print average statistics of selected commands");
7037c478bd9Sstevel@tonic-gate 	diag("-q	print average statistics only");
7047c478bd9Sstevel@tonic-gate 	diag("-l line	\tshow processes belonging to terminal /dev/line");
7057c478bd9Sstevel@tonic-gate 	diag("-u user	\tshow processes belonging to user name or user ID");
7067c478bd9Sstevel@tonic-gate 	diag("-u #	\tshow processes executed by super-user");
7077c478bd9Sstevel@tonic-gate 	diag("-u ?	\tshow processes executed by unknown UID's");
7087c478bd9Sstevel@tonic-gate 	diag("-g group	show processes belonging to group name of group ID");
7097c478bd9Sstevel@tonic-gate 	diag("-s time	\tshow processes ending after time (hh[:mm[:ss]])");
7107c478bd9Sstevel@tonic-gate 	diag("-e time	\tshow processes starting before time");
7117c478bd9Sstevel@tonic-gate 	diag("-S time	\tshow processes starting after time");
7127c478bd9Sstevel@tonic-gate 	diag("-E time	\tshow processes ending before time");
7137c478bd9Sstevel@tonic-gate 	diag("-n regex	select commands matching the ed(1) regular expression");
7147c478bd9Sstevel@tonic-gate 	diag("-o file	\tdo not print, put selected pacct records into file");
7157c478bd9Sstevel@tonic-gate 	diag("-H factor	show processes that exceed hog factor");
7167c478bd9Sstevel@tonic-gate 	diag("-O sec	\tshow processes that exceed CPU system time sec");
7177c478bd9Sstevel@tonic-gate 	diag("-C sec	\tshow processes that exceed total CPU time sec");
7187c478bd9Sstevel@tonic-gate 	diag("-I chars	show processes that transfer more than char chars");
7197c478bd9Sstevel@tonic-gate }
720