home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / sarg1221.zip / lastlog.c < prev    next >
C/C++ Source or Header  |  2002-10-08  |  2KB  |  90 lines

  1. /*
  2.  * sarg - Squid user management log
  3.  * Mar/98 - Pedro L Orso - orso@onda.com.br
  4.  */
  5.  
  6. #include "include/conf.h"
  7.  
  8. void mklastlog(char *outdir, int debug)
  9. {
  10.  
  11.    FILE *fp_in, *fp_ou;
  12.    DIR *dirp;
  13.    struct dirent *direntp;
  14.    char temp[MAXLEN];
  15.    char warea[MAXLEN];
  16.    char ftime[128];
  17.    int  ftot=0;
  18.    time_t t;
  19.    struct tm *local;
  20.    struct stat statb;
  21.  
  22.    if(strcmp(LastLog,"0") == 0)
  23.       return;
  24.  
  25.    sprintf(temp,"%slastlog1",outdir);
  26.    if((fp_ou=fopen(temp,"w"))==NULL) {
  27.      fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);        
  28.      exit(1);
  29.    }
  30.  
  31.    dirp = opendir(outdir);
  32.    while ((direntp = readdir( dirp )) != NULL ){
  33.       if(strstr(direntp->d_name,"-") == 0)
  34.          continue;
  35.  
  36.       sprintf(warea,"%s%s",outdir,direntp->d_name);
  37.       stat(warea,&statb);
  38.       t=statb.st_ctime;
  39.       local = localtime(&t);
  40.       strftime(ftime, 127, "%Y%m%d%H%M%S", local);
  41.       sprintf(buf,"%s %s\n",ftime,direntp->d_name);
  42.       fputs(buf,fp_ou);
  43.       ftot++;
  44.    }
  45.  
  46.    (void)closedir( dirp );
  47.    fclose(fp_ou);
  48.    
  49.    sprintf(buf, SORT" -n -k 1,1 -o %slastlog %s",outdir,temp);
  50.    system(buf);
  51.  
  52.    unlink(temp);
  53.  
  54.    if(ftot<=atoi(LastLog)) {
  55.       sprintf(temp,"%slastlog",outdir);
  56.       if(access(temp, R_OK) == 0)
  57.          unlink(temp);
  58.       return;
  59.    }
  60.  
  61.    ftot-=atoi(LastLog);
  62.  
  63.    sprintf(temp,"%slastlog",outdir);
  64.    if((fp_in=fopen(temp,"r"))==NULL) {
  65.      fprintf(stderr, "SARG: (lastlog) %s: %s\n",text[9],temp);        
  66.      exit(1);
  67.    }
  68.  
  69.    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
  70.       getword(warea,buf,' ');
  71.       buf[strlen(buf)-1]='\0';
  72.      
  73.       if(ftot) {
  74.          if(debug) {
  75.             sprintf(msg,"%s: %s",text[81],buf);
  76.             debuga(msg);
  77.          }
  78.          sprintf(temp, RM" -r %s%s",outdir,buf);
  79.          system(temp);
  80.          ftot--;
  81.       }
  82.    }
  83.  
  84.    fclose(fp_in);
  85.    sprintf(temp,"%slastlog",outdir);
  86.    unlink(temp);
  87.  
  88.    return;
  89. }
  90.