]> git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
Moved all the functions declarations from conf.h to defs.h for consistency.
[thirdparty/sarg.git] / totday.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void day_totalize(const char *tmp, const char *user, int indexonly)
30 {
31
32 FILE *fp_in, *fp_ou;
33
34 char data[20];
35 char hora[20];
36 char min[20];
37 char elap[20];
38 char odata[20];
39 char ohora[20];
40 char oelap[20];
41 char hm[20];
42 char ohm[20];
43 char csort[255];
44 char wdirname[MAXLEN];
45 char sortout[MAXLEN];
46 char arqout[MAXLEN];
47 int regs=0;
48 long long int telap=0;
49 long long int tused=0;
50 int cstatus;
51
52 if(indexonly) return;
53 if(strstr(ReportType,"users_sites") == 0) return;
54
55 sprintf(wdirname,"%s/%s.htmp",tmp,user);
56 sprintf(arqout,"%s/%s.day",tmp,user);
57 sprintf(sortout,"%s/%s.sort",tmp,user);
58
59 sprintf(csort,"sort -k 1,1 -k 2,2 -o '%s' '%s'",sortout,wdirname);
60 cstatus=system(csort);
61 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
62 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
63 fprintf(stderr, "SARG: sort command: %s\n",csort);
64 exit(1);
65 }
66 if((fp_in=fopen(sortout,"r"))==NULL) {
67 fprintf(stderr, "SARG: (totday) %s: %s\n",text[8],sortout);
68 fprintf(stderr, "SARG: sort command: %s\n",csort);
69 exit(1);
70 }
71
72 unlink(wdirname);
73
74 if((fp_ou=fopen(arqout,"w"))==NULL) {
75 fprintf(stderr, "SARG: (totday) %s: %s\n",text[8],arqout);
76 exit(1);
77 }
78
79 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
80 if(strstr(buf,"\n") != 0)
81 buf[strlen(buf)-1]='\0';
82
83 if (getword(data,sizeof(data),buf,' ')<0 || getword(hora,sizeof(hora),buf,':')<0 ||
84 getword(min,sizeof(min),buf,':')<0 || getword(elap,sizeof(elap),buf,' ')<0 ||
85 getword(elap,sizeof(elap),buf,0)<0) {
86 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",sortout);
87 exit(1);
88 }
89 sprintf(hm,"%s%s",hora,min);
90
91 if(!regs) {
92 strcpy(odata,data);
93 strcpy(ohora,hora);
94 strcpy(oelap,elap);
95 strcpy(ohm,hm);
96 regs++;
97 }
98
99 if(strcmp(hora,ohora) != 0 || strcmp(data,odata) != 0) {
100 if(tused > telap)
101 tused=telap;
102
103 my_lltoa(telap,val1,15);
104 fprintf(fp_ou,"%s %s %s\n",odata,ohora,val1);
105 strcpy(odata,data);
106 strcpy(ohora,hora);
107 strcpy(ohm,hm);
108 telap=0;
109 tused=0;
110 }
111
112 if(strcmp(ohm,hm) != 0) {
113 tused+=60000;
114 strcpy(ohm,hm);
115 }
116
117 telap+=my_atoll(elap);
118
119 }
120
121 if(tused > telap)
122 tused=telap;
123
124 my_lltoa(telap,val1,15);
125 fprintf(fp_ou,"%s %s %s\n",data,hora,val1);
126
127 fclose(fp_in);
128 fclose(fp_ou);
129
130 unlink(sortout);
131
132 return;
133
134 }