xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 27681)
122714Sdist /*
222714Sdist **  Sendmail
322714Sdist **  Copyright (c) 1983  Eric P. Allman
422714Sdist **  Berkeley, California
522714Sdist **
622714Sdist **  Copyright (c) 1983 Regents of the University of California.
722714Sdist **  All rights reserved.  The Berkeley software License Agreement
822714Sdist **  specifies the terms and conditions for redistribution.
922714Sdist */
1022714Sdist 
1122714Sdist #ifndef lint
12*27681Sbloom static char	SccsId[] = "@(#)stats.c	5.8 (Berkeley) 05/02/86";
1322714Sdist #endif not lint
1422714Sdist 
154286Seric # include "sendmail.h"
16*27681Sbloom # include "mailstats.h"
174286Seric 
189383Seric struct statistics	Stat;
1924944Seric 
2024944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
2124944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
229383Seric /*
239383Seric **  MARKSTATS -- mark statistics
249383Seric */
259383Seric 
269383Seric markstats(e, to)
279383Seric 	register ENVELOPE *e;
289383Seric 	register ADDRESS *to;
299383Seric {
309383Seric 	if (to == NULL)
319383Seric 	{
3225689Seric 		if (e->e_from.q_mailer != NULL)
3324945Seric 		{
3424945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
3524945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
3624945Seric 				KBYTES(CurEnv->e_msgsize);
3724945Seric 		}
389383Seric 	}
399383Seric 	else
409383Seric 	{
419383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
4224944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
439383Seric 	}
449383Seric }
459383Seric /*
464286Seric **  POSTSTATS -- post statistics in the statistics file
474286Seric **
484286Seric **	Parameters:
494286Seric **		sfile -- the name of the statistics file.
504286Seric **
514286Seric **	Returns:
524286Seric **		none.
534286Seric **
544286Seric **	Side Effects:
554286Seric **		merges the Stat structure with the sfile file.
564286Seric */
574286Seric 
584286Seric poststats(sfile)
594286Seric 	char *sfile;
604286Seric {
614286Seric 	register int fd;
624286Seric 	struct statistics stat;
6323123Seric 	extern off_t lseek();
644286Seric 
6523516Seric 	if (sfile == NULL)
6623516Seric 		return;
6723516Seric 
684319Seric 	(void) time(&Stat.stat_itime);
694286Seric 	Stat.stat_size = sizeof Stat;
704286Seric 
714286Seric 	fd = open(sfile, 2);
724286Seric 	if (fd < 0)
7311937Seric 	{
7411937Seric 		errno = 0;
754286Seric 		return;
7611937Seric 	}
774319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
784286Seric 	    stat.stat_size == sizeof stat)
794286Seric 	{
804286Seric 		/* merge current statistics into statfile */
814286Seric 		register int i;
824286Seric 
834286Seric 		for (i = 0; i < MAXMAILERS; i++)
844286Seric 		{
854286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
864286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
874286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
884286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
894286Seric 		}
904286Seric 	}
914286Seric 	else
9216887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
934286Seric 
944286Seric 	/* write out results */
9523123Seric 	(void) lseek(fd, (off_t) 0, 0);
964319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
974319Seric 	(void) close(fd);
984286Seric }
99