xref: /original-bsd/usr.sbin/sendmail/src/stats.c (revision f1656be1)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  *  Sendmail
13  *  Copyright (c) 1983  Eric P. Allman
14  *  Berkeley, California
15  */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)stats.c	5.9 (Berkeley) 03/13/88";
19 #endif /* not lint */
20 
21 # include "sendmail.h"
22 # include "mailstats.h"
23 
24 struct statistics	Stat;
25 
26 #define ONE_K		1000		/* one thousand (twenty-four?) */
27 #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
28 /*
29 **  MARKSTATS -- mark statistics
30 */
31 
32 markstats(e, to)
33 	register ENVELOPE *e;
34 	register ADDRESS *to;
35 {
36 	if (to == NULL)
37 	{
38 		if (e->e_from.q_mailer != NULL)
39 		{
40 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
41 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
42 				KBYTES(CurEnv->e_msgsize);
43 		}
44 	}
45 	else
46 	{
47 		Stat.stat_nt[to->q_mailer->m_mno]++;
48 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
49 	}
50 }
51 /*
52 **  POSTSTATS -- post statistics in the statistics file
53 **
54 **	Parameters:
55 **		sfile -- the name of the statistics file.
56 **
57 **	Returns:
58 **		none.
59 **
60 **	Side Effects:
61 **		merges the Stat structure with the sfile file.
62 */
63 
64 poststats(sfile)
65 	char *sfile;
66 {
67 	register int fd;
68 	struct statistics stat;
69 	extern off_t lseek();
70 
71 	if (sfile == NULL)
72 		return;
73 
74 	(void) time(&Stat.stat_itime);
75 	Stat.stat_size = sizeof Stat;
76 
77 	fd = open(sfile, 2);
78 	if (fd < 0)
79 	{
80 		errno = 0;
81 		return;
82 	}
83 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
84 	    stat.stat_size == sizeof stat)
85 	{
86 		/* merge current statistics into statfile */
87 		register int i;
88 
89 		for (i = 0; i < MAXMAILERS; i++)
90 		{
91 			stat.stat_nf[i] += Stat.stat_nf[i];
92 			stat.stat_bf[i] += Stat.stat_bf[i];
93 			stat.stat_nt[i] += Stat.stat_nt[i];
94 			stat.stat_bt[i] += Stat.stat_bt[i];
95 		}
96 	}
97 	else
98 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
99 
100 	/* write out results */
101 	(void) lseek(fd, (off_t) 0, 0);
102 	(void) write(fd, (char *) &stat, sizeof stat);
103 	(void) close(fd);
104 }
105