home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
-
- #define PARMSZ 200
- #define DAY_TIME(h,m,s) (((long)h*60L+(long)m)*60L+(long)s)
- #define MINS_PER_DAY (24L*60L)
-
- char buffer[200];
-
- FILE *fopen();
- int sc, sscanf();
- char *fgets();
- char parm1[PARMSZ], parm2[PARMSZ], parm3[PARMSZ],
- parm4[PARMSZ], parm5[PARMSZ], parm6[PARMSZ];
- char start_time[PARMSZ];
-
-
- int main() {
-
- int running_flag = 0; /* indicates whether a session is open */
-
- /*
- For each record of standard input:
-
- 1) Read the fields
- 2) if the 4th field is SUCCEEDED, start a session
- - remember the time for later
- 3) if the 4th file is OK and the 5th is (conversation, finish a session
- 4) if the 4th file is LOGIN and the 5th is FAILED finish a session
- */
- while (fgets(buffer,(int)sizeof(buffer),stdin)) {
-
- sc = sscanf(buffer,"%s%s%s%s%s%s",
- parm1, parm2, parm3, parm4, parm5, parm6);
-
- if (sc >= 5) {
-
- if (!strcmp(parm4,"SUCCEEDED")) {
- if (running_flag)
- (void)fprintf (stderr, "Dangling open at %s\n", start_time);
- (void)strcpy (start_time, parm3);
- running_flag = 1;
- }
-
- if ((!strcmp(parm4,"OK") && !strcmp(parm5,"(conversation"))
- || (!strcmp(parm4,"LOGIN") && !strcmp(parm5,"FAILED"))
- || (!strcmp(parm4,"FAILED") && !strcmp(parm5,"(conversation"))
- || (!strcmp(parm4,"HANDSHAKE") && !strcmp(parm5,"FAILED"))) {
- if (running_flag) {
- talk_end (parm2, start_time, parm3);
- running_flag = 0;
- }
- }
-
- if ((!strcmp(parm5,"FAILED") && !strcmp(parm6,"(conversation"))) {
- if (running_flag) {
- talk_end (parm2, start_time, parm4);
- running_flag = 0;
- }
- }
- }
- }
- return (0);
- }
-
- long time_secs();
-
- /*
- talk_end - subtract times and output record
- */
- talk_end (system, from, to)
- char *system, *from, *to;
- {
- long from_secs, to_secs, diff_secs;
- long diff_min;
-
- from_secs = time_secs (from);
- if (from_secs == -1L)
- return;
- to_secs = time_secs (to);
- if (to_secs == -1L)
- return;
- diff_secs = to_secs-from_secs;
- diff_min = (diff_secs + 59L) / 60L;
- if (diff_min < 0)
- diff_min += MINS_PER_DAY;
- (void)printf ("%s %s %ld\n", system, from, diff_min);
- }
-
-
- /*
- time_secs - convert a string to seconds since midnight
- */
- long time_secs (time_string)
- char *time_string;
- {
- int h,m,s;
-
- if (sscanf(time_string, "(%*d/%*d-%d:%d:%d", &h, &m, &s) == 3) {
- return (DAY_TIME (h,m,s));
- } else {
- (void)fprintf(stderr, "Error converting time string %s/n", time_string);
- return (-1L);
- }
- }
-