This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / sendmail / mailstats / mailstats.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1983 Eric P. Allman
6f14531a
RG
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
15637ed4
RG
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36#ifndef lint
6f14531a
RG
37static char copyright[] =
38"@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
15637ed4
RG
40#endif /* not lint */
41
42#ifndef lint
d747e748 43static char sccsid[] = "@(#)mailstats.c 8.2 (Berkeley) 7/27/93";
15637ed4
RG
44#endif /* not lint */
45
15637ed4
RG
46#include <sendmail.h>
47#include <mailstats.h>
6f14531a
RG
48#include <pathnames.h>
49
50#define MNAMELEN 20 /* max length of mailer name */
15637ed4
RG
51
52main(argc, argv)
53 int argc;
54 char **argv;
55{
56 extern char *optarg;
57 extern int optind;
58 struct statistics stat;
59 register int i;
6f14531a 60 int mno;
15637ed4 61 int ch, fd;
6f14531a
RG
62 char *sfile;
63 char *cfile;
64 FILE *cfp;
65 bool mnames;
66 char mtable[MAXMAILERS][MNAMELEN+1];
67 char sfilebuf[100];
68 char buf[MAXLINE];
69 extern char *ctime();
70
71 cfile = _PATH_SENDMAILCF;
72 sfile = NULL;
73 mnames = TRUE;
74 while ((ch = getopt(argc, argv, "C:f:o")) != EOF)
75 {
76 switch (ch)
77 {
78 case 'C':
79 cfile = optarg;
80 break;
15637ed4 81
6f14531a 82 case 'f':
15637ed4
RG
83 sfile = optarg;
84 break;
6f14531a
RG
85
86 case 'o':
87 mnames = FALSE;
88 break;
89
90 case '?':
91 default:
92 usage:
93 fputs("usage: mailstats [-C cffile] [-f stfile]\n", stderr);
15637ed4
RG
94 exit(EX_USAGE);
95 }
6f14531a 96 }
15637ed4
RG
97 argc -= optind;
98 argv += optind;
99
6f14531a
RG
100 if (argc != 0)
101 goto usage;
102
103 if ((cfp = fopen(cfile, "r")) == NULL)
104 {
105 fprintf(stderr, "mailstats: ");
106 perror(cfile);
107 exit(EX_NOINPUT);
108 }
109
110 mno = 0;
111 (void) strcpy(mtable[mno++], "prog");
112 (void) strcpy(mtable[mno++], "*file*");
113 (void) strcpy(mtable[mno++], "*include*");
114
115 while (fgets(buf, sizeof(buf), cfp) != NULL)
116 {
117 register char *b;
118 char *s;
119 register char *m;
120
121 b = buf;
122 switch (*b++)
123 {
124 case 'M': /* mailer definition */
125 break;
126
127 case 'O': /* option -- see if .st file */
128 if (*b++ != 'S')
129 continue;
130
131 /* yep -- save this */
132 strcpy(sfilebuf, b);
133 b = strchr(sfilebuf, '\n');
134 if (b != NULL)
135 *b = '\0';
136 if (sfile == NULL)
137 sfile = sfilebuf;
138
139 default:
140 continue;
141 }
142
143 if (mno >= MAXMAILERS)
144 {
145 fprintf(stderr,
146 "Too many mailers defined, %d max.\n",
147 MAXMAILERS);
148 exit(EX_SOFTWARE);
149 }
150 m = mtable[mno];
151 s = m + MNAMELEN; /* is [MNAMELEN+1] */
152 while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
153 *m++ = *b++;
154 *m = '\0';
155 for (i = 0; i < mno; i++)
156 {
157 if (strcmp(mtable[i], mtable[mno]) == 0)
158 break;
159 }
160 if (i == mno)
161 mno++;
162 }
163 (void) fclose(cfp);
164 for (; mno < MAXMAILERS; mno++)
165 mtable[mno][0]='\0';
166
167 if (sfile == NULL)
168 {
169 fprintf(stderr, "mailstats: no statistics file located\n");
170 exit (EX_OSFILE);
171 }
172
15637ed4
RG
173 if ((fd = open(sfile, O_RDONLY)) < 0) {
174 fputs("mailstats: ", stderr);
175 perror(sfile);
176 exit(EX_NOINPUT);
177 }
178 if (read(fd, &stat, sizeof(stat)) != sizeof(stat) ||
6f14531a
RG
179 stat.stat_size != sizeof(stat))
180 {
15637ed4
RG
181 fputs("mailstats: file size changed.\n", stderr);
182 exit(EX_OSERR);
183 }
184
185 printf("Statistics from %s", ctime(&stat.stat_itime));
6f14531a
RG
186 printf(" M msgsfr bytes_from msgsto bytes_to%s\n",
187 mnames ? " Mailer" : "");
15637ed4 188 for (i = 0; i < MAXMAILERS; i++)
6f14531a 189 {
15637ed4 190 if (stat.stat_nf[i] || stat.stat_nt[i])
6f14531a
RG
191 {
192 printf("%2d %6ld %10ldK %6ld %10ldK", i,
15637ed4
RG
193 stat.stat_nf[i], stat.stat_bf[i],
194 stat.stat_nt[i], stat.stat_bt[i]);
6f14531a
RG
195 if (mnames)
196 printf(" %s", mtable[i]);
197 printf("\n");
198 }
199 }
200 exit(EX_OK);
15637ed4 201}