]> git.ipfire.org Git - thirdparty/sarg.git/blame - datafile.c
Remove unused variables
[thirdparty/sarg.git] / datafile.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
f84a35a3 30void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache);
25697a35
GS
31
32void data_file(char *tmp)
33{
9bd92830 34 FILE *fp_in, *fp_ou=NULL;
25697a35 35
9bd92830
FM
36 char *buf;
37 char accdia[11], acchora[9], accip[MAXLEN], *accurl;
38 char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN];
39 char *oldurl;
5025f299 40 char acccode[50], oldacccode[50];
9bd92830
FM
41 char ipantes[MAXLEN], nameantes[MAXLEN];
42 char accsmart[MAXLEN];
43 char crc2[50];
44 char *str;
45 char tmp3[MAXLEN];
9bd92830 46 char u2[MAX_USER_LEN];
93551487 47 userscan uscan;
9bd92830
FM
48 long long int nbytes=0;
49 long long int nelap=0;
50 long long int nacc=0;
51 int rtotal=0;
52 long long int incache=0;
53 long long int oucache=0;
54 long long int accbytes;
55 long long int accelap;
9bd92830
FM
56 int new_user;
57 int same_url;
58 int url_len;
59 int ourl_size;
60 struct getwordstruct gwarea;
61 struct userinfostruct *uinfo;
62 longline line;
25697a35 63
9bd92830
FM
64 ipantes[0]='\0';
65 nameantes[0]='\0';
25697a35 66
9bd92830
FM
67 oldurl=NULL;
68 ourl_size=0;
25697a35 69
93551487
FM
70 uscan=userinfo_startscan();
71 if (uscan == NULL) {
72 debuga(_("Cannot enumerate the user list\n"));
9bd92830
FM
73 exit(EXIT_FAILURE);
74 }
93551487 75 while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
9bd92830
FM
76 strcpy(u2,uinfo->id);
77 if(Ip2Name && uinfo->id_is_ip) {
78 strcpy(ipantes,u2);
79 ip2name(u2,sizeof(u2));
80 strcpy(nameantes,u2);
81 }
82 user_find(uinfo->label,MAX_USER_LEN, u2);
93551487 83 if(debug) debuga(_("Reading user file: %s/%s\n"),tmp,uinfo->filename);
f2ec8c75 84
93551487
FM
85 if (snprintf(tmp3,sizeof(tmp3),"%s/%s.log",tmp,uinfo->filename)>=sizeof(tmp3)) {
86 debuga(_("(datafile) directory path too long: %s/%s.log\n"),tmp,uinfo->filename);
9bd92830
FM
87 exit(EXIT_FAILURE);
88 }
f2ec8c75 89
9bd92830
FM
90 if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
91 debuga(_("(datafile) Cannot open file %s\n"),tmp3);
92 exit(EXIT_FAILURE);
93 }
25697a35 94
9bd92830
FM
95 if ((line=longline_create())==NULL) {
96 debuga(_("Not enough memory to read the downloaded files.\n"));
97 exit(EXIT_FAILURE);
98 }
f84a35a3 99
9bd92830
FM
100 ttopen=0;
101 new_user=1;
102 while((buf=longline_read(fp_in,line))!=NULL) {
103 getword_start(&gwarea,buf);
104 if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 ||
007905af
FM
105 getword(accip,sizeof(accip),&gwarea,'\t')<0 ||
106 getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 ||
107 getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) {
9bd92830
FM
108 debuga(_("There is a broken record or garbage in file %s\n"),tmp3);
109 exit(EXIT_FAILURE);
110 }
111 if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) {
112 debuga(_("There is an invalid smart info in file %s\n"),tmp3);
113 exit(EXIT_FAILURE);
114 }
25697a35 115
9bd92830
FM
116 if(Ip2Name) {
117 if(strcmp(accip,ipantes) != 0) {
118 strcpy(ipantes,accip);
119 ip2name(accip,sizeof(accip));
120 strcpy(nameantes,accip);
121 }
122 else strcpy(accip,nameantes);
123 }
5542cb59 124
9bd92830
FM
125 if(!rtotal){
126 url_len=strlen(accurl);
127 if (!oldurl || url_len>=ourl_size) {
128 ourl_size=url_len+1;
129 oldurl=realloc(oldurl,ourl_size);
130 if (!oldurl) {
131 debuga(_("Not enough memory to store the url\n"));
132 exit(EXIT_FAILURE);
133 }
134 }
135 strcpy(oldurl,accurl);
136 strcpy(oldacccode,acccode);
137 strcpy(oldaccip,accip);
138 strcpy(oldaccdia,accdia);
139 strcpy(oldacchora,acchora);
140 new_user=0;
141 rtotal++;
142 }
143 same_url=(strcmp(oldurl,accurl) == 0);
5542cb59 144
9bd92830
FM
145 if(!same_url || new_user){
146 if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){
147 debuga(_("(datafile) Cannot open file %s\n"),DataFile);
148 exit(EXIT_FAILURE);
149 }
150 saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache);
151 nacc=0;
152 nbytes=0;
153 nelap=0;
154 incache=0;
155 oucache=0;
156 new_user=0;
157 }
5542cb59 158
9bd92830
FM
159 nacc++;
160 nbytes+=accbytes;
161 nelap+=accelap;
25697a35 162
9bd92830
FM
163 strcpy(crc2,acccode);
164 str=strchr(crc2,'/');
165 if (str) *str='\0';
5542cb59 166
9bd92830
FM
167 if(strstr(crc2,"MISS") != 0) oucache+=accbytes;
168 else incache+=accbytes;
25697a35 169
9bd92830
FM
170 if (!same_url) {
171 url_len=strlen(accurl);
172 if (url_len>=ourl_size) {
173 ourl_size=url_len+1;
174 oldurl=realloc(oldurl,ourl_size);
175 if (!oldurl) {
176 debuga(_("Not enough memory to store the url\n"));
177 exit(EXIT_FAILURE);
178 }
179 }
180 strcpy(oldurl,accurl);
181 }
182 new_user=0;
183 strcpy(oldacccode,acccode);
184 strcpy(oldaccip,accip);
185 strcpy(oldaccdia,accdia);
186 strcpy(oldacchora,acchora);
187 }
5542cb59 188
9bd92830
FM
189 fclose(fp_in);
190 longline_destroy(&line);
191 }
93551487 192 userinfo_stopscan(uscan);
9bd92830
FM
193 if (oldurl) free(oldurl);
194 if (fp_ou) fclose(fp_ou);
5542cb59 195
9bd92830
FM
196 if(debug)
197 debuga(_("Datafile %s written successfully\n"),DataFile);
25697a35
GS
198}
199
f84a35a3 200void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache)
25697a35 201{
9bd92830
FM
202 char val[20];
203 char name[512];
25697a35 204
9bd92830
FM
205 if((DataFileFields & DATA_FIELD_USER) != 0) {
206 fputs(uinfo->label,fp_ou);
207 fputc(DataFileDelimiter[0],fp_ou);
208 }
209 if((DataFileFields & DATA_FIELD_DATE) != 0) {
210 fputs(dia,fp_ou);
211 fputc(DataFileDelimiter[0],fp_ou);
212 }
213 if((DataFileFields & DATA_FIELD_TIME) != 0) {
214 fputs(hora,fp_ou);
215 fputc(DataFileDelimiter[0],fp_ou);
216 }
217 if((DataFileFields & DATA_FIELD_URL) != 0) {
218 url_hostname(url,name,sizeof(name));
4afbb7a5 219 if (DataFileUrl == DATAFILEURL_IP) name2ip(name,sizeof(name));
9bd92830
FM
220 fputs(name,fp_ou);
221 fputc(DataFileDelimiter[0],fp_ou);
222 }
223 if((DataFileFields & DATA_FIELD_CONNECT) != 0) {
224 my_lltoa(nacc,val,sizeof(val),0);
225 fputs(val,fp_ou);
226 fputc(DataFileDelimiter[0],fp_ou);
227 }
228 if((DataFileFields & DATA_FIELD_BYTES) != 0) {
229 my_lltoa(nbytes,val,sizeof(val),0);
230 fputs(val,fp_ou);
231 fputc(DataFileDelimiter[0],fp_ou);
232 }
233 if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) {
234 my_lltoa(incache,val,sizeof(val),0);
235 fputs(val,fp_ou);
236 fputc(DataFileDelimiter[0],fp_ou);
237 }
238 if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) {
239 my_lltoa(oucache,val,sizeof(val),0);
240 fputs(val,fp_ou);
241 fputc(DataFileDelimiter[0],fp_ou);
242 }
243 if((DataFileFields & DATA_FIELD_ELAPSED) != 0) {
244 my_lltoa(nelap,val,sizeof(val),0);
245 fputs(val,fp_ou);
246 fputc(DataFileDelimiter[0],fp_ou);
247 }
25697a35 248
9bd92830 249 fputc('\n',fp_ou);
25697a35 250}