Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #define FILENAME "flashpolicyd.log"
- #define BACKUPNAME "fplogbackup"
- void backupFile(unsigned);
- unsigned checkTime(void);
- int main(void) {
- unsigned currentTime = checkTime();
- unsigned newTime = currentTime;
- FILE * fileHandle;
- while (1) {
- newTime = checkTime();
- if (newTime != currentTime) {
- backupFile(currentTime);
- currentTime = newTime;
- fileHandle = fopen(FILENAME, "w");
- if (fileHandle) fclose(fileHandle);
- }
- sleep(900);
- }
- return 0;
- }
- unsigned checkTime (void) {
- struct tm timeData;
- timeData = *gmtime(time(NULL));
- unsigned timeValue;
- timeValue = timeData.tm_year * 100000 + timeData.tm_mon * 1000 + timeData.tm_mday * 10 + (timeData.tm_hour >> 3);
- return timeValue;
- }
- void backupFile (unsigned timestamp) {
- FILE * f1, f2;
- f1 = fopen(FILENAME, "r");
- char * newname = malloc(200);
- strcpy(newname, BACKUPNAME);
- char * number = malloc(200);
- sprintf(number, "%u", timestamp);
- strcat(newname, number);
- f2 = fopen(newname, "w");
- if (f1 && f2) {
- int newchar;
- while ((newchar = getc(f1)) != EOF) putc(newchar, f2);
- }
- if (f1) fclose(f1);
- if (f2) fclose(f2);
- free(newname);
- free(number);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement