122724Sdist /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
3*62512Sbostic  * Copyright (c) 1988, 1993
4*62512Sbostic  *	The Regents of the University of California.  All rights reserved.
534047Sbostic  *
642822Sbostic  * %sccs.include.redist.c%
734047Sbostic  *
834047Sbostic  */
922724Sdist 
1022724Sdist #ifndef lint
11*62512Sbostic static char copyright[] =
12*62512Sbostic "@(#) Copyright (c) 1988, 1993\n\
13*62512Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1434047Sbostic #endif /* not lint */
1522724Sdist 
1622724Sdist #ifndef lint
17*62512Sbostic static char sccsid[] = "@(#)mailstats.c	8.1 (Berkeley) 06/07/93";
1834047Sbostic #endif /* not lint */
194379Seric 
2034047Sbostic #include <sys/file.h>
2134047Sbostic #include <sendmail.h>
2234047Sbostic #include <mailstats.h>
2358152Seric #include <pathnames.h>
244379Seric 
2558152Seric #define MNAMELEN	20	/* max length of mailer name */
2658152Seric 
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;
3558152Seric 	int mno;
3634047Sbostic 	int ch, fd;
3758152Seric 	char *sfile;
3858152Seric 	char *cfile;
3958152Seric 	FILE *cfp;
4058152Seric 	bool mnames;
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 */
10358152Seric 			if (*b++ != 'S')
10458152Seric 				continue;
10558152Seric 
10658152Seric 			/* yep -- save this */
10758152Seric 			strcpy(sfilebuf, b);
10858152Seric 			b = strchr(sfilebuf, '\n');
10958152Seric 			if (b != NULL)
11058152Seric 				*b = '\0';
11158152Seric 			if (sfile == NULL)
11258152Seric 				sfile = sfilebuf;
11358152Seric 
11458152Seric 		  default:
11558152Seric 			continue;
11658152Seric 		}
11758152Seric 
11858152Seric 		if (mno >= MAXMAILERS)
11958152Seric 		{
12058152Seric 			fprintf(stderr,
12158152Seric 				"Too many mailers defined, %d max.\n",
12258152Seric 				MAXMAILERS);
12358152Seric 			exit(EX_SOFTWARE);
12458152Seric 		}
12558152Seric 		m = mtable[mno];
12658152Seric 		s = m + MNAMELEN;		/* is [MNAMELEN+1] */
12758152Seric 		while (*b != ',' && !isspace(*b) && *b != '\0' && m < s)
12858152Seric 			*m++ = *b++;
12958152Seric 		*m = '\0';
13058152Seric 		for (i = 0; i < mno; i++)
13158152Seric 		{
13258152Seric 			if (strcmp(mtable[i], mtable[mno]) == 0)
13358152Seric 				break;
13458152Seric 		}
13558152Seric 		if (i == mno)
13658152Seric 			mno++;
13758152Seric 	}
13858152Seric 	(void) fclose(cfp);
13958152Seric 	for (; mno < MAXMAILERS; mno++)
14058152Seric 		mtable[mno][0]='\0';
14158152Seric 
14258152Seric 	if (sfile == NULL)
14358152Seric 	{
14458152Seric 		fprintf(stderr, "mailstats: no statistics file located\n");
14558152Seric 		exit (EX_OSFILE);
14658152Seric 	}
14758152Seric 
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) ||
15458152Seric 	    stat.stat_size != sizeof(stat))
15558152Seric 	{
15634047Sbostic 		fputs("mailstats: file size changed.\n", stderr);
1574379Seric 		exit(EX_OSERR);
1584379Seric 	}
1594379Seric 
1604380Seric 	printf("Statistics from %s", ctime(&stat.stat_itime));
16158152Seric 	printf(" M msgsfr bytes_from  msgsto   bytes_to%s\n",
16258152Seric 		mnames ? "  Mailer" : "");
1634379Seric 	for (i = 0; i < MAXMAILERS; i++)
16458152Seric 	{
16534047Sbostic 		if (stat.stat_nf[i] || stat.stat_nt[i])
16658152Seric 		{
16758152Seric 			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]);
17058152Seric 			if (mnames)
17158152Seric 				printf("  %s", mtable[i]);
17258152Seric 			printf("\n");
17358152Seric 		}
17458152Seric 	}
17558152Seric 	exit(EX_OK);
1764379Seric }
177