home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
-
- #define NAMESIZE (31)
- #define DBSIZE 300
- #define MAXHUD (200)
- char gepath[128];
-
- FILE *dbfp;
-
- int thisdate;
- struct tm *block;
-
- struct dbrecord {
- char echoname[NAMESIZE];
- int area; /* area number */
- int tally[30]; /* 30 day record */
- } db[DBSIZE] = {0};
-
- #define a_area (((struct dbrecord *)a)->area)
- #define b_area (((struct dbrecord *)b)->area)
- #define a_name (((struct dbrecord *)a)->echoname)
- #define b_name (((struct dbrecord *)b)->echoname)
-
- int sort_function(const void *a, const void *b) {
- if (a_area == 0) return (b_area ? 1 : 0);
- if (b_area == 0) return -1;
- if (a_area <= MAXHUD) { /* first is hudson */
- if (b_area <= MAXHUD)
- return (a_area < b_area? -1 : 1); /* no dupes are possible */
- else
- return -1; /* Hudson less than passthrough */
- }
- else { /* first is passthrough */
- if (b_area <= MAXHUD)
- return 1;
- else
- return strcmp(a_name, b_name);
- }
- }
-
- void sortit(void) {
- int i, i2, j;
- for (i=0; i<DBSIZE; i++) { /* delete entries that have no messages */
- for (j=0,i2=0; i2<30; i2++) j += db[i].tally[i2];
- if (j == 0) { /* clear entry */
- db[i].echoname[0] = 0;
- db[i].area = 0;
- }
- }
- qsort((void *)db, DBSIZE, sizeof(struct dbrecord), sort_function);
- }
-
- void makereport(char *filename, int days, char *title) {
-
- FILE *fp;
- char ans[64];
- char areastr[6];
- int st = (days>28 || days==1 ? 0 : 1); /* starting day */
- int i,j;
- long gt=0; /* grand total */
- int tot[DBSIZE]; /* total for each area */
- int wtot[4][DBSIZE]; /* totals for weeks */
-
- days += st; /* when we start on day 1, end is one further */
-
- if ((fp = fopen(filename, "w")) == NULL) {
- fprintf(stderr, "Error--unable to create %s\n", filename);
- return; /* no error exit on this one */
- }
-
- strcpy(ans, asctime(block));
- fprintf(fp, "%s Report prepared %.10s, %.4s\n\n",
- title, ans, &ans[20]);
-
-
- for (i = 0; i < DBSIZE; i++) {
- tot[i] = 0;
- for (j = st; j < days; j++) tot[i] += db[i].tally[j];
- gt += tot[i];
- if (days==29) {
- wtot[0][i] = wtot[1][i] = wtot[2][i] = wtot[3][i] = 0;
- for (j=1; j<8; j++) {
- wtot[0][i] += db[i].tally[j];
- wtot[1][i] += db[i].tally[j+7];
- wtot[2][i] += db[i].tally[j+14];
- wtot[3][i] += db[i].tally[j+21];
- }
- }
- }
-
- if (days==8)
- fputs(" # Area Name Daily Totals Total Percent\n",fp);
- else if (days==29)
- fputs(" # Area Name Weekly Totals Total Percent\n",fp);
- else
- fputs(" # Area Name Total Percent\n",fp);
-
- for (i = 0; i < DBSIZE; i++) {
- if (tot[i] == 0) continue;
- if (db[i].area > MAXHUD)
- strcpy(areastr, " P ");
- else
- sprintf(areastr, "%3d", db[i].area);
- if (days==8) { /* different for weekly report */
- fprintf(fp, "%s %-20.20s%4d%4d%4d%4d%4d%4d%4d %5d %.1f%%\n",
- areastr,
- db[i].echoname,
- db[i].tally[7],
- db[i].tally[6],
- db[i].tally[5],
- db[i].tally[4],
- db[i].tally[3],
- db[i].tally[2],
- db[i].tally[1],
- tot[i],
- (100.0 * tot[i])/gt);
- }
- else if (days==29) { /* and for monthly report */
- fprintf(fp, "%s %-20.20s%4d %4d %4d %4d %5d %.1f%%\n",
- areastr,
- db[i].echoname,
- wtot[3][i], wtot[2][i], wtot[1][i], wtot[0][i],
- tot[i],
- (100.0 * tot[i])/gt);
- }
- else {
- fprintf(fp, "%s %-30.30s %5d %.1f%%\n", areastr,
- db[i].echoname,
- tot[i],
- (100.0 * tot[i])/gt);
- }
- }
- fprintf(fp, "\n%ld total messages\n", gt);
- fclose(fp);
- }
-
- void readsummary(void) {
- FILE *transp;
- char filename[128];
- char buff[128];
- char area[NAMESIZE];
- int i,j, areanum;
-
- strcpy(filename, gepath);
- strcat(filename, "summary.log");
-
- if ((transp = fopen(filename, "r")) == NULL) {
- fprintf(stderr, "Note-- summary.log file not found\n");
- return;
- }
-
- i=5;
- while (i--) fgets(buff, sizeof(buff), transp); /* header lines */
-
- while (fgets(buff, sizeof(buff), transp) != NULL) {
- if (strncmp(buff, "-----", 5) == 0) break; /* end reached */
- if (sscanf(buff, "%d %30s %d", &areanum, &area, &j) != 3 ||
- areanum < 1 ) continue; /* bad record ?? */
- for (i=0; i<DBSIZE && db[i].echoname[0] != 0; i++)
- if (strcmp(area, db[i].echoname)==0) break;
- if (i < DBSIZE) {
- strcpy(db[i].echoname, area);
- db[i].area = areanum;
- db[i].tally[0] += j;
- }
- }
- fclose(transp);
- unlink(filename); /* get rid of file! */
- }
-
-
- void getdb(void) { /* get the database */
- time_t timer;
- int olddate;
- int i, diff;
-
- timer = time(NULL);
- block = localtime(&timer);
- thisdate = block->tm_yday;
-
- if ((dbfp = fopen("netrep2.dat","rb+")) == NULL) { /* no file */
- if ((dbfp = fopen("netrep2.dat", "wb+")) == NULL) {
- fprintf(stderr, "Error-- could not create netrep2.dat file\n");
- exit(1);
- }
- memset(db, 0, sizeof(db));
- return;
- }
-
- fread(&olddate, sizeof(int), 1, dbfp);
-
- if (fread(db, sizeof(db), 1, dbfp) != 1) {
- fprintf(stderr, "Error -- bad netrep2.dat file\n");
- /* exit(1); */
- }
-
- rewind(dbfp);
-
-
- if (olddate == thisdate) return;
-
- if (olddate > thisdate) {
- olddate -= 365;
- if (block->tm_year % 4 == 1) olddate--;
- }
-
- diff = thisdate - olddate;
-
- if (diff > 29) { /* boy has time passed! */
- memset(db, 0, sizeof(db));
- return;
- }
-
- for (i=0; i<DBSIZE; i++) {
- memmove(&db[i].tally[diff], &db[i].tally[0], sizeof(int)*(30-diff));
- memset(&db[i].tally[0], 0, sizeof(int)*diff);
- }
- return;
- }
-
-
- void putdb(void) {
- if (fwrite(&thisdate, sizeof(int), 1, dbfp) != 1 ||
- fwrite(db, sizeof(db), 1, dbfp) != 1) {
- fprintf(stderr, "Error -- write of netrep2.dat file failed");
- exit(1);
- }
- fclose(dbfp);
- }
-
-
- void main(int argc, char **argv) {
- strcpy(gepath, getenv("GE")); /* gecho path */
- if (*gepath && gepath[strlen(gepath)-1] !='\\') strcat(gepath, "\\");
-
- getdb();
-
- readsummary();
-
- if (argc > 1) sortit(); /* sort database if we are making a report */
- if(argc > 1) makereport(argv[1], 1, "Todays's Echo Traffic ");
- if(argc > 2) makereport(argv[2], 7, "Previous Week's Echo Traffic");
- if(argc > 3) makereport(argv[3], 28, "Four Week Echo Traffic ");
- if(argc > 4) makereport(argv[4], 30, "Thirty Day Echo Traffic ");
-
- putdb();
- }
-