Last active
August 12, 2020 16:38
-
-
Save iggyvolz/29a495fe98685b935d05ae62293705c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#define _XOPEN_SOURCE 500 /* See feature_test_macros(7) */ | |
#include <ftw.h> | |
#include<stdio.h> | |
#include <string.h> | |
#include <sys/time.h> | |
#include <time.h> | |
#include <unistd.h> | |
struct timespec start; | |
int argc; | |
const char** argv; | |
// Adapted from timercmp for timespec | |
# define timespeccmp(a, b, CMP) \ | |
(((a)->tv_sec == (b)->tv_sec) \ | |
? ((a)->tv_nsec CMP (b)->tv_nsec) \ | |
: ((a)->tv_sec CMP (b)->tv_sec)) | |
#ifndef FTW_STOP | |
#define FTW_STOP 1 | |
#endif | |
#ifndef FTW_CONTINUE | |
#define FTW_CONTINUE 0 | |
#endif | |
int callback (const char* filename, const struct stat* status, int flag, struct FTW *__info) | |
{ | |
for(int i=2;i<argc;i++) { | |
if(strncmp(filename, argv[i], strlen(argv[i])) == 0) { | |
#ifdef FTW_SKIP_SUBTREE | |
return FTW_SKIP_SUBTREE; | |
#else | |
return FTW_CONTINUE; // Continue with the files despite knowing we will ignore them in the future | |
#endif | |
} | |
} | |
// Check if the time is later than the last | |
if(timespeccmp(&start, &(status->st_mtim), <) || timespeccmp(&start, &(status->st_ctim), <)) | |
{ | |
// Found newly modified file | |
return FTW_STOP; | |
} | |
return FTW_CONTINUE; | |
} | |
int main(int _argc, const char** _argv) | |
{ | |
argc = _argc; | |
argv = _argv; | |
if(argc<2) { | |
printf("%s", "Required parameter: directory to watch\n"); | |
return 1; | |
} | |
clock_gettime(CLOCK_REALTIME, &start); | |
while(nftw(argv[1], callback, 10, | |
#ifdef FTW_ACTIONRETVAL | |
FTW_ACTIONRETVAL | |
#else | |
0 | |
#endif | |
) != FTW_STOP); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment