[go: up one dir, main page]

Menu

[eb94e0]: / lastlog.c  Maximize  Restore  History

Download this file

130 lines (112 with data), 3.9 kB

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