set QDONTSEND together with QBADADDR
[unix-history] / usr / src / usr.sbin / sendmail / mailstats / mailstats.c
CommitLineData
aeb2545d 1/*
5e8b0e60 2 * Copyright (c) 1983 Eric P. Allman
502cf5c0
KB
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
417f7a11 6 * %sccs.include.redist.c%
502cf5c0 7 *
502cf5c0 8 */
aeb2545d
DF
9
10#ifndef lint
11char copyright[] =
502cf5c0 12"@(#) Copyright (c) 1988 Regents of the University of California.\n\
aeb2545d 13 All rights reserved.\n";
502cf5c0 14#endif /* not lint */
aeb2545d
DF
15
16#ifndef lint
42efaf55 17static char sccsid[] = "@(#)mailstats.c 6.2 (Berkeley) %G%";
502cf5c0 18#endif /* not lint */
9064e1ea 19
502cf5c0
KB
20#include <sys/file.h>
21#include <sendmail.h>
22#include <mailstats.h>
42efaf55
EA
23#include <pathnames.h>
24
25#define MNAMELEN 20 /* max length of mailer name */
9064e1ea
EA
26
27main(argc, argv)
502cf5c0
KB
28 int argc;
29 char **argv;
9064e1ea 30{
502cf5c0
KB
31 extern char *optarg;
32 extern int optind;
9064e1ea 33 struct statistics stat;
9064e1ea 34 register int i;
42efaf55 35 int mno;
502cf5c0 36 int ch, fd;
42efaf55
EA
37 char *sfile;
38 char *cfile;
39 FILE *cfp;
40 bool mnames;
41 char mtable[MAXMAILERS][MNAMELEN+1];
42 char sfilebuf[100];
43 char buf[MAXLINE];
44 extern char *ctime();
45
46 cfile = _PATH_SENDMAILCF;
47 sfile = NULL;
48 mnames = TRUE;
49 while ((ch = getopt(argc, argv, "C:f:o")) != EOF)
50 {
51 switch (ch)
52 {
53 case 'C':
54 cfile = optarg;
55 break;
9064e1ea 56
42efaf55 57 case 'f':
502cf5c0
KB
58 sfile = optarg;
59 break;
42efaf55
EA
60
61 case 'o':
62 mnames = FALSE;
63 break;
64
65 case '?':
66 default:
67 usage:
68 fputs("usage: mailstats [-C cffile] [-f stfile]\n", stderr);
502cf5c0
KB
69 exit(EX_USAGE);
70 }
42efaf55 71 }
502cf5c0
KB
72 argc -= optind;
73 argv += optind;
74
42efaf55
EA
75 if (argc != 0)
76 goto usage;
77
78 if ((cfp = fopen(cfile, "r")) == NULL)
79 {
80 fprintf(stderr, "mailstats: ");
81 perror(cfile);
82 exit(EX_NOINPUT);
83 }
84
85 mno = 0;
86 (void) strcpy(mtable[mno++], "prog");
87 (void) strcpy(mtable[mno++], "*file*");
88 (void) strcpy(mtable[mno++], "*include*");
89
90 while (fgets(buf, sizeof(buf), cfp) != NULL)
91 {
92 register char *b;
93 char *s;
94 register char *m;
95
96 b = buf;
97 switch (*b++)
98 {
99 case 'M': /* mailer definition */
100 break;
101
102 case 'O': /* option -- see if .st file */
103 if (*b++ != 'S')
104 continue;
105
106 /* yep -- save this */
107 strcpy(sfilebuf, b);
108 b = strchr(sfilebuf, '\n');
109 if (b != NULL)
110 *b = '\0';
111 if (sfile == NULL)
112 sfile = sfilebuf;
113
114 default:
115 continue;
116 }
117
118 if (mno >= MAXMAILERS)
119 {
120 fprintf(stderr,
121 "Too many mailers defined, %d max.\n",
122 MAXMAILERS);
123 exit(EX_SOFTWARE);
124 }
125 m = mtable[mno];
126 s = m + MNAMELEN; /* is [MNAMELEN+1] */
127 while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
128 *m++ = *b++;
129 *m = '\0';
130 for (i = 0; i < mno; i++)
131 {
132 if (strcmp(mtable[i], mtable[mno]) == 0)
133 break;
134 }
135 if (i == mno)
136 mno++;
137 }
138 (void) fclose(cfp);
139 for (; mno < MAXMAILERS; mno++)
140 mtable[mno][0]='\0';
141
142 if (sfile == NULL)
143 {
144 fprintf(stderr, "mailstats: no statistics file located\n");
145 exit (EX_OSFILE);
146 }
147
502cf5c0
KB
148 if ((fd = open(sfile, O_RDONLY)) < 0) {
149 fputs("mailstats: ", stderr);
9064e1ea
EA
150 perror(sfile);
151 exit(EX_NOINPUT);
152 }
502cf5c0 153 if (read(fd, &stat, sizeof(stat)) != sizeof(stat) ||
42efaf55
EA
154 stat.stat_size != sizeof(stat))
155 {
502cf5c0 156 fputs("mailstats: file size changed.\n", stderr);
9064e1ea
EA
157 exit(EX_OSERR);
158 }
159
387e90c7 160 printf("Statistics from %s", ctime(&stat.stat_itime));
42efaf55
EA
161 printf(" M msgsfr bytes_from msgsto bytes_to%s\n",
162 mnames ? " Mailer" : "");
9064e1ea 163 for (i = 0; i < MAXMAILERS; i++)
42efaf55 164 {
502cf5c0 165 if (stat.stat_nf[i] || stat.stat_nt[i])
42efaf55
EA
166 {
167 printf("%2d %6ld %10ldK %6ld %10ldK", i,
502cf5c0
KB
168 stat.stat_nf[i], stat.stat_bf[i],
169 stat.stat_nt[i], stat.stat_bt[i]);
42efaf55
EA
170 if (mnames)
171 printf(" %s", mtable[i]);
172 printf("\n");
173 }
174 }
175 exit(EX_OK);
9064e1ea 176}