Commit | Line | Data |
---|---|---|
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 |
37 | static 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 | |
6f14531a | 43 | static char sccsid[] = "@(#)mailstats.c 8.1 (Berkeley) 6/7/93"; |
15637ed4 RG |
44 | #endif /* not lint */ |
45 | ||
46 | #include <sys/file.h> | |
47 | #include <sendmail.h> | |
48 | #include <mailstats.h> | |
6f14531a RG |
49 | #include <pathnames.h> |
50 | ||
51 | #define MNAMELEN 20 /* max length of mailer name */ | |
15637ed4 RG |
52 | |
53 | main(argc, argv) | |
54 | int argc; | |
55 | char **argv; | |
56 | { | |
57 | extern char *optarg; | |
58 | extern int optind; | |
59 | struct statistics stat; | |
60 | register int i; | |
6f14531a | 61 | int mno; |
15637ed4 | 62 | int ch, fd; |
6f14531a RG |
63 | char *sfile; |
64 | char *cfile; | |
65 | FILE *cfp; | |
66 | bool mnames; | |
67 | char mtable[MAXMAILERS][MNAMELEN+1]; | |
68 | char sfilebuf[100]; | |
69 | char buf[MAXLINE]; | |
70 | extern char *ctime(); | |
71 | ||
72 | cfile = _PATH_SENDMAILCF; | |
73 | sfile = NULL; | |
74 | mnames = TRUE; | |
75 | while ((ch = getopt(argc, argv, "C:f:o")) != EOF) | |
76 | { | |
77 | switch (ch) | |
78 | { | |
79 | case 'C': | |
80 | cfile = optarg; | |
81 | break; | |
15637ed4 | 82 | |
6f14531a | 83 | case 'f': |
15637ed4 RG |
84 | sfile = optarg; |
85 | break; | |
6f14531a RG |
86 | |
87 | case 'o': | |
88 | mnames = FALSE; | |
89 | break; | |
90 | ||
91 | case '?': | |
92 | default: | |
93 | usage: | |
94 | fputs("usage: mailstats [-C cffile] [-f stfile]\n", stderr); | |
15637ed4 RG |
95 | exit(EX_USAGE); |
96 | } | |
6f14531a | 97 | } |
15637ed4 RG |
98 | argc -= optind; |
99 | argv += optind; | |
100 | ||
6f14531a RG |
101 | if (argc != 0) |
102 | goto usage; | |
103 | ||
104 | if ((cfp = fopen(cfile, "r")) == NULL) | |
105 | { | |
106 | fprintf(stderr, "mailstats: "); | |
107 | perror(cfile); | |
108 | exit(EX_NOINPUT); | |
109 | } | |
110 | ||
111 | mno = 0; | |
112 | (void) strcpy(mtable[mno++], "prog"); | |
113 | (void) strcpy(mtable[mno++], "*file*"); | |
114 | (void) strcpy(mtable[mno++], "*include*"); | |
115 | ||
116 | while (fgets(buf, sizeof(buf), cfp) != NULL) | |
117 | { | |
118 | register char *b; | |
119 | char *s; | |
120 | register char *m; | |
121 | ||
122 | b = buf; | |
123 | switch (*b++) | |
124 | { | |
125 | case 'M': /* mailer definition */ | |
126 | break; | |
127 | ||
128 | case 'O': /* option -- see if .st file */ | |
129 | if (*b++ != 'S') | |
130 | continue; | |
131 | ||
132 | /* yep -- save this */ | |
133 | strcpy(sfilebuf, b); | |
134 | b = strchr(sfilebuf, '\n'); | |
135 | if (b != NULL) | |
136 | *b = '\0'; | |
137 | if (sfile == NULL) | |
138 | sfile = sfilebuf; | |
139 | ||
140 | default: | |
141 | continue; | |
142 | } | |
143 | ||
144 | if (mno >= MAXMAILERS) | |
145 | { | |
146 | fprintf(stderr, | |
147 | "Too many mailers defined, %d max.\n", | |
148 | MAXMAILERS); | |
149 | exit(EX_SOFTWARE); | |
150 | } | |
151 | m = mtable[mno]; | |
152 | s = m + MNAMELEN; /* is [MNAMELEN+1] */ | |
153 | while (*b != ',' && !isspace(*b) && *b != '\0' && m < s) | |
154 | *m++ = *b++; | |
155 | *m = '\0'; | |
156 | for (i = 0; i < mno; i++) | |
157 | { | |
158 | if (strcmp(mtable[i], mtable[mno]) == 0) | |
159 | break; | |
160 | } | |
161 | if (i == mno) | |
162 | mno++; | |
163 | } | |
164 | (void) fclose(cfp); | |
165 | for (; mno < MAXMAILERS; mno++) | |
166 | mtable[mno][0]='\0'; | |
167 | ||
168 | if (sfile == NULL) | |
169 | { | |
170 | fprintf(stderr, "mailstats: no statistics file located\n"); | |
171 | exit (EX_OSFILE); | |
172 | } | |
173 | ||
15637ed4 RG |
174 | if ((fd = open(sfile, O_RDONLY)) < 0) { |
175 | fputs("mailstats: ", stderr); | |
176 | perror(sfile); | |
177 | exit(EX_NOINPUT); | |
178 | } | |
179 | if (read(fd, &stat, sizeof(stat)) != sizeof(stat) || | |
6f14531a RG |
180 | stat.stat_size != sizeof(stat)) |
181 | { | |
15637ed4 RG |
182 | fputs("mailstats: file size changed.\n", stderr); |
183 | exit(EX_OSERR); | |
184 | } | |
185 | ||
186 | printf("Statistics from %s", ctime(&stat.stat_itime)); | |
6f14531a RG |
187 | printf(" M msgsfr bytes_from msgsto bytes_to%s\n", |
188 | mnames ? " Mailer" : ""); | |
15637ed4 | 189 | for (i = 0; i < MAXMAILERS; i++) |
6f14531a | 190 | { |
15637ed4 | 191 | if (stat.stat_nf[i] || stat.stat_nt[i]) |
6f14531a RG |
192 | { |
193 | printf("%2d %6ld %10ldK %6ld %10ldK", i, | |
15637ed4 RG |
194 | stat.stat_nf[i], stat.stat_bf[i], |
195 | stat.stat_nt[i], stat.stat_bt[i]); | |
6f14531a RG |
196 | if (mnames) |
197 | printf(" %s", mtable[i]); | |
198 | printf("\n"); | |
199 | } | |
200 | } | |
201 | exit(EX_OK); | |
15637ed4 | 202 | } |