Advertisement
aaaaaa123456789

Delete/backup logs (fixed)

Feb 15th, 2012
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define FILENAME "flashpolicyd.log"
  8. #define BACKUPNAME "fplogbackup"
  9.  
  10. void backupFile(unsigned);
  11. unsigned checkTime(void);
  12.  
  13. int main(void) {
  14.   unsigned currentTime = checkTime();
  15.   unsigned newTime = currentTime;
  16.   FILE * fileHandle;
  17.   while (1) {
  18.     newTime = checkTime();
  19.     if (newTime != currentTime) {
  20.       backupFile(currentTime);
  21.       currentTime = newTime;
  22.       fileHandle = fopen(FILENAME, "w");
  23.       if (fileHandle) fclose(fileHandle);
  24.     }
  25.     sleep(900);
  26.   }
  27.   return 0;
  28. }
  29.  
  30. unsigned checkTime (void) {
  31.   struct tm timeData;
  32.   timeData = *gmtime(time(NULL));
  33.   unsigned timeValue;
  34.   timeValue = timeData.tm_year * 100000 + timeData.tm_mon * 1000 + timeData.tm_mday * 10 + (timeData.tm_hour >> 3);
  35.   return timeValue;
  36. }
  37.  
  38. void backupFile (unsigned timestamp) {
  39.   FILE * f1, f2;
  40.   f1 = fopen(FILENAME, "r");
  41.   char * newname = malloc(200);
  42.   strcpy(newname, BACKUPNAME);
  43.   char * number = malloc(200);
  44.   sprintf(number, "%u", timestamp);
  45.   strcat(newname, number);
  46.   f2 = fopen(newname, "w");
  47.   if (f1 && f2) {
  48.     int newchar;
  49.     while ((newchar = getc(f1)) != EOF) putc(newchar, f2);
  50.   }
  51.   if (f1) fclose(f1);
  52.   if (f2) fclose(f2);
  53.   free(newname);
  54.   free(number);
  55. }
Advertisement
RAW Paste Data Copied
Advertisement