]> git.ipfire.org Git - thirdparty/sarg.git/blob - lastlog.c
Imported Sarg 2.1.0
[thirdparty/sarg.git] / lastlog.c
1 /*
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
3 * 1998, 2005
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
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"
27
28 void mklastlog()
29 {
30
31 FILE *fp_in, *fp_ou;
32 DIR *dirp;
33 struct dirent *direntp;
34 char temp[MAXLEN];
35 char warea[MAXLEN];
36 char ftime[128];
37 int ftot=0;
38 time_t t;
39 struct tm *local;
40 struct stat statb;
41
42 if(strcmp(LastLog,"0") == 0)
43 return;
44
45 sprintf(temp,"%slastlog1",outdir);
46 if((fp_ou=fopen(temp,"w"))==NULL) {
47 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
48 exit(1);
49 }
50
51 dirp = opendir(outdir);
52 while ((direntp = readdir( dirp )) != NULL ){
53 if(strstr(direntp->d_name,"-") == 0)
54 continue;
55
56 sprintf(warea,"%s%s",outdir,direntp->d_name);
57 stat(warea,&statb);
58 t=statb.st_ctime;
59 local = localtime(&t);
60 strftime(ftime, 127, "%Y%m%d%H%M%S", local);
61 sprintf(buf,"%s %s\n",ftime,direntp->d_name);
62 printf("BUF=%s\n",buf);
63 fputs(buf,fp_ou);
64 ftot++;
65 }
66 exit(0);
67
68 (void)rewinddir( dirp );
69 (void)closedir( dirp );
70 fclose(fp_ou);
71
72 sprintf(buf,"sort -n -k 1,1 -o '%slastlog' '%s'",outdir,temp);
73 system(buf);
74
75 unlink(temp);
76
77 if(ftot<=atoi(LastLog)) {
78 sprintf(temp,"%slastlog",outdir);
79 if(access(temp, R_OK) == 0)
80 unlink(temp);
81 return;
82 }
83
84 ftot-=atoi(LastLog);
85
86 sprintf(temp,"%slastlog",outdir);
87 if((fp_in=fopen(temp,"r"))==NULL) {
88 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
89 exit(1);
90 }
91
92 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
93 getword(warea,buf,' ');
94 buf[strlen(buf)-1]='\0';
95
96 if(ftot) {
97 if(debug) {
98 sprintf(msg,"%s: %s",text[81],buf);
99 debuga(msg);
100 }
101 // sprintf(temp,"%s%s",outdir,buf);
102 sprintf(temp,"rm -r %s%s",outdir,buf);
103 system(temp);
104 unlink(temp);
105 ftot--;
106 }
107 }
108
109 fclose(fp_in);
110 sprintf(temp,"%slastlog",outdir);
111 unlink(temp);
112
113 return;
114 }