]> git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
Fix the creation of a report for only one user with command line option -u
[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 buf[200];
36 char data[20];
37 char hora[20];
38 char min[20];
39 char elap[20];
40 char odata[20];
41 char ohora[20];
42 char oelap[20];
43 char csort[255];
44 char wdirname[MAXLEN];
45 char sortout[2048];
46 char arqout[2048];
47 int regs=0;
48 long long int telap=0;
49 int cstatus;
50 struct getwordstruct gwarea;
51
52 if(indexonly) return;
53 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
54
55 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) {
56 debuga(_("File name too long: %s/%s.htmp\n"),tmp,uinfo->filename);
57 exit(EXIT_FAILURE);
58 }
59 if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
60 debuga(_("File name too long: %s/%s.day\n"),tmp,uinfo->filename);
61 exit(EXIT_FAILURE);
62 }
63 if (snprintf(sortout,sizeof(sortout),"%s/%s.sort",tmp,uinfo->filename)>=sizeof(sortout)) {
64 debuga(_("File name too long: %s/%s.sort\n"),tmp,uinfo->filename);
65 exit(EXIT_FAILURE);
66 }
67
68 sprintf(csort,"sort -k 1,1 -k 2,2 -o \"%s\" \"%s\"",sortout,wdirname);
69 cstatus=system(csort);
70 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
71 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
72 debuga(_("sort command: %s\n"),csort);
73 exit(EXIT_FAILURE);
74 }
75 if((fp_in=fopen(sortout,"r"))==NULL) {
76 debuga(_("(totday) Cannot open log file %s\n"),sortout);
77 debuga(_("sort command: %s\n"),csort);
78 exit(EXIT_FAILURE);
79 }
80
81 unlink(wdirname);
82
83 if((fp_ou=fopen(arqout,"w"))==NULL) {
84 debuga(_("(totday) Cannot open log file %s\n"),arqout);
85 exit(EXIT_FAILURE);
86 }
87
88 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
89 fixendofline(buf);
90 getword_start(&gwarea,buf);
91 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,':')<0 ||
92 getword(min,sizeof(min),&gwarea,':')<0 || getword_skip(20,&gwarea,'\t')<0 ||
93 getword(elap,sizeof(elap),&gwarea,0)<0) {
94 debuga(_("There is a broken record or garbage in file %s\n"),sortout);
95 exit(EXIT_FAILURE);
96 }
97
98 if(!regs) {
99 strcpy(odata,data);
100 strcpy(ohora,hora);
101 strcpy(oelap,elap);
102 regs++;
103 }
104
105 if(strcmp(hora,ohora) != 0 || strcmp(data,odata) != 0) {
106 fprintf(fp_ou,"%s\t%s\t%015lld\n",odata,ohora,telap);
107 strcpy(odata,data);
108 strcpy(ohora,hora);
109 telap=0;
110 }
111
112 telap+=my_atoll(elap);
113 }
114
115 fprintf(fp_ou,"%s\t%s\t%015lld\n",data,hora,telap);
116
117 fclose(fp_in);
118 if (fclose(fp_ou)==EOF) {
119 debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
120 exit(EXIT_FAILURE);
121 }
122
123 unlink(sortout);
124
125 return;
126
127 }