2 * Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
12 * $FreeBSD: src/contrib/sendmail/mailstats/mailstats.c,v 1.5.2.7 2002/09/03 01:50:05 gshapiro Exp $
19 "@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
20 All rights reserved.\n\
21 Copyright (c) 1988, 1993\n\
22 The Regents of the University of California. All rights reserved.\n")
24 SM_IDSTR(id, "@(#)$Id: mailstats.c,v 8.98 2002/05/24 23:10:15 gshapiro Exp $")
33 # undef EX_OK /* unistd.h may have another use for this */
37 #include <sm/errstring.h>
38 #include <sm/limits.h>
39 #include <sendmail/sendmail.h>
40 #include <sendmail/mailstats.h>
41 #include <sendmail/pathnames.h>
44 #define MNAMELEN 20 /* max length of mailer name */
61 long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0, rejmsgs = 0;
65 #endif /* _FFR_QUARANTINE */
67 char mtable[MAXMAILERS][MNAMELEN + 1];
68 char sfilebuf[MAXPATHLEN];
70 struct statistics stats;
75 cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
80 while ((ch = getopt(argc, argv, "cC:f:opP")) != -1)
85 cfile = getcfname(0, 0, SM_GET_SUBMIT_CF, NULL);
111 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
112 "usage: mailstats [-C cffile] [-c] [-P] [-f stfile] [-o] [-p]\n");
122 if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
126 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "mailstats: ");
133 (void) sm_strlcpy(mtable[mno++], "prog", MNAMELEN + 1);
134 (void) sm_strlcpy(mtable[mno++], "*file*", MNAMELEN + 1);
135 (void) sm_strlcpy(mtable[mno++], "*include*", MNAMELEN + 1);
137 while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
143 b = strchr(buf, '#');
145 b = strchr(buf, '\n');
147 b = &buf[strlen(buf)];
148 while (isascii(*--b) && isspace(*b))
155 case 'M': /* mailer definition */
158 case 'O': /* option -- see if .st file */
159 if (sm_strncasecmp(b, " StatusFile", 11) == 0 &&
160 !(isascii(b[11]) && isalnum(b[11])))
162 /* new form -- find value */
166 while (isascii(*++b) && isspace(*b))
169 else if (*b++ != 'S')
171 /* something else boring */
175 /* this is the S or StatusFile option -- save it */
176 if (sm_strlcpy(sfilebuf, b, sizeof sfilebuf) >=
179 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
180 "StatusFile filename too long: %.30s...\n",
191 if (mno >= MAXMAILERS)
193 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
194 "Too many mailers defined, %d max.\n",
199 s = m + MNAMELEN; /* is [MNAMELEN + 1] */
200 while (*b != ',' && !(isascii(*b) && isspace(*b)) &&
204 for (i = 0; i < mno; i++)
206 if (strcmp(mtable[i], mtable[mno]) == 0)
212 (void) sm_io_close(cfp, SM_TIME_DEFAULT);
213 for (; mno < MAXMAILERS; mno++)
214 mtable[mno][0] = '\0';
218 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
219 "mailstats: no statistics file located\n");
223 fd = open(sfile, O_RDONLY, 0600);
224 if ((fd < 0) || (i = read(fd, &stats, sizeof stats)) < 0)
227 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, "mailstats: ");
235 if ((i = read(fd, &stats, sizeof stats)) < 0)
238 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
246 memset((ARBPTR_T) &stats, '\0', sizeof stats);
247 (void) time(&stats.stat_itime);
252 if (stats.stat_magic != STAT_MAGIC)
254 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
255 "mailstats: incorrect magic number in %s\n",
259 else if (stats.stat_version != STAT_VERSION)
261 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
262 "mailstats version (%d) incompatible with %s version (%d)\n",
268 else if (i != sizeof stats || stats.stat_size != sizeof(stats))
270 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
271 "mailstats: file size changed.\n");
279 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%ld %ld\n",
280 (long) stats.stat_itime, (long) now);
284 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
285 "Statistics from %s",
286 ctime(&stats.stat_itime));
287 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
288 " M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis");
290 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " msgsqur");
291 #endif /* _FFR_QUARANTINE */
292 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s\n",
293 mnames ? " Mailer" : "");
295 for (i = 0; i < MAXMAILERS; i++)
297 if (stats.stat_nf[i] || stats.stat_nt[i] ||
300 #endif /* _FFR_QUARANTINE */
301 stats.stat_nr[i] || stats.stat_nd[i])
306 format = "%2d %8ld %10ld %8ld %10ld %6ld %6ld";
308 format = "%2d %8ld %10ldK %8ld %10ldK %6ld %6ld";
309 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
318 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
319 " %6ld", stats.stat_nq[i]);
320 #endif /* _FFR_QUARANTINE */
322 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
325 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
326 frmsgs += stats.stat_nf[i];
327 frbytes += stats.stat_bf[i];
328 tomsgs += stats.stat_nt[i];
329 tobytes += stats.stat_bt[i];
330 rejmsgs += stats.stat_nr[i];
331 dismsgs += stats.stat_nd[i];
333 quarmsgs += stats.stat_nq[i];
334 #endif /* _FFR_QUARANTINE */
339 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
340 " T %8ld %10ld %8ld %10ld %6ld %6ld",
341 frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
344 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
346 #endif /* _FFR_QUARANTINE */
347 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
348 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
349 " C %8ld %8ld %6ld\n",
350 stats.stat_cf, stats.stat_ct,
355 fd = open(sfile, O_RDWR | O_TRUNC, 0600);
362 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
363 "=============================================================");
365 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "========");
366 #endif /* _FFR_QUARANTINE */
367 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
368 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
369 " T %8ld %10ldK %8ld %10ldK %6ld %6ld",
370 frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
373 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
375 #endif /* _FFR_QUARANTINE */
376 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
377 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
378 " C %8ld %10s %8ld %10s %6ld\n",
379 stats.stat_cf, "", stats.stat_ct, "",