]> git.ipfire.org Git - thirdparty/sarg.git/blame - sort.c
Use a function to safely copy the strings
[thirdparty/sarg.git] / sort.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
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 31{
93551487
FM
32 userscan uscan;
33 struct userinfostruct *uinfo;
9bd92830 34 int cstatus;
9bd92830 35 char csort[MAXLEN];
93551487 36 char arqou[MAXLEN], arqin[MAXLEN];
9bd92830
FM
37 const char *field1="2,2";
38 const char *field2="1,1";
39 const char *field3="3,3";
40 const char *order;
41
9bd92830
FM
42 if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return;
43
44 if((UserSort & USER_SORT_CONNECT) != 0) {
45 field1="1,1";
46 field2="2,2";
47 field3="3,3";
48 } else if((UserSort & USER_SORT_SITE) != 0) {
49 field1="3,3";
50 field2="2,2";
51 field3="1,1";
52 } else if((UserSort & USER_SORT_TIME) != 0) {
53 field1="5,5";
54 field2="2,2";
55 field3="1,1";
56 }
57
58 if((UserSort & USER_SORT_REVERSE) == 0)
59 order="";
60 else
61 order="-r";
62
93551487
FM
63 uscan=userinfo_startscan();
64 if (uscan == NULL) {
65 debuga(_("Cannot enumerate the user list\n"));
9bd92830
FM
66 exit(EXIT_FAILURE);
67 }
93551487
FM
68 while ((uinfo = userinfo_advancescan(uscan)) != NULL ){
69 if (snprintf(arqin,sizeof(arqin),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(arqin)) {
70 debuga(_("file name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
71 exit(EXIT_FAILURE);
72 }
73 if (snprintf(arqou,sizeof(arqou),"%s/%s.txt",tmp,uinfo->filename)>=sizeof(arqou)) {
74 debuga(_("file name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
75 exit(EXIT_FAILURE);
9bd92830 76 }
9bd92830
FM
77
78 if(debug) {
79 debuga(_("Sorting file: %s\n"),arqou);
80 }
81
78eeb33f
FM
82 if (snprintf(csort,sizeof(csort),"sort -n -T \"%s\" -t \"\t\" %s -k %s -k %s -k %s -o \"%s\" \"%s\"",tmp,order,field1,field2,field3,arqou,arqin)>=sizeof(csort)) {
83 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),arqin,arqou);
84 exit(EXIT_FAILURE);
85 }
9bd92830
FM
86 cstatus=system(csort);
87 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
88 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
89 debuga(_("sort command: %s\n"),csort);
90 exit(EXIT_FAILURE);
91 }
08f9b029
FM
92 if (unlink(arqin)) {
93 debuga(_("Cannot delete %s - %s\n"),arqin,strerror(errno));
94 exit(EXIT_FAILURE);
95 }
9bd92830
FM
96 }
97
93551487 98 userinfo_stopscan(uscan);
9bd92830 99 return;
25697a35
GS
100}
101
32e71fa4 102void sort_users_log(const char *tmp, int debug)
25697a35 103{
9bd92830
FM
104 DIR *dirp;
105 struct dirent *direntp;
106 char csort[MAXLEN];
107 char user[MAXLEN];
108 char wdname[MAXLEN];
109 int cstatus;
110 int dlen;
111 int clen;
112 const char unsortext[]=".unsort";
113
114 if(debug) {
115 debuga(_("pre-sorting files\n"));
116 }
117
118 if ((dirp = opendir(tmp)) == NULL) {
119 debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
120 exit(EXIT_FAILURE);
121 }
122 while ( (direntp = readdir( dirp )) != NULL ){
123 dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1);
124 if (dlen<0) continue;
125 if(strcmp(direntp->d_name+dlen,unsortext) != 0)
126 continue;
127 if(strcmp(direntp->d_name,"authfail.log.unsort") == 0)
128 continue;
129
130 if (dlen>0) {
131 if (dlen>=sizeof(user)) continue;
a87d4d11 132 safe_strcpy(user,direntp->d_name,dlen+1);
9bd92830
FM
133 } else {
134 user[0]='\0';
135 }
136
137 if(strcmp(direntp->d_name,"download.unsort") == 0)
eb843369 138 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s/%s.log\" \"%s/%s.unsort\"",
007905af 139 tmp, tmp, user, tmp, user);
9bd92830 140 else
eb843369 141 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"",
007905af 142 tmp, tmp, user, tmp, user);
9bd92830
FM
143 if (clen>=sizeof(csort)) {
144 debuga(_("user name too long to sort %s\n"),csort);
145 exit(EXIT_FAILURE);
146 }
147 cstatus=system(csort);
148 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
149 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
150 debuga(_("sort command: %s\n"),csort);
151 exit(EXIT_FAILURE);
152 }
153 if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",tmp,user)>=sizeof(wdname)) {
154 debuga(_("user name too long for %s/%s.unsort\n"),tmp,user);
155 exit(EXIT_FAILURE);
156 }
08f9b029
FM
157 if (unlink(wdname)) {
158 debuga(_("Cannot delete %s - %s\n"),wdname,strerror(errno));
159 exit(EXIT_FAILURE);
160 }
9bd92830
FM
161 }
162 (void)closedir( dirp );
163
164 return;
25697a35 165}
15d5372b
FM
166
167void sort_labels(const char **label,const char **order)
168{
9bd92830
FM
169 if((UserSort & USER_SORT_CONNECT) != 0) {
170 *label=_("connect");
171 } else if((UserSort & USER_SORT_SITE) != 0) {
172 *label=_("site");
173 } else if((UserSort & USER_SORT_TIME) != 0) {
174 *label=_("time");
175 } else {
176 *label=_("bytes");
177 }
178
179 if((UserSort & USER_SORT_REVERSE) == 0)
180 *order=_("normal");
181 else
182 *order=_("reverse");
15d5372b 183}