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