122724Sdist /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
334047Sbostic  * Copyright (c) 1988 Regents of the University of California.
434047Sbostic  * All rights reserved.
534047Sbostic  *
642822Sbostic  * %sccs.include.redist.c%
734047Sbostic  *
834047Sbostic  */
922724Sdist 
1022724Sdist #ifndef lint
1122724Sdist char copyright[] =
1234047Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
1322724Sdist  All rights reserved.\n";
1434047Sbostic #endif /* not lint */
1522724Sdist 
1622724Sdist #ifndef lint
17*58152Seric static char sccsid[] = "@(#)mailstats.c	6.2 (Berkeley) 02/23/93";
1834047Sbostic #endif /* not lint */
194379Seric 
2034047Sbostic #include <sys/file.h>
2134047Sbostic #include <sendmail.h>
2234047Sbostic #include <mailstats.h>
23*58152Seric #include <pathnames.h>
244379Seric 
25*58152Seric #define MNAMELEN	20	/* max length of mailer name */
26*58152Seric 
274379Seric main(argc, argv)
2834047Sbostic 	int argc;
2934047Sbostic 	char **argv;
304379Seric {
3134047Sbostic 	extern char *optarg;
3234047Sbostic 	extern int optind;
334379Seric 	struct statistics stat;
344379Seric 	register int i;
35*58152Seric 	int mno;
3634047Sbostic 	int ch, fd;
37*58152Seric 	char *sfile;
38*58152Seric 	char *cfile;
39*58152Seric 	FILE *cfp;
40*58152Seric 	bool mnames;
41*58152Seric 	char mtable[MAXMAILERS][MNAMELEN+1];
42*58152Seric 	char sfilebuf[100];
43*58152Seric 	char buf[MAXLINE];
44*58152Seric 	extern char *ctime();
454379Seric 
46*58152Seric 	cfile = _PATH_SENDMAILCF;
47*58152Seric 	sfile = NULL;
48*58152Seric 	mnames = TRUE;
49*58152Seric 	while ((ch = getopt(argc, argv, "C:f:o")) != EOF)
50*58152Seric 	{
51*58152Seric 		switch (ch)
52*58152Seric 		{
53*58152Seric 		  case 'C':
54*58152Seric 			cfile = optarg;
55*58152Seric 			break;
56*58152Seric 
57*58152Seric 		  case 'f':
5834047Sbostic 			sfile = optarg;
5934047Sbostic 			break;
60*58152Seric 
61*58152Seric 		  case 'o':
62*58152Seric 			mnames = FALSE;
63*58152Seric 			break;
64*58152Seric 
65*58152Seric 		  case '?':
66*58152Seric 		  default:
67*58152Seric   usage:
68*58152Seric 			fputs("usage: mailstats [-C cffile] [-f stfile]\n", stderr);
6934047Sbostic 			exit(EX_USAGE);
7034047Sbostic 		}
71*58152Seric 	}
7234047Sbostic 	argc -= optind;
7334047Sbostic 	argv += optind;
7434047Sbostic 
75*58152Seric 	if (argc != 0)
76*58152Seric 		goto usage;
77*58152Seric 
78*58152Seric 	if ((cfp = fopen(cfile, "r")) == NULL)
79*58152Seric 	{
80*58152Seric 		fprintf(stderr, "mailstats: ");
81*58152Seric 		perror(cfile);
82*58152Seric 		exit(EX_NOINPUT);
83*58152Seric 	}
84*58152Seric 
85*58152Seric 	mno = 0;
86*58152Seric 	(void) strcpy(mtable[mno++], "prog");
87*58152Seric 	(void) strcpy(mtable[mno++], "*file*");
88*58152Seric 	(void) strcpy(mtable[mno++], "*include*");
89*58152Seric 
90*58152Seric 	while (fgets(buf, sizeof(buf), cfp) != NULL)
91*58152Seric 	{
92*58152Seric 		register char *b;
93*58152Seric 		char *s;
94*58152Seric 		register char *m;
95*58152Seric 
96*58152Seric 		b = buf;
97*58152Seric 		switch (*b++)
98*58152Seric 		{
99*58152Seric 		  case 'M':		/* mailer definition */
100*58152Seric 			break;
101*58152Seric 
102*58152Seric 		  case 'O':		/* option -- see if .st file */
103*58152Seric 			if (*b++ != 'S')
104*58152Seric 				continue;
105*58152Seric 
106*58152Seric 			/* yep -- save this */
107*58152Seric 			strcpy(sfilebuf, b);
108*58152Seric 			b = strchr(sfilebuf, '\n');
109*58152Seric 			if (b != NULL)
110*58152Seric 				*b = '\0';
111*58152Seric 			if (sfile == NULL)
112*58152Seric 				sfile = sfilebuf;
113*58152Seric 
114*58152Seric 		  default:
115*58152Seric 			continue;
116*58152Seric 		}
117*58152Seric 
118*58152Seric 		if (mno >= MAXMAILERS)
119*58152Seric 		{
120*58152Seric 			fprintf(stderr,
121*58152Seric 				"Too many mailers defined, %d max.\n",
122*58152Seric 				MAXMAILERS);
123*58152Seric 			exit(EX_SOFTWARE);
124*58152Seric 		}
125*58152Seric 		m = mtable[mno];
126*58152Seric 		s = m + MNAMELEN;		/* is [MNAMELEN+1] */
127*58152Seric 		while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
128*58152Seric 			*m++ = *b++;
129*58152Seric 		*m = '\0';
130*58152Seric 		for (i = 0; i < mno; i++)
131*58152Seric 		{
132*58152Seric 			if (strcmp(mtable[i], mtable[mno]) == 0)
133*58152Seric 				break;
134*58152Seric 		}
135*58152Seric 		if (i == mno)
136*58152Seric 			mno++;
137*58152Seric 	}
138*58152Seric 	(void) fclose(cfp);
139*58152Seric 	for (; mno < MAXMAILERS; mno++)
140*58152Seric 		mtable[mno][0]='\0';
141*58152Seric 
142*58152Seric 	if (sfile == NULL)
143*58152Seric 	{
144*58152Seric 		fprintf(stderr, "mailstats: no statistics file located\n");
145*58152Seric 		exit (EX_OSFILE);
146*58152Seric 	}
147*58152Seric 
14834047Sbostic 	if ((fd = open(sfile, O_RDONLY)) < 0) {
14934047Sbostic 		fputs("mailstats: ", stderr);
1504379Seric 		perror(sfile);
1514379Seric 		exit(EX_NOINPUT);
1524379Seric 	}
15334047Sbostic 	if (read(fd, &stat, sizeof(stat)) != sizeof(stat) ||
154*58152Seric 	    stat.stat_size != sizeof(stat))
155*58152Seric 	{
15634047Sbostic 		fputs("mailstats: file size changed.\n", stderr);
1574379Seric 		exit(EX_OSERR);
1584379Seric 	}
1594379Seric 
1604380Seric 	printf("Statistics from %s", ctime(&stat.stat_itime));
161*58152Seric 	printf(" M msgsfr bytes_from  msgsto   bytes_to%s\n",
162*58152Seric 		mnames ? "  Mailer" : "");
1634379Seric 	for (i = 0; i < MAXMAILERS; i++)
164*58152Seric 	{
16534047Sbostic 		if (stat.stat_nf[i] || stat.stat_nt[i])
166*58152Seric 		{
167*58152Seric 			printf("%2d %6ld %10ldK %6ld %10ldK", i,
16834047Sbostic 			    stat.stat_nf[i], stat.stat_bf[i],
16934047Sbostic 			    stat.stat_nt[i], stat.stat_bt[i]);
170*58152Seric 			if (mnames)
171*58152Seric 				printf("  %s", mtable[i]);
172*58152Seric 			printf("\n");
173*58152Seric 		}
174*58152Seric 	}
175*58152Seric 	exit(EX_OK);
1764379Seric }
177