]> git.ipfire.org Git - thirdparty/sarg.git/blob - sort.c
Update the messages
[thirdparty/sarg.git] / sort.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 tmpsort(void)
31 {
32
33 DIR *dirp;
34 struct dirent *direntp;
35 int cstatus;
36 const char tmpext[]=".utmp";
37 int dlen;
38 char csort[MAXLEN];
39 char arqou[MAXLEN], arqin[MAXLEN], wnome[MAXLEN];
40 const char *field1="2,2";
41 const char *field2="1,1";
42 const char *field3="3,3";
43 const char *order="-r";
44
45 if(indexonly) return;
46 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
47
48 strup(UserSortField);
49 if(strcmp(UserSortField,"CONNECT") == 0) {
50 field1="1,1";
51 field2="2,2";
52 field3="3,3";
53 } else if(strcmp(UserSortField,"SITE") == 0) {
54 field1="3,3";
55 field2="2,2";
56 field3="1,1";
57 } else if(strcmp(UserSortField,"TIME") == 0) {
58 field1="5,5";
59 field2="2,2";
60 field3="1,1";
61 }
62
63 strlow(UserSortOrder);
64 if(strcmp(UserSortOrder,"normal") == 0)
65 order="";
66
67 if ((dirp = opendir(tmp)) == NULL) {
68 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
69 exit(EXIT_FAILURE);
70 }
71 while ((direntp = readdir( dirp )) != NULL ){
72 dlen=strlen(direntp->d_name)-(sizeof(tmpext)-1);
73 if (dlen<0) continue;
74 if(strcmp(direntp->d_name+dlen,tmpext) != 0)
75 continue;
76
77 if (dlen>0) {
78 if (dlen>=sizeof(wnome)) continue;
79 strncpy(wnome,direntp->d_name,dlen);
80 wnome[dlen]='\0';
81 } else {
82 wnome[0]='\0';
83 }
84
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) {
92 debuga(_("Sorting file: %s\n"),arqou);
93 }
94
95 strcat(arqou,".txt");
96 sprintf(csort,"sort -n -T \"%s\" %s -k %s -k %s -k %s -o \"%s\" \"%s\"",TempDir,order,field1,field2,field3,arqou,arqin);
97 cstatus=system(csort);
98 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
99 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
100 debuga(_("sort command: %s\n"),csort);
101 exit(EXIT_FAILURE);
102 }
103 unlink(arqin);
104
105 }
106
107 (void)closedir( dirp );
108 return;
109 }
110
111 void sort_users_log(const char *tmp, int debug)
112 {
113 DIR *dirp;
114 struct dirent *direntp;
115 char csort[MAXLEN];
116 char wtmp[MAXLEN];
117 char user[MAXLEN];
118 char wdname[MAXLEN];
119 int cstatus;
120 int dlen;
121 int clen;
122 const char unsortext[]=".unsort";
123
124 if(debug) {
125 debuga(_("pre-sorting files\n"));
126 }
127
128 snprintf(wtmp,sizeof(wtmp),"%s/sarg",tmp);
129
130 if ((dirp = opendir(wtmp)) == NULL) {
131 debuga(_("Failed to open directory %s - %s\n"),wtmp,strerror(errno));
132 exit(EXIT_FAILURE);
133 }
134 while ( (direntp = readdir( dirp )) != NULL ){
135 dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1);
136 if (dlen<0) continue;
137 if(strcmp(direntp->d_name+dlen,unsortext) != 0)
138 continue;
139 if(strcmp(direntp->d_name,"authfail.log.unsort") == 0)
140 continue;
141
142 if (dlen>0) {
143 if (dlen>=sizeof(user)) continue;
144 strncpy(user,direntp->d_name,dlen);
145 user[dlen]=0;
146 } else {
147 user[0]='\0';
148 }
149
150 if(strcmp(direntp->d_name,"download.unsort") == 0)
151 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\"",
152 tmp, wtmp, user, wtmp, user);
153 else
154 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"",
155 tmp, wtmp, user, wtmp, user);
156 if (clen>=sizeof(csort)) {
157 debuga(_("user name too long to sort %s\n"),csort);
158 exit(EXIT_FAILURE);
159 }
160 cstatus=system(csort);
161 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
162 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
163 debuga(_("sort command: %s\n"),csort);
164 exit(EXIT_FAILURE);
165 }
166 if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",wtmp,user)>=sizeof(wdname)) {
167 debuga(_("user name too long for %s/%s.unsort\n"),wtmp,user);
168 exit(EXIT_FAILURE);
169 }
170 unlink(wdname);
171 }
172 (void)closedir( dirp );
173
174 return;
175 }