home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8708 / 7 / scan_cost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  2.4 KB  |  106 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define PARMSZ 200
  5. #define DAY_TIME(h,m,s)    (((long)h*60L+(long)m)*60L+(long)s)
  6. #define MINS_PER_DAY    (24L*60L)
  7.  
  8. char buffer[200];
  9.  
  10. FILE *fopen();
  11. int sc, sscanf();
  12. char *fgets();
  13. char parm1[PARMSZ], parm2[PARMSZ], parm3[PARMSZ],
  14.      parm4[PARMSZ], parm5[PARMSZ], parm6[PARMSZ];
  15. char start_time[PARMSZ];
  16.  
  17.  
  18. int main() {
  19.  
  20.     int running_flag = 0;    /* indicates whether a session is open */
  21.  
  22.     /*
  23.     For each record of standard input:
  24.  
  25.     1) Read the fields
  26.     2) if the 4th field is SUCCEEDED, start a session
  27.         - remember the time for later
  28.     3) if the 4th file is OK and the 5th is (conversation, finish a session
  29.     4) if the 4th file is LOGIN and the 5th is FAILED finish a session
  30.     */
  31.     while (fgets(buffer,(int)sizeof(buffer),stdin)) {
  32.  
  33.     sc = sscanf(buffer,"%s%s%s%s%s%s",
  34.         parm1, parm2, parm3, parm4, parm5, parm6);
  35.  
  36.     if (sc >= 5) {
  37.  
  38.         if (!strcmp(parm4,"SUCCEEDED")) {
  39.         if (running_flag)
  40.             (void)fprintf (stderr, "Dangling open at %s\n", start_time);
  41.         (void)strcpy (start_time, parm3);
  42.         running_flag = 1;
  43.         }
  44.  
  45.         if ((!strcmp(parm4,"OK") && !strcmp(parm5,"(conversation"))
  46.         || (!strcmp(parm4,"LOGIN") && !strcmp(parm5,"FAILED"))
  47.         || (!strcmp(parm4,"FAILED") && !strcmp(parm5,"(conversation"))
  48.         || (!strcmp(parm4,"HANDSHAKE") && !strcmp(parm5,"FAILED"))) {
  49.         if (running_flag) {
  50.             talk_end (parm2, start_time, parm3);
  51.             running_flag = 0;
  52.         }
  53.         }
  54.  
  55.         if ((!strcmp(parm5,"FAILED") && !strcmp(parm6,"(conversation"))) {
  56.         if (running_flag) {
  57.             talk_end (parm2, start_time, parm4);
  58.             running_flag = 0;
  59.         }
  60.         }
  61.     }
  62.     }
  63.     return (0);
  64. }
  65.  
  66. long time_secs();
  67.  
  68. /*
  69.     talk_end - subtract times and output record
  70. */
  71. talk_end (system, from, to)
  72.     char *system, *from, *to;
  73. {
  74.     long from_secs, to_secs, diff_secs;
  75.     long diff_min;
  76.  
  77.     from_secs = time_secs (from);
  78.     if (from_secs == -1L)
  79.     return;
  80.     to_secs = time_secs (to);
  81.     if (to_secs == -1L)
  82.     return;
  83.     diff_secs = to_secs-from_secs;
  84.     diff_min = (diff_secs + 59L) / 60L;
  85.     if (diff_min < 0)
  86.     diff_min += MINS_PER_DAY;
  87.     (void)printf ("%s %s %ld\n", system, from, diff_min);
  88. }
  89.     
  90.  
  91. /*
  92.     time_secs - convert a string to seconds since midnight
  93. */
  94. long time_secs (time_string)
  95.     char *time_string;
  96. {
  97.     int h,m,s;
  98.  
  99.     if (sscanf(time_string, "(%*d/%*d-%d:%d:%d", &h, &m, &s) == 3) {
  100.         return (DAY_TIME (h,m,s));
  101.     } else {
  102.     (void)fprintf(stderr, "Error converting time string %s/n", time_string);
  103.     return (-1L);
  104.     }
  105. }
  106.