xref: /titanic_52/usr/src/cmd/acct/acctmerg.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 /*	Copyright (c) 1999 by Sun Microsystems, Inc. */
25 /*	All rights reserved. */
26 
27 
28 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
29 
30 /*
31  *	acctmerg [-a] [-i] [-p] [-t] [-u] [-v] [file...]
32  *	-a	output in tacct.h/ascii (instead of tacct.h)
33  *	-i	input is in tacct.h/ascii (instead of tacct.h)
34  *	-p	print input files with no processing
35  *	-t	output single record that totals all input
36  *	-u	summarize by uid, rather than uid/name
37  *	-v	output in verbose tacct.h/ascii
38  *	reads std input and 0-NFILE files, all in tacct.h format,
39  *	sorted by uid/name.
40  *	merge/adds all records with same uid/name (or same uid if -u,
41  *	or all records if -t], writes to std. output
42  *	(still in tacct.h format)
43  *	note that this can be used to summarize the std input
44  */
45 
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include "acctdef.h"
50 
51 int	nfile;			/* index of last used in fl */
52 FILE	*fl[NFILE]	= {stdin};
53 
54 struct	tacct tb[NFILE];	/* current record from each file */
55 struct	tacct	tt = {
56 	0,
57 	"TOTAL"
58 };
59 int	asciiout;
60 int	asciiinp;
61 int	printonly;
62 int	totalonly;
63 int	uidsum;
64 int	verbose;
65 struct tacct	*getleast();
66 
67 int 	exitcode = 0;
68 
69 main(argc, argv)
70 int argc;
71 char **argv;
72 {
73 	register i;
74 	register struct tacct *tp;
75 
76 	while (--argc > 0) {
77 		if (**++argv == '-')
78 			switch (*++*argv) {
79 			case 'a':
80 				asciiout++;
81 				continue;
82 			case 'i':
83 				asciiinp++;
84 				continue;
85 			case 'p':
86 				printonly++;
87 				continue;
88 			case 't':
89 				totalonly++;
90 				continue;
91 			case 'u':
92 				uidsum++;
93 				continue;
94 			case 'v':
95 				verbose++;
96 				asciiout++;
97 				continue;
98 			}
99 		else {
100 			if (++nfile >= NFILE) {
101 				fprintf(stderr, "acctmerg: >%d files\n", NFILE);
102 				exit(1);
103 			}
104 			if ((fl[nfile] = fopen(*argv, "r")) == NULL) {
105 				fprintf(stderr, "acctmerg: can't open %s\n", *argv);
106 				exitcode = 1;
107 				/*	exit(1); 	*/
108 			}
109 		}
110 	}
111 
112 	if (printonly) {
113 		for (i = 0; i <= nfile; i++)
114 			while (getnext(i))
115 				prtacct(&tb[i]);
116 		exit(exitcode);
117 	}
118 
119 	for (i = 0; i <= nfile; i++)
120 		if(getnext(i) == 0) {
121 			fprintf(stderr,"acctmerg: read error file %d.  File may be empty.\n", i);
122 			exitcode = 2;
123 
124 		}
125 
126 	while ((tp = getleast()) != NULL)	/* get least uid of all files, */
127 		sumcurr(tp);			/* sum all entries for that uid, */
128 	if (totalonly)				/* and write the 'summed' record */
129 		output(&tt);
130 
131 	exit(exitcode);
132 }
133 
134 /*
135  *	getleast returns ptr to least (lowest uid)  element of current
136  *	avail, NULL if none left; always returns 1st of equals
137  */
138 struct tacct *getleast()
139 {
140 	register struct tacct *tp, *least;
141 
142 	least = NULL;
143 	for (tp = tb; tp <= &tb[nfile]; tp++) {
144 		if (tp->ta_name[0] == '\0')
145 			continue;
146 		if (least == NULL ||
147 			tp->ta_uid < least->ta_uid ||
148 			((tp->ta_uid == least->ta_uid) &&
149 			!uidsum &&
150 			(strncmp(tp->ta_name, least->ta_name, NSZ) < 0)))
151 			least = tp;
152 	}
153 	return(least);
154 }
155 
156 /*
157  *	sumcurr sums all entries with same uid/name (into tp->tacct record)
158  *	writes it out, gets new entry
159  */
160 sumcurr(tp)
161 register struct tacct *tp;
162 {
163 	struct tacct tc;
164 	char *memcpy();
165 
166 	memcpy(&tc, tp, sizeof(struct tacct));
167 	tacctadd(&tt, tp);	/* gets total of all uids */
168 	getnext(tp-&tb[0]);	/* get next one in same file */
169 	while (tp <= &tb[nfile])
170 		if (tp->ta_name[0] != '\0' &&
171 			tp->ta_uid == tc.ta_uid &&
172 			(uidsum || EQN(tp->ta_name, tc.ta_name))) {
173 			tacctadd(&tc, tp);
174 			tacctadd(&tt, tp);
175 			getnext(tp-&tb[0]);
176 		} else
177 			tp++;	/* look at next file */
178 	if (!totalonly)
179 		output(&tc);
180 }
181 
182 tacctadd(t1, t2)
183 register struct tacct *t1, *t2;
184 {
185 	t1->ta_cpu[0] = t1->ta_cpu[0] + t2->ta_cpu[0];
186 	t1->ta_cpu[1] = t1->ta_cpu[1] + t2->ta_cpu[1];
187 	t1->ta_kcore[0] = t1->ta_kcore[0] + t2->ta_kcore[0];
188 	t1->ta_kcore[1] = t1->ta_kcore[1] + t2->ta_kcore[1];
189 	t1->ta_con[0] = t1->ta_con[0] + t2->ta_con[0];
190 	t1->ta_con[1] = t1->ta_con[1] + t2->ta_con[1];
191 	t1->ta_du = t1->ta_du + t2->ta_du;
192 	t1->ta_pc += t2->ta_pc;
193 	t1->ta_sc += t2->ta_sc;
194 	t1->ta_dc += t2->ta_dc;
195 	t1->ta_fee += t2->ta_fee;
196 }
197 
198 output(tp)
199 register struct tacct *tp;
200 {
201 	if (asciiout)
202 		prtacct(tp);
203 	else
204 		fwrite(tp, sizeof(*tp), 1, stdout);
205 }
206 /*
207  *	getnext reads next record from stream i, returns 1 if one existed
208  */
209 getnext(i)
210 register i;
211 {
212 	register struct tacct *tp;
213 
214 	tp = &tb[i];
215 	tp->ta_name[0] = '\0';
216 	if (fl[i] == NULL)
217 		return(0);
218 	if (asciiinp) {
219 		if (fscanf(fl[i],
220 			"%ld\t%s\t%e %e %e %e %e %e %e %lu\t%hu\t%hu\t%hu",
221 			&tp->ta_uid,
222 			tp->ta_name,
223 			&tp->ta_cpu[0], &tp->ta_cpu[1],
224 			&tp->ta_kcore[0], &tp->ta_kcore[1],
225 			&tp->ta_con[0], &tp->ta_con[1],
226 			&tp->ta_du,
227 			&tp->ta_pc,
228 			&tp->ta_sc,
229 			&tp->ta_dc,
230 			&tp->ta_fee) != EOF)
231 			return(1);
232 	} else {
233 		if (fread(tp, sizeof(*tp), 1, fl[i]) == 1)
234 			return(1);
235 	}
236 	fclose(fl[i]);
237 	fl[i] = NULL;
238 	return(0);
239 }
240 
241 char fmt[] = "%ld\t%.*s\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%lu\t%hu\t%hu\t%hu\n";
242 char fmtv[] = "%ld\t%.*s\t%e %e %e %e %e %e %e %lu %hu\t%hu\t%hu\n";
243 
244 prtacct(tp)
245 register struct tacct *tp;
246 {
247 	printf(verbose ? fmtv : fmt,
248 	    tp->ta_uid,
249 	    OUTPUT_NSZ,
250 	    tp->ta_name,
251 	    tp->ta_cpu[0], tp->ta_cpu[1],
252 	    tp->ta_kcore[0], tp->ta_kcore[1],
253 	    tp->ta_con[0], tp->ta_con[1],
254 	    tp->ta_du,
255 	    tp->ta_pc,
256 	    tp->ta_sc,
257 	    tp->ta_dc,
258 	    tp->ta_fee);
259 }
260