#include <stdio.h>
#include <string.h>
#include <time.h>

void logtail(const char *infile, const char *outfile) {

     FILE *fpi = fopen(infile, "r");
     FILE *fpo = fopen(outfile, "w");
     if ((fpi == NULL) || (fpo == NULL)) {
        return;
    }

    char buffer[512];
    unsigned int count = 0;
    unsigned int tail = 0;

// no of lines to calculate the last 100 lines
    while (fgets(buffer, sizeof(buffer), fpi) != NULL)  {
        count++;
    }
// the final_scammer from yesterday line count stops here !
    if (strcmp(outfile,"/tmp/empty") == 0) {
        printf("%d", count);
    } else {

      rewind(fpi);
      if (count < 100) tail = 0;
      else tail = count - 100;
      count = 0;
      while (fgets(buffer, sizeof(buffer), fpi) != NULL)  {
          count++;
          if (count > tail) {
             fprintf(fpo,"%s", buffer);
          }
      }
    }
    fclose(fpi);
    fclose(fpo);

}


int main(void) {

  time_t now;
 struct tm  *ts;
 char yesterday[80];

 now = time(NULL);
 ts = localtime(&now);
 ts->tm_mday--;
 mktime(ts); /* Normalise ts */
 strftime(yesterday, sizeof(yesterday), "%d-%m-%Y", ts);

 char log[50]="/var/tmp/final_scammer.zone_";
 char out[]="/tmp/empty";
 strcat(log, yesterday);


 char log1[]="/var/log/iptables.log";
 char out1[]="/home/www/monitor/iptables.log";
 char log2[]="/var/log/mail.log";
 char out2[]="/home/www/monitor/mail.log";
 char log3[]="/var/log/apache2/error.log";
 char out3[]="/home/www/monitor/error.log";
 char log4[]="/var/log/auth.log";
 char out4[]="/home/www/monitor/auth.log";
 char log5[]="/etc/watchdog/watch.log";
 char out5[]="/home/www/monitor/watch.log";

 logtail(log,out);
 logtail(log1,out1);
 logtail(log2,out2);
 logtail(log3,out3);
 logtail(log4,out4);
 logtail(log5,out5);

  return 0;
}