Skip to content

Instantly share code, notes, and snippets.

@nicola-gigante
Created November 28, 2016 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicola-gigante/937e8b1e59f347b771312ee3068bdff7 to your computer and use it in GitHub Desktop.
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.
#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