Created
November 28, 2016 16:43
-
-
Save nicola-gigante/937e8b1e59f347b771312ee3068bdff7 to your computer and use it in GitHub Desktop.
Esempio di programma C che usa la chiamata di sistema `stat()` per avvisare quando un file viene modificato.
This file contains 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
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
int main(int argc, char **argv) | |
{ | |
if(argc < 2) { | |
fprintf(stderr, "Specificare il nome di un file.\n"); | |
return 1; | |
} | |
char *file = argv[1]; | |
struct stat sb; | |
if(stat(file, &sb) == -1) | |
{ | |
fprintf(stderr, "%s: errore nell'accesso al file %s: %s\n", | |
argv[0], file, strerror(errno)); | |
return 1; | |
} | |
time_t mtime = sb.st_mtime; | |
while(1) { | |
if(stat(file, &sb) == -1) | |
{ | |
fprintf(stderr, "%s: errore nell'accesso al file %s: %s\n", | |
argv[0], file, strerror(errno)); | |
return 1; | |
} | |
if(sb.st_mtime != mtime) { | |
printf("Il file %s è stato modificato\n", file); | |
return 0; | |
} | |
sleep(5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment