+ int cstatus;
+ char csort[MAXLEN];
+ char arqou[MAXLEN], arqin[MAXLEN];
+ const char *field1="2,2";
+ const char *field2="1,1";
+ const char *field3="3,3";
+ const char *order;
+
+ if((UserSort & USER_SORT_CONNECT) != 0) {
+ field1="1,1";
+ field2="2,2";
+ field3="3,3";
+ } else if((UserSort & USER_SORT_SITE) != 0) {
+ field1="3,3";
+ field2="2,2";
+ field3="1,1";
+ } else if((UserSort & USER_SORT_TIME) != 0) {
+ field1="5,5";
+ field2="2,2";
+ field3="1,1";
+ }
+
+ if((UserSort & USER_SORT_REVERSE) == 0)
+ order="";
+ else
+ order="-r";
+
+ if (snprintf(arqin,sizeof(arqin),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(arqin)) {
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/%s.utmp\n",tmp,uinfo->filename);
+ exit(EXIT_FAILURE);
+ }
+ if (snprintf(arqou,sizeof(arqou),"%s/htmlrel.txt",tmp)>=sizeof(arqou)) {
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/htmlrel.txt\n",tmp);
+ exit(EXIT_FAILURE);
+ }
+
+ if(debug) {
+ debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),arqin);
+ }
+
+ 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)) {
+ debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),arqin,arqou);
+ exit(EXIT_FAILURE);
+ }
+ cstatus=system(csort);
+ if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+ debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+ debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
+ exit(EXIT_FAILURE);
+ }
+ if (!KeepTempLog && unlink(arqin)) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),arqin,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ return;
+}
+
+/*!
+The function sorts the \c unsort file in the temporary directory. These files correspond
+to the format described in \ref UserUnsortLog.
+
+\param tmp The temorary directory of the sarg files.
+\param debug \c True to output debug information.
+\param uinfo The user whose log must be sorted.