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