]> git.ipfire.org Git - thirdparty/sarg.git/blame - sort.c
Rewrite the function to format numbers with a suffix KMGT
[thirdparty/sarg.git] / sort.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
67302a9e 3 * 1998, 2013
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
461b479d
FM
30/*!
31Sort all the \c utmp files form the temporary directory. The sort can be made according to the
32number of connections, the accessed sites or the time of the access depending on the value of
33::UserSortField. The sorting is either made in increasing or decreasing order as specified by
34the value of ::UserSortOrder.
35*/
e5b3a129 36void tmpsort(const struct userinfostruct *uinfo)
25697a35 37{
9bd92830 38 int cstatus;
9bd92830 39 char csort[MAXLEN];
93551487 40 char arqou[MAXLEN], arqin[MAXLEN];
9bd92830
FM
41 const char *field1="2,2";
42 const char *field2="1,1";
43 const char *field3="3,3";
44 const char *order;
45
9bd92830
FM
46 if((UserSort & USER_SORT_CONNECT) != 0) {
47 field1="1,1";
48 field2="2,2";
49 field3="3,3";
50 } else if((UserSort & USER_SORT_SITE) != 0) {
51 field1="3,3";
52 field2="2,2";
53 field3="1,1";
54 } else if((UserSort & USER_SORT_TIME) != 0) {
55 field1="5,5";
56 field2="2,2";
57 field3="1,1";
58 }
59
60 if((UserSort & USER_SORT_REVERSE) == 0)
61 order="";
62 else
63 order="-r";
64
e5b3a129 65 if (snprintf(arqin,sizeof(arqin),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(arqin)) {
4bb93ba7 66 debuga(_("File name too long: %s/%s.utmp\n"),tmp,uinfo->filename);
e5b3a129
FM
67 exit(EXIT_FAILURE);
68 }
69 if (snprintf(arqou,sizeof(arqou),"%s/htmlrel.txt",tmp)>=sizeof(arqou)) {
4bb93ba7 70 debuga(_("File name too long: %s/htmlrel.txt\n"),tmp);
9bd92830
FM
71 exit(EXIT_FAILURE);
72 }
9bd92830 73
e5b3a129
FM
74 if(debug) {
75 debuga(_("Sorting file: %s\n"),arqin);
76 }
9bd92830 77
e5b3a129
FM
78 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)) {
79 debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),arqin,arqou);
80 exit(EXIT_FAILURE);
81 }
82 cstatus=system(csort);
83 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
84 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
85 debuga(_("sort command: %s\n"),csort);
86 exit(EXIT_FAILURE);
87 }
11767c6a
FM
88 if (!KeepTempLog && unlink(arqin)) {
89 debuga(_("Cannot delete \"%s\": %s\n"),arqin,strerror(errno));
e5b3a129 90 exit(EXIT_FAILURE);
9bd92830
FM
91 }
92
9bd92830 93 return;
25697a35
GS
94}
95
461b479d
FM
96/*!
97The function sorts the \c unsort file in the temporary directory. These files correspond
98to the format described in \ref UserUnsortLog.
99
100\param tmp The temorary directory of the sarg files.
101\param debug \c True to output debug information.
102\param uinfo The user whose log must be sorted.
103
104The user's files are sorted by columns 5, 1 and 2 that are the columns of the number of bytes transfered,
105the date of the access and the time of the access.
106
107The sorted files are written in files with the extension \c log and the name of the unsorted
108file without the \c unsort extension. The unsorted file is deleted just after the sorting.
109*/
110void sort_users_log(const char *tmp, int debug,struct userinfostruct *uinfo)
25697a35 111{
9bd92830 112 char csort[MAXLEN];
461b479d 113 const char *user;
9bd92830 114 int cstatus;
9bd92830 115 int clen;
9bd92830
FM
116
117 if(debug) {
170a77ea 118 debuga(_("Sorting log %s/%s.user_unsort\n"),tmp,uinfo->filename);
9bd92830
FM
119 }
120
461b479d 121 user=uinfo->filename;
170a77ea 122 clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.user_log\" \"%s/%s.user_unsort\"",
461b479d
FM
123 tmp, tmp, user, tmp, user);
124 if (clen>=sizeof(csort)) {
125 debuga(_("user name too long to sort %s\n"),csort);
9bd92830
FM
126 exit(EXIT_FAILURE);
127 }
461b479d
FM
128 cstatus=system(csort);
129 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
130 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
131 debuga(_("sort command: %s\n"),csort);
132 exit(EXIT_FAILURE);
133 }
170a77ea
FM
134 if (snprintf(csort,sizeof(csort),"%s/%s.user_unsort",tmp,user)>=sizeof(csort)) {
135 debuga(_("user name too long for %s/%s.user_unsort\n"),tmp,user);
461b479d
FM
136 exit(EXIT_FAILURE);
137 }
11767c6a
FM
138 if (!KeepTempLog && unlink(csort)) {
139 debuga(_("Cannot delete \"%s\": %s\n"),csort,strerror(errno));
461b479d 140 exit(EXIT_FAILURE);
9bd92830 141 }
9bd92830
FM
142
143 return;
25697a35 144}
15d5372b 145
461b479d
FM
146/*!
147Get the internationalized text to display when reporting the sort criterion and order
148of a user list.
149
150\param label A pointer to set to the string of the sort criterion name.
151\param order A pointer to set to the string of the sort order name
152*/
15d5372b
FM
153void sort_labels(const char **label,const char **order)
154{
9bd92830
FM
155 if((UserSort & USER_SORT_CONNECT) != 0) {
156 *label=_("connect");
157 } else if((UserSort & USER_SORT_SITE) != 0) {
158 *label=_("site");
159 } else if((UserSort & USER_SORT_TIME) != 0) {
160 *label=_("time");
161 } else {
162 *label=_("bytes");
163 }
164
165 if((UserSort & USER_SORT_REVERSE) == 0)
166 *order=_("normal");
167 else
168 *order=_("reverse");
15d5372b 169}