xref: /original-bsd/usr.sbin/sendmail/src/stats.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)stats.c	8.1 (Berkeley) 06/07/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include "mailstats.h"
15 
16 struct statistics	Stat;
17 
18 #define ONE_K		1000		/* one thousand (twenty-four?) */
19 #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
20 /*
21 **  MARKSTATS -- mark statistics
22 */
23 
24 markstats(e, to)
25 	register ENVELOPE *e;
26 	register ADDRESS *to;
27 {
28 	if (to == NULL)
29 	{
30 		if (e->e_from.q_mailer != NULL)
31 		{
32 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
33 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
34 				KBYTES(e->e_msgsize);
35 		}
36 	}
37 	else
38 	{
39 		Stat.stat_nt[to->q_mailer->m_mno]++;
40 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
41 	}
42 }
43 /*
44 **  POSTSTATS -- post statistics in the statistics file
45 **
46 **	Parameters:
47 **		sfile -- the name of the statistics file.
48 **
49 **	Returns:
50 **		none.
51 **
52 **	Side Effects:
53 **		merges the Stat structure with the sfile file.
54 */
55 
56 poststats(sfile)
57 	char *sfile;
58 {
59 	register int fd;
60 	struct statistics stat;
61 	extern off_t lseek();
62 
63 	if (sfile == NULL)
64 		return;
65 
66 	(void) time(&Stat.stat_itime);
67 	Stat.stat_size = sizeof Stat;
68 
69 	fd = open(sfile, 2);
70 	if (fd < 0)
71 	{
72 		errno = 0;
73 		return;
74 	}
75 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
76 	    stat.stat_size == sizeof stat)
77 	{
78 		/* merge current statistics into statfile */
79 		register int i;
80 
81 		for (i = 0; i < MAXMAILERS; i++)
82 		{
83 			stat.stat_nf[i] += Stat.stat_nf[i];
84 			stat.stat_bf[i] += Stat.stat_bf[i];
85 			stat.stat_nt[i] += Stat.stat_nt[i];
86 			stat.stat_bt[i] += Stat.stat_bt[i];
87 		}
88 	}
89 	else
90 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
91 
92 	/* write out results */
93 	(void) lseek(fd, (off_t) 0, 0);
94 	(void) write(fd, (char *) &stat, sizeof stat);
95 	(void) close(fd);
96 }
97