]> git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
a29638b41bbcc440032d76350f29053c51dc3321
[thirdparty/sarg.git] / totday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 void day_totalize(const char *tmp, const struct userinfostruct *uinfo, int indexonly)
31 {
32
33 FILE *fp_in, *fp_ou;
34
35 char data[20];
36 char hora[20];
37 char min[20];
38 char elap[20];
39 char odata[20];
40 char ohora[20];
41 char oelap[20];
42 char csort[255];
43 char wdirname[MAXLEN];
44 char sortout[MAXLEN];
45 char arqout[MAXLEN];
46 int regs=0;
47 long long int telap=0;
48 int cstatus;
49 struct getwordstruct gwarea;
50
51 if(indexonly) return;
52 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
53
54 sprintf(wdirname,"%s/%s.htmp",tmp,uinfo->filename);
55 sprintf(arqout,"%s/%s.day",tmp,uinfo->filename);
56 sprintf(sortout,"%s/%s.sort",tmp,uinfo->filename);
57
58 sprintf(csort,"sort -k 1,1 -k 2,2 -o \"%s\" \"%s\"",sortout,wdirname);
59 cstatus=system(csort);
60 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
61 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
62 debuga(_("sort command: %s\n"),csort);
63 exit(1);
64 }
65 if((fp_in=fopen(sortout,"r"))==NULL) {
66 debuga(_("SARG: (totday) Cannot open log file: %s\n"),sortout);
67 debuga(_("sort command: %s\n"),csort);
68 exit(1);
69 }
70
71 unlink(wdirname);
72
73 if((fp_ou=fopen(arqout,"w"))==NULL) {
74 debuga(_("(totday) Cannot open log file: %s\n"),arqout);
75 exit(1);
76 }
77
78 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
79 fixendofline(buf);
80 getword_start(&gwarea,buf);
81 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,':')<0 ||
82 getword(min,sizeof(min),&gwarea,':')<0 || getword_skip(20,&gwarea,'\t')<0 ||
83 getword(elap,sizeof(elap),&gwarea,0)<0) {
84 debuga(_("There is a broken record or garbage in file %s\n"),sortout);
85 exit(1);
86 }
87
88 if(!regs) {
89 strcpy(odata,data);
90 strcpy(ohora,hora);
91 strcpy(oelap,elap);
92 regs++;
93 }
94
95 if(strcmp(hora,ohora) != 0 || strcmp(data,odata) != 0) {
96 fprintf(fp_ou,"%s\t%s\t%015lld\n",odata,ohora,telap);
97 strcpy(odata,data);
98 strcpy(ohora,hora);
99 telap=0;
100 }
101
102 telap+=my_atoll(elap);
103 }
104
105 fprintf(fp_ou,"%s\t%s\t%015lld\n",data,hora,telap);
106
107 fclose(fp_in);
108 fclose(fp_ou);
109
110 unlink(sortout);
111
112 return;
113
114 }