]> git.ipfire.org Git - thirdparty/sarg.git/blame - lastlog.c
Fix the creation of a report for only one user with command line option -u
[thirdparty/sarg.git] / lastlog.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
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 mklastlog(const char *outdir)
25697a35
GS
31{
32
33 FILE *fp_in, *fp_ou;
34 DIR *dirp;
35 struct dirent *direntp;
06b39c87 36 char buf[MAXLEN];
25697a35
GS
37 char temp[MAXLEN];
38 char warea[MAXLEN];
39 char ftime[128];
40 int ftot=0;
41 time_t t;
42 struct tm *local;
43 struct stat statb;
456d78a5 44 int cstatus;
9c7c6346 45 struct getwordstruct gwarea;
25697a35 46
0511cf2d 47 if(LastLog <= 0)
25697a35
GS
48 return;
49
e21b6c02 50 snprintf(temp,sizeof(temp),"%slastlog1",outdir);
25697a35 51 if((fp_ou=fopen(temp,"w"))==NULL) {
d574e592 52 debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
06b39c87 53 exit(EXIT_FAILURE);
25697a35
GS
54 }
55
e21b6c02
FM
56 if ((dirp = opendir(outdir)) == NULL) {
57 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
58 exit(EXIT_FAILURE);
59 }
25697a35 60 while ((direntp = readdir( dirp )) != NULL ){
9c7c6346 61 if(strchr(direntp->d_name,'-') == 0)
25697a35
GS
62 continue;
63
e21b6c02
FM
64 snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name);
65 if (stat(warea,&statb) == -1) {
66 debuga(_("Failed to get the creation time of %s\n"),warea);
67 continue;
68 }
25697a35
GS
69 t=statb.st_ctime;
70 local = localtime(&t);
120d768c
FM
71 strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
72 fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name);
25697a35
GS
73 ftot++;
74 }
75
06b39c87 76 closedir( dirp );
25697a35 77 fclose(fp_ou);
120d768c 78
06b39c87 79 snprintf(buf,sizeof(buf),"sort -n -k 1,1 -o \"%slastlog\" \"%s\"",outdir,temp);
456d78a5
FM
80 cstatus=system(buf);
81 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
d574e592
FM
82 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
83 debuga(_("sort command: %s\n"),buf);
06b39c87 84 exit(EXIT_FAILURE);
456d78a5 85 }
25697a35
GS
86
87 unlink(temp);
88
0511cf2d 89 if(ftot<=LastLog) {
e21b6c02 90 snprintf(temp,sizeof(temp),"%slastlog",outdir);
25697a35
GS
91 if(access(temp, R_OK) == 0)
92 unlink(temp);
93 return;
94 }
95
0511cf2d 96 ftot-=LastLog;
25697a35 97
e21b6c02 98 snprintf(temp,sizeof(temp),"%slastlog",outdir);
25697a35 99 if((fp_in=fopen(temp,"r"))==NULL) {
d574e592 100 debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
06b39c87 101 exit(EXIT_FAILURE);
25697a35
GS
102 }
103
9c7c6346
FM
104 while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) {
105 fixendofline(buf);
106 getword_start(&gwarea,buf);
107 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
d574e592 108 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),temp);
06b39c87 109 exit(EXIT_FAILURE);
4bcb77cf 110 }
120d768c 111
9c7c6346 112 if(debug)
d574e592 113 debuga(_("Removing old report file %s\n"),gwarea.current);
51465d08 114 if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) {
d574e592 115 debuga(_("Directory name too long: %s%s\n"),outdir,gwarea.current);
06b39c87 116 exit(EXIT_FAILURE);
9c7c6346 117 }
51465d08 118 unlinkdir(temp,0);
9c7c6346 119 ftot--;
25697a35
GS
120 }
121
122 fclose(fp_in);
e21b6c02
FM
123 snprintf(temp,sizeof(temp),"%slastlog",outdir);
124 if (unlink(temp) == -1) {
125 debuga(_("Failed to delete the file %s\n"),temp);
126 }
120d768c 127
25697a35
GS
128 return;
129}