File: stats.c

package info (click to toggle)
uutraf 1.1-10
  • links: PTS
  • area: main
  • in suites: woody
  • size: 104 kB
  • ctags: 110
  • sloc: ansic: 805; makefile: 88; sh: 17
file content (144 lines) | stat: -rw-r--r-- 4,644 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*****************************************************************************
 * stats.c -- Part of uutraf.c, an UUCP Traffic analyzer and cost estimator  *
 *                                                                           *
 * Reads an Taylor-UUCP Log-/Statsfile, and generates a report out of it     *
 *                                                                           *
 * stats.c is (c) David Frey, 1995    					     *
 *								             *
 * Modifications by Yves Arrouye, 1996                                       *
 *								             *
 * This program is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU General Public License as published by the     *
 * Free Software Foundation; either version 2 of the License, or (at your    *
 * option) any later version.                                                *
 *									     *
 * This program is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                      *
 * See the GNU General Public License for more details.                      *
 *									     *
 * You should have received a copy of the GNU General Public License along   *
 * with this program; if not, write to the Free Software Foundation, Inc.,   *
 * 675 Mass Ave, Cambridge, MA 02139, USA.                                   *
 *****************************************************************************/

/* $Log: stats.c,v $
 * Revision 1.0  1996/01/01 16:44:17  david
 * Initial revision
 * */

static char RCSId[]="$Id: stats.c,v 1.0 1996/01/01 16:44:17 david Rel $";
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <error.h>
#include <errno.h>

#include "uutraf.h"

extern int errno;

void ReadStatsfile(char *statsfilename)
/*
 * Reads in the stats file and builds a linked list of lines, containing
 * the original information.
 * This list is used by AnalyseLogfile, since it is not possible to get
 * the size of a mail without the information in the stats file.
 * Moreover, when connecting to an non-Taylor UUCP-site, the size of a
 * received file is not recorded in the log file.
 *
 */
{
  FILE *statsfile;
  statsentry *ptr;

  char hostname[HOSTNAMELENGTH+1];
  hostentry *host;
  char statsline[LINE_MAX+1];
  char *text;
  char text0[LINE_MAX+1];
  struct tm time;
  short int received;
  float duration;
  int sec100;

  statsfile=fopen(statsfilename,"r"); 
  if (statsfile == NULL)
  {
    fprintf(stderr,"%s: can't open %s: %s\n",
	    progname,statsfilename,strerror(errno));
    exit(1);
  }

  /* Read and parse the Statsfile */
  while (fgets(statsline,LINE_MAX,statsfile) != NULL)
  {
    ptr = calloc(1,sizeof(statsentry));
    if (ptr != NULL)
    {
      sscanf(statsline,"%*s %8s (%d-%d-%d %d:%d:%d.%d) %[ -~]",
	     hostname, &time.tm_year,&time.tm_mon, &time.tm_mday,
	               &time.tm_hour,&time.tm_min, &time.tm_sec, 
	               &sec100,text0);

      ptr->when = statstime(time,sec100);
      text = &text0[0];

      if (strncmp(text,FAILED,sizeof(FAILED)-1) == 0)
        text += (sizeof(FAILED)-1);
      
      ptr->received = received
	= (strncmp(text,RECEIVED,sizeof(RECEIVED)-1) == 0);
      if (received) text += (sizeof(RECEIVED)-1);
      else          text += (sizeof(SENT)-1);

      ptr->size = atol(text);          
      while (isdigit(*text))  text++;
      while (!isdigit(*text)) text++;  
      duration = atof(text);

      host = gethostentry(hostname);
      if (received) 
      { 
	host->datarecnum++;  host->datarecbytes  += ptr->size; 
	host->rectime += duration; 
      }
      else          
      { 
	host->datasentnum++; host->datasentbytes += ptr->size; 
	host->senttime += duration; 
      }
      ptr->next = host->statslist; host->statslist = ptr;
    }
    else
    {
      fprintf(stderr,"%s: Out of memory! (ReadStatsfile)\n",progname);
      exit(1);
    }
  }
  fclose(statsfile);
}

statsentry *GetStatsEntry(statsentry *statslist, int received,
    off_t size, etime_t when)
{
  statsentry *ptr, *prevptr, *prevprevptr;
  statsentry *statptr = 0;

  ptr = statslist; prevptr = NULL;
  while ((ptr != NULL) && when < ptr->when
    && (!statptr || !prevptr || prevptr->received != received))
  {
    if ((ptr->received == received) && ((ptr->size == size) || (size==0))) {
      statptr = ptr;
    }
    prevprevptr = prevptr; prevptr = ptr; ptr = ptr->next;
  }

  return statptr ? statptr : prevptr;
}