]> git.ipfire.org Git - thirdparty/sarg.git/blame - lastlog.c
Fix HTML output
[thirdparty/sarg.git] / lastlog.c
CommitLineData
25697a35 1/*
c37945ed 2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
a6e5c172 3 * 1998, 2010
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26#include "include/conf.h"
5f3cfd1d 27#include "include/defs.h"
25697a35 28
32e71fa4 29void mklastlog(const char *outdir)
25697a35
GS
30{
31
32 FILE *fp_in, *fp_ou;
33 DIR *dirp;
34 struct dirent *direntp;
35 char temp[MAXLEN];
36 char warea[MAXLEN];
37 char ftime[128];
38 int ftot=0;
39 time_t t;
40 struct tm *local;
41 struct stat statb;
456d78a5 42 int cstatus;
9c7c6346 43 struct getwordstruct gwarea;
25697a35 44
0511cf2d 45 if(LastLog <= 0)
25697a35
GS
46 return;
47
48 sprintf(temp,"%slastlog1",outdir);
49 if((fp_ou=fopen(temp,"w"))==NULL) {
120d768c 50 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
25697a35
GS
51 exit(1);
52 }
53
54 dirp = opendir(outdir);
55 while ((direntp = readdir( dirp )) != NULL ){
9c7c6346 56 if(strchr(direntp->d_name,'-') == 0)
25697a35
GS
57 continue;
58
59 sprintf(warea,"%s%s",outdir,direntp->d_name);
60 stat(warea,&statb);
61 t=statb.st_ctime;
62 local = localtime(&t);
120d768c
FM
63 strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
64 fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name);
25697a35
GS
65 ftot++;
66 }
67
25697a35
GS
68 (void)closedir( dirp );
69 fclose(fp_ou);
120d768c 70
9a2efbd0 71 sprintf(buf,"sort -n -k 1,1 -o \"%slastlog\" \"%s\"",outdir,temp);
456d78a5
FM
72 cstatus=system(buf);
73 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
74 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
75 fprintf(stderr, "SARG: sort command: %s\n",buf);
76 exit(1);
77 }
25697a35
GS
78
79 unlink(temp);
80
0511cf2d 81 if(ftot<=LastLog) {
25697a35
GS
82 sprintf(temp,"%slastlog",outdir);
83 if(access(temp, R_OK) == 0)
84 unlink(temp);
85 return;
86 }
87
0511cf2d 88 ftot-=LastLog;
25697a35
GS
89
90 sprintf(temp,"%slastlog",outdir);
91 if((fp_in=fopen(temp,"r"))==NULL) {
120d768c 92 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
25697a35
GS
93 exit(1);
94 }
95
9c7c6346
FM
96 while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) {
97 fixendofline(buf);
98 getword_start(&gwarea,buf);
99 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
4bcb77cf
FM
100 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",temp);
101 exit(1);
102 }
120d768c 103
9c7c6346
FM
104 if(debug)
105 debuga("%s: %s",text[81],gwarea.current);
51465d08 106 if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) {
9c7c6346
FM
107 fprintf(stderr,"SARG: Directory name too long: %s%s\n",outdir,gwarea.current);
108 exit(1);
109 }
51465d08 110 unlinkdir(temp,0);
9c7c6346 111 ftot--;
25697a35
GS
112 }
113
114 fclose(fp_in);
115 sprintf(temp,"%slastlog",outdir);
116 unlink(temp);
120d768c 117
25697a35
GS
118 return;
119}