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.4 (Berkeley) 08/14/94"; 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 (strncasecmp(b, " StatusFile", 11) == 0 && 104 !isalnum(b[11])) 105 { 106 /* new form -- find value */ 107 b = strchr(b, '='); 108 if (b == NULL) 109 continue; 110 while (isspace(*++b)) 111 continue; 112 } 113 else if (*b++ != 'S') 114 { 115 /* something else boring */ 116 continue; 117 } 118 119 /* this is the S or StatusFile option -- save it */ 120 strcpy(sfilebuf, b); 121 b = strchr(sfilebuf, '#'); 122 if (b == NULL) 123 b = strchr(sfilebuf, '\n'); 124 if (b == NULL) 125 b = &sfilebuf[strlen(sfilebuf)]; 126 while (isspace(*--b)) 127 continue; 128 *++b = '\0'; 129 if (sfile == NULL) 130 sfile = sfilebuf; 131 132 default: 133 continue; 134 } 135 136 if (mno >= MAXMAILERS) 137 { 138 fprintf(stderr, 139 "Too many mailers defined, %d max.\n", 140 MAXMAILERS); 141 exit(EX_SOFTWARE); 142 } 143 m = mtable[mno]; 144 s = m + MNAMELEN; /* is [MNAMELEN+1] */ 145 while (*b != ',' && !isspace(*b) && *b != '\0' && m < s) 146 *m++ = *b++; 147 *m = '\0'; 148 for (i = 0; i < mno; i++) 149 { 150 if (strcmp(mtable[i], mtable[mno]) == 0) 151 break; 152 } 153 if (i == mno) 154 mno++; 155 } 156 (void) fclose(cfp); 157 for (; mno < MAXMAILERS; mno++) 158 mtable[mno][0]='\0'; 159 160 if (sfile == NULL) 161 { 162 fprintf(stderr, "mailstats: no statistics file located\n"); 163 exit (EX_OSFILE); 164 } 165 166 if ((fd = open(sfile, O_RDONLY)) < 0) { 167 fputs("mailstats: ", stderr); 168 perror(sfile); 169 exit(EX_NOINPUT); 170 } 171 if (read(fd, &stat, sizeof(stat)) != sizeof(stat) || 172 stat.stat_size != sizeof(stat)) 173 { 174 fputs("mailstats: file size changed.\n", stderr); 175 exit(EX_OSERR); 176 } 177 178 printf("Statistics from %s", ctime(&stat.stat_itime)); 179 printf(" M msgsfr bytes_from msgsto bytes_to%s\n", 180 mnames ? " Mailer" : ""); 181 for (i = 0; i < MAXMAILERS; i++) 182 { 183 if (stat.stat_nf[i] || stat.stat_nt[i]) 184 { 185 printf("%2d %6ld %10ldK %6ld %10ldK", i, 186 stat.stat_nf[i], stat.stat_bf[i], 187 stat.stat_nt[i], stat.stat_bt[i]); 188 if (mnames) 189 printf(" %s", mtable[i]); 190 printf("\n"); 191 frmsgs += stat.stat_nf[i]; 192 frbytes += stat.stat_bf[i]; 193 tomsgs += stat.stat_nt[i]; 194 tobytes += stat.stat_bt[i]; 195 } 196 } 197 printf("========================================\n"); 198 printf(" T %6ld %10ldK %6ld %10ldK\n", 199 frmsgs, frbytes, tomsgs, tobytes); 200 exit(EX_OK); 201 } 202