122724Sdist /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
362512Sbostic  * Copyright (c) 1988, 1993
462512Sbostic  *	The Regents of the University of California.  All rights reserved.
534047Sbostic  *
642822Sbostic  * %sccs.include.redist.c%
734047Sbostic  *
834047Sbostic  */
922724Sdist 
1022724Sdist #ifndef lint
1162512Sbostic static char copyright[] =
1262512Sbostic "@(#) Copyright (c) 1988, 1993\n\
1362512Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1434047Sbostic #endif /* not lint */
1522724Sdist 
1622724Sdist #ifndef lint
17*67673Seric static char sccsid[] = "@(#)mailstats.c	8.4 (Berkeley) 08/14/94";
1834047Sbostic #endif /* not lint */
194379Seric 
2034047Sbostic #include <sendmail.h>
2134047Sbostic #include <mailstats.h>
2258152Seric #include <pathnames.h>
234379Seric 
2458152Seric #define MNAMELEN	20	/* max length of mailer name */
2558152Seric 
main(argc,argv)264379Seric main(argc, argv)
2734047Sbostic 	int argc;
2834047Sbostic 	char **argv;
294379Seric {
3034047Sbostic 	extern char *optarg;
3134047Sbostic 	extern int optind;
324379Seric 	struct statistics stat;
334379Seric 	register int i;
3458152Seric 	int mno;
3534047Sbostic 	int ch, fd;
3658152Seric 	char *sfile;
3758152Seric 	char *cfile;
3858152Seric 	FILE *cfp;
3958152Seric 	bool mnames;
4065221Seric 	long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0;
4158152Seric 	char mtable[MAXMAILERS][MNAMELEN+1];
4258152Seric 	char sfilebuf[100];
4358152Seric 	char buf[MAXLINE];
4458152Seric 	extern char *ctime();
454379Seric 
4658152Seric 	cfile = _PATH_SENDMAILCF;
4758152Seric 	sfile = NULL;
4858152Seric 	mnames = TRUE;
4958152Seric 	while ((ch = getopt(argc, argv, "C:f:o")) != EOF)
5058152Seric 	{
5158152Seric 		switch (ch)
5258152Seric 		{
5358152Seric 		  case 'C':
5458152Seric 			cfile = optarg;
5558152Seric 			break;
5658152Seric 
5758152Seric 		  case 'f':
5834047Sbostic 			sfile = optarg;
5934047Sbostic 			break;
6058152Seric 
6158152Seric 		  case 'o':
6258152Seric 			mnames = FALSE;
6358152Seric 			break;
6458152Seric 
6558152Seric 		  case '?':
6658152Seric 		  default:
6758152Seric   usage:
6858152Seric 			fputs("usage: mailstats [-C cffile] [-f stfile]\n", stderr);
6934047Sbostic 			exit(EX_USAGE);
7034047Sbostic 		}
7158152Seric 	}
7234047Sbostic 	argc -= optind;
7334047Sbostic 	argv += optind;
7434047Sbostic 
7558152Seric 	if (argc != 0)
7658152Seric 		goto usage;
7758152Seric 
7858152Seric 	if ((cfp = fopen(cfile, "r")) == NULL)
7958152Seric 	{
8058152Seric 		fprintf(stderr, "mailstats: ");
8158152Seric 		perror(cfile);
8258152Seric 		exit(EX_NOINPUT);
8358152Seric 	}
8458152Seric 
8558152Seric 	mno = 0;
8658152Seric 	(void) strcpy(mtable[mno++], "prog");
8758152Seric 	(void) strcpy(mtable[mno++], "*file*");
8858152Seric 	(void) strcpy(mtable[mno++], "*include*");
8958152Seric 
9058152Seric 	while (fgets(buf, sizeof(buf), cfp) != NULL)
9158152Seric 	{
9258152Seric 		register char *b;
9358152Seric 		char *s;
9458152Seric 		register char *m;
9558152Seric 
9658152Seric 		b = buf;
9758152Seric 		switch (*b++)
9858152Seric 		{
9958152Seric 		  case 'M':		/* mailer definition */
10058152Seric 			break;
10158152Seric 
10258152Seric 		  case 'O':		/* option -- see if .st file */
103*67673Seric 			if (strncasecmp(b, " StatusFile", 11) == 0 &&
104*67673Seric 			    !isalnum(b[11]))
105*67673Seric 			{
106*67673Seric 				/* new form -- find value */
107*67673Seric 				b = strchr(b, '=');
108*67673Seric 				if (b == NULL)
109*67673Seric 					continue;
110*67673Seric 				while (isspace(*++b))
111*67673Seric 					continue;
112*67673Seric 			}
113*67673Seric 			else if (*b++ != 'S')
114*67673Seric 			{
115*67673Seric 				/* something else boring */
11658152Seric 				continue;
117*67673Seric 			}
11858152Seric 
119*67673Seric 			/* this is the S or StatusFile option -- save it */
12058152Seric 			strcpy(sfilebuf, b);
121*67673Seric 			b = strchr(sfilebuf, '#');
122*67673Seric 			if (b == NULL)
123*67673Seric 				b = strchr(sfilebuf, '\n');
124*67673Seric 			if (b == NULL)
125*67673Seric 				b = &sfilebuf[strlen(sfilebuf)];
126*67673Seric 			while (isspace(*--b))
127*67673Seric 				continue;
128*67673Seric 			*++b = '\0';
12958152Seric 			if (sfile == NULL)
13058152Seric 				sfile = sfilebuf;
13158152Seric 
13258152Seric 		  default:
13358152Seric 			continue;
13458152Seric 		}
13558152Seric 
13658152Seric 		if (mno >= MAXMAILERS)
13758152Seric 		{
13858152Seric 			fprintf(stderr,
13958152Seric 				"Too many mailers defined, %d max.\n",
14058152Seric 				MAXMAILERS);
14158152Seric 			exit(EX_SOFTWARE);
14258152Seric 		}
14358152Seric 		m = mtable[mno];
14458152Seric 		s = m + MNAMELEN;		/* is [MNAMELEN+1] */
14558152Seric 		while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
14658152Seric 			*m++ = *b++;
14758152Seric 		*m = '\0';
14858152Seric 		for (i = 0; i < mno; i++)
14958152Seric 		{
15058152Seric 			if (strcmp(mtable[i], mtable[mno]) == 0)
15158152Seric 				break;
15258152Seric 		}
15358152Seric 		if (i == mno)
15458152Seric 			mno++;
15558152Seric 	}
15658152Seric 	(void) fclose(cfp);
15758152Seric 	for (; mno < MAXMAILERS; mno++)
15858152Seric 		mtable[mno][0]='\0';
15958152Seric 
16058152Seric 	if (sfile == NULL)
16158152Seric 	{
16258152Seric 		fprintf(stderr, "mailstats: no statistics file located\n");
16358152Seric 		exit (EX_OSFILE);
16458152Seric 	}
16558152Seric 
16634047Sbostic 	if ((fd = open(sfile, O_RDONLY)) < 0) {
16734047Sbostic 		fputs("mailstats: ", stderr);
1684379Seric 		perror(sfile);
1694379Seric 		exit(EX_NOINPUT);
1704379Seric 	}
17134047Sbostic 	if (read(fd, &stat, sizeof(stat)) != sizeof(stat) ||
17258152Seric 	    stat.stat_size != sizeof(stat))
17358152Seric 	{
17434047Sbostic 		fputs("mailstats: file size changed.\n", stderr);
1754379Seric 		exit(EX_OSERR);
1764379Seric 	}
1774379Seric 
1784380Seric 	printf("Statistics from %s", ctime(&stat.stat_itime));
17958152Seric 	printf(" M msgsfr bytes_from  msgsto   bytes_to%s\n",
18058152Seric 		mnames ? "  Mailer" : "");
1814379Seric 	for (i = 0; i < MAXMAILERS; i++)
18258152Seric 	{
18334047Sbostic 		if (stat.stat_nf[i] || stat.stat_nt[i])
18458152Seric 		{
18558152Seric 			printf("%2d %6ld %10ldK %6ld %10ldK", i,
18634047Sbostic 			    stat.stat_nf[i], stat.stat_bf[i],
18734047Sbostic 			    stat.stat_nt[i], stat.stat_bt[i]);
18858152Seric 			if (mnames)
18958152Seric 				printf("  %s", mtable[i]);
19058152Seric 			printf("\n");
19165221Seric 			frmsgs += stat.stat_nf[i];
19265221Seric 			frbytes += stat.stat_bf[i];
19365221Seric 			tomsgs += stat.stat_nt[i];
19465221Seric 			tobytes += stat.stat_bt[i];
19558152Seric 		}
19658152Seric 	}
19765221Seric 	printf("========================================\n");
19865221Seric 	printf(" T %6ld %10ldK %6ld %10ldK\n",
19965221Seric 		frmsgs, frbytes, tomsgs, tobytes);
20058152Seric 	exit(EX_OK);
2014379Seric }
202