]> git.ipfire.org Git - thirdparty/sarg.git/blame - sort.c
Don't use string comparison to sort the top users, top sites and user lists.
[thirdparty/sarg.git] / sort.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
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
32e71fa4 30void tmpsort(void)
25697a35
GS
31{
32
33 DIR *dirp;
34 struct dirent *direntp;
456d78a5 35 int cstatus;
32e71fa4
FM
36 const char tmpext[]=".utmp";
37 int dlen;
25697a35 38 char csort[MAXLEN];
32e71fa4 39 char arqou[MAXLEN], arqin[MAXLEN], wnome[MAXLEN];
8ef7a8b8
FM
40 const char *field1="2,2";
41 const char *field2="1,1";
42 const char *field3="3,3";
15d5372b 43 const char *order;
25697a35
GS
44
45 if(indexonly) return;
085c1e1f 46 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
25697a35 47
15d5372b 48 if((UserSort & USER_SORT_CONNECT) != 0) {
8ef7a8b8
FM
49 field1="1,1";
50 field2="2,2";
51 field3="3,3";
15d5372b 52 } else if((UserSort & USER_SORT_SITE) != 0) {
8ef7a8b8
FM
53 field1="3,3";
54 field2="2,2";
55 field3="1,1";
15d5372b 56 } else if((UserSort & USER_SORT_TIME) != 0) {
8ef7a8b8
FM
57 field1="5,5";
58 field2="2,2";
59 field3="1,1";
32e71fa4
FM
60 }
61
15d5372b 62 if((UserSort & USER_SORT_REVERSE) == 0)
8ef7a8b8 63 order="";
15d5372b
FM
64 else
65 order="-r";
25697a35 66
e21b6c02
FM
67 if ((dirp = opendir(tmp)) == NULL) {
68 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
69 exit(EXIT_FAILURE);
70 }
25697a35 71 while ((direntp = readdir( dirp )) != NULL ){
32e71fa4
FM
72 dlen=strlen(direntp->d_name)-(sizeof(tmpext)-1);
73 if (dlen<0) continue;
74 if(strcmp(direntp->d_name+dlen,tmpext) != 0)
25697a35
GS
75 continue;
76
32e71fa4
FM
77 if (dlen>0) {
78 if (dlen>=sizeof(wnome)) continue;
79 strncpy(wnome,direntp->d_name,dlen);
0a4e18e1 80 wnome[dlen]='\0';
32e71fa4
FM
81 } else {
82 wnome[0]='\0';
25697a35 83 }
5542cb59 84
25697a35
GS
85 strcpy(arqou,tmp);
86 strcat(arqou,"/");
87 strcpy(arqin,arqou);
88 strcat(arqou,wnome);
89 strcat(arqin,direntp->d_name);
90
91 if(debug) {
10210234 92 debuga(_("Sorting file: %s\n"),arqou);
25697a35
GS
93 }
94
25697a35 95 strcat(arqou,".txt");
4e302ff5 96 sprintf(csort,"sort -n -T \"%s\" %s -k %s -k %s -k %s -o \"%s\" \"%s\"",tmp,order,field1,field2,field3,arqou,arqin);
456d78a5
FM
97 cstatus=system(csort);
98 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
10210234
FM
99 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
100 debuga(_("sort command: %s\n"),csort);
06b39c87 101 exit(EXIT_FAILURE);
456d78a5 102 }
25697a35
GS
103 unlink(arqin);
104
105 }
106
25697a35
GS
107 (void)closedir( dirp );
108 return;
109}
110
32e71fa4 111void sort_users_log(const char *tmp, int debug)
25697a35 112{
25697a35
GS
113 DIR *dirp;
114 struct dirent *direntp;
115 char csort[MAXLEN];
32e71fa4 116 char user[MAXLEN];
25697a35 117 char wdname[MAXLEN];
456d78a5 118 int cstatus;
32e71fa4 119 int dlen;
8ef7a8b8
FM
120 int clen;
121 const char unsortext[]=".unsort";
25697a35 122
d6e703cc 123 if(debug) {
10210234 124 debuga(_("pre-sorting files\n"));
d6e703cc
FM
125 }
126
4e302ff5
FM
127 if ((dirp = opendir(tmp)) == NULL) {
128 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
e21b6c02
FM
129 exit(EXIT_FAILURE);
130 }
25697a35 131 while ( (direntp = readdir( dirp )) != NULL ){
32e71fa4
FM
132 dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1);
133 if (dlen<0) continue;
134 if(strcmp(direntp->d_name+dlen,unsortext) != 0)
25697a35 135 continue;
32e71fa4 136 if(strcmp(direntp->d_name,"authfail.log.unsort") == 0)
491b862f
GS
137 continue;
138
32e71fa4
FM
139 if (dlen>0) {
140 if (dlen>=sizeof(user)) continue;
141 strncpy(user,direntp->d_name,dlen);
142 user[dlen]=0;
143 } else {
8ef7a8b8 144 user[0]='\0';
25697a35
GS
145 }
146
147 if(strcmp(direntp->d_name,"download.unsort") == 0)
8ef7a8b8 148 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s/%s.log\" \"%s/%s.unsort\"",
4e302ff5 149 tmp, tmp, user, tmp, user);
25697a35 150 else
f2ec8c75 151 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"",
4e302ff5 152 tmp, tmp, user, tmp, user);
8ef7a8b8 153 if (clen>=sizeof(csort)) {
fcdc0918 154 debuga(_("user name too long to sort %s\n"),csort);
06b39c87 155 exit(EXIT_FAILURE);
8ef7a8b8 156 }
456d78a5
FM
157 cstatus=system(csort);
158 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
fcdc0918
FM
159 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
160 debuga(_("sort command: %s\n"),csort);
06b39c87 161 exit(EXIT_FAILURE);
456d78a5 162 }
4e302ff5
FM
163 if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",tmp,user)>=sizeof(wdname)) {
164 debuga(_("user name too long for %s/%s.unsort\n"),tmp,user);
06b39c87 165 exit(EXIT_FAILURE);
32e71fa4 166 }
25697a35 167 unlink(wdname);
25697a35 168 }
25697a35
GS
169 (void)closedir( dirp );
170
171 return;
172}
15d5372b
FM
173
174void sort_labels(const char **label,const char **order)
175{
176 if((UserSort & USER_SORT_CONNECT) != 0) {
177 *label=_("connect");
178 } else if((UserSort & USER_SORT_SITE) != 0) {
179 *label=_("site");
180 } else if((UserSort & USER_SORT_TIME) != 0) {
181 *label=_("time");
182 } else {
183 *label=_("bytes");
184 }
185
186 if((UserSort & USER_SORT_REVERSE) == 0)
187 *order=_("normal");
188 else
189 *order=_("reverse");
190}