1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  *
8  */
9 
10 #ifndef lint
11 static char copyright[] =
12 "@(#) Copyright (c) 1988, 1993\n\
13 	The Regents of the University of California.  All rights reserved.\n";
14 #endif /* not lint */
15 
16 #ifndef lint
17 static char sccsid[] = "@(#)mailstats.c	8.3 (Berkeley) 12/27/93";
18 #endif /* not lint */
19 
20 #include <sendmail.h>
21 #include <mailstats.h>
22 #include <pathnames.h>
23 
24 #define MNAMELEN	20	/* max length of mailer name */
25 
26 main(argc, argv)
27 	int argc;
28 	char **argv;
29 {
30 	extern char *optarg;
31 	extern int optind;
32 	struct statistics stat;
33 	register int i;
34 	int mno;
35 	int ch, fd;
36 	char *sfile;
37 	char *cfile;
38 	FILE *cfp;
39 	bool mnames;
40 	long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0;
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;
56 
57 		  case 'f':
58 			sfile = optarg;
59 			break;
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);
69 			exit(EX_USAGE);
70 		}
71 	}
72 	argc -= optind;
73 	argv += optind;
74 
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 
148 	if ((fd = open(sfile, O_RDONLY)) < 0) {
149 		fputs("mailstats: ", stderr);
150 		perror(sfile);
151 		exit(EX_NOINPUT);
152 	}
153 	if (read(fd, &stat, sizeof(stat)) != sizeof(stat) ||
154 	    stat.stat_size != sizeof(stat))
155 	{
156 		fputs("mailstats: file size changed.\n", stderr);
157 		exit(EX_OSERR);
158 	}
159 
160 	printf("Statistics from %s", ctime(&stat.stat_itime));
161 	printf(" M msgsfr bytes_from  msgsto   bytes_to%s\n",
162 		mnames ? "  Mailer" : "");
163 	for (i = 0; i < MAXMAILERS; i++)
164 	{
165 		if (stat.stat_nf[i] || stat.stat_nt[i])
166 		{
167 			printf("%2d %6ld %10ldK %6ld %10ldK", i,
168 			    stat.stat_nf[i], stat.stat_bf[i],
169 			    stat.stat_nt[i], stat.stat_bt[i]);
170 			if (mnames)
171 				printf("  %s", mtable[i]);
172 			printf("\n");
173 			frmsgs += stat.stat_nf[i];
174 			frbytes += stat.stat_bf[i];
175 			tomsgs += stat.stat_nt[i];
176 			tobytes += stat.stat_bt[i];
177 		}
178 	}
179 	printf("========================================\n");
180 	printf(" T %6ld %10ldK %6ld %10ldK\n",
181 		frmsgs, frbytes, tomsgs, tobytes);
182 	exit(EX_OK);
183 }
184