fixes in setproctitle to avoid problems with titles longer than
[unix-history] / usr / src / usr.sbin / sendmail / src / stats.c
CommitLineData
b2a81223
DF
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
650a0daf 12static char SccsId[] = "@(#)stats.c 5.6 (Berkeley) %G%";
b2a81223
DF
13#endif not lint
14
334bcb35
EA
15# include "sendmail.h"
16
334bcb35 17/*
7338e3d4
EA
18** Statistics structure.
19*/
20
21struct statistics
22{
23 time_t stat_itime; /* file initialization time */
24 short stat_size; /* size of this structure */
25 long stat_nf[MAXMAILERS]; /* # msgs from each mailer */
26 long stat_bf[MAXMAILERS]; /* kbytes from each mailer */
27 long stat_nt[MAXMAILERS]; /* # msgs to each mailer */
28 long stat_bt[MAXMAILERS]; /* kbytes to each mailer */
29};
30
31struct statistics Stat;
2e3062fe
EA
32
33#define ONE_K 1000 /* one thousand (twenty-four?) */
34#define KBYTES(x) (((x) + (ONE_K - 1)) / ONE_K)
7338e3d4
EA
35\f/*
36** MARKSTATS -- mark statistics
37*/
38
39markstats(e, to)
40 register ENVELOPE *e;
41 register ADDRESS *to;
42{
43 if (to == NULL)
44 {
650a0daf
EA
45
46 /*
47 ** If is possible to get mail from an unparseable address,
48 ** in this case, the q_mailer field is null, so that the
49 ** indirection below causes a dereference of a NULL pointer.
189a3471 50 */
650a0daf 51
189a3471
MAN
52 if (e->e_from.q_mailer != NULL )
53 {
54 Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
55 Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
650a0daf 56 KBYTES(CurEnv->e_msgsize);
189a3471 57 }
7338e3d4
EA
58 }
59 else
60 {
61 Stat.stat_nt[to->q_mailer->m_mno]++;
2e3062fe 62 Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
7338e3d4
EA
63 }
64}
65\f/*
334bcb35
EA
66** POSTSTATS -- post statistics in the statistics file
67**
68** Parameters:
69** sfile -- the name of the statistics file.
70**
71** Returns:
72** none.
73**
74** Side Effects:
75** merges the Stat structure with the sfile file.
76*/
77
334bcb35
EA
78poststats(sfile)
79 char *sfile;
80{
81 register int fd;
82 struct statistics stat;
17a67c62 83 extern off_t lseek();
334bcb35 84
666b39ad
EA
85 if (sfile == NULL)
86 return;
87
74c5fe7c 88 (void) time(&Stat.stat_itime);
334bcb35
EA
89 Stat.stat_size = sizeof Stat;
90
91 fd = open(sfile, 2);
92 if (fd < 0)
70faa7c8
EA
93 {
94 errno = 0;
334bcb35 95 return;
70faa7c8 96 }
74c5fe7c 97 if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
334bcb35
EA
98 stat.stat_size == sizeof stat)
99 {
100 /* merge current statistics into statfile */
101 register int i;
102
103 for (i = 0; i < MAXMAILERS; i++)
104 {
105 stat.stat_nf[i] += Stat.stat_nf[i];
106 stat.stat_bf[i] += Stat.stat_bf[i];
107 stat.stat_nt[i] += Stat.stat_nt[i];
108 stat.stat_bt[i] += Stat.stat_bt[i];
109 }
110 }
111 else
83f674d8 112 bcopy((char *) &Stat, (char *) &stat, sizeof stat);
334bcb35
EA
113
114 /* write out results */
17a67c62 115 (void) lseek(fd, (off_t) 0, 0);
74c5fe7c
EA
116 (void) write(fd, (char *) &stat, sizeof stat);
117 (void) close(fd);
334bcb35 118}