]> git.ipfire.org Git - thirdparty/sarg.git/blob - lastlog.c
All the sprintf/fputs have been replaced by fprintf to avoid an intermediary buffer...
[thirdparty/sarg.git] / lastlog.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
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(const char *outdir)
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 int cstatus;
42
43 if(strcmp(LastLog,"0") == 0)
44 return;
45
46 sprintf(temp,"%slastlog1",outdir);
47 if((fp_ou=fopen(temp,"w"))==NULL) {
48 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
49 exit(1);
50 }
51
52 dirp = opendir(outdir);
53 while ((direntp = readdir( dirp )) != NULL ){
54 if(strstr(direntp->d_name,"-") == 0)
55 continue;
56
57 sprintf(warea,"%s%s",outdir,direntp->d_name);
58 stat(warea,&statb);
59 t=statb.st_ctime;
60 local = localtime(&t);
61 strftime(ftime, 127, "%Y%m%d%H%M%S", local);
62 fprintf(fp_ou,"%s %s\n",ftime,direntp->d_name);
63 ftot++;
64 }
65
66 (void)rewinddir( dirp );
67 (void)closedir( dirp );
68 fclose(fp_ou);
69
70 sprintf(buf,"sort -n -k 1,1 -o '%slastlog' '%s'",outdir,temp);
71 cstatus=system(buf);
72 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
73 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
74 fprintf(stderr, "SARG: sort command: %s\n",buf);
75 exit(1);
76 }
77
78 unlink(temp);
79
80 if(ftot<=atoi(LastLog)) {
81 sprintf(temp,"%slastlog",outdir);
82 if(access(temp, R_OK) == 0)
83 unlink(temp);
84 return;
85 }
86
87 ftot-=atoi(LastLog);
88
89 sprintf(temp,"%slastlog",outdir);
90 if((fp_in=fopen(temp,"r"))==NULL) {
91 fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);
92 exit(1);
93 }
94
95 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
96 if (getword(warea,sizeof(warea),buf,' ')<0) {
97 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",temp);
98 exit(1);
99 }
100 buf[strlen(buf)-1]='\0';
101
102 if(ftot) {
103 if(debug) {
104 sprintf(msg,"%s: %s",text[81],buf);
105 debuga(msg);
106 }
107 // sprintf(temp,"%s%s",outdir,buf);
108 sprintf(temp,"rm -r %s%s",outdir,buf);
109 system(temp);
110 unlink(temp);
111 ftot--;
112 }
113 }
114
115 fclose(fp_in);
116 sprintf(temp,"%slastlog",outdir);
117 unlink(temp);
118
119 return;
120 }