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