home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / GianuzziV / SO1 / Esignal.c < prev    next >
C/C++ Source or Header  |  2001-11-07  |  2KB  |  84 lines

  1. /* Esignal.c
  2.    Eseguendo il comando kill -USR1 pid del processo (non SIGUSR1)
  3.    si determina se atime e' cambiato in qualche argomento.
  4.    Deve eseguire in background */
  5.  
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. time_t get_atime(char *);
  12. void restat();
  13.  
  14. time_t * atimes;        /* array che memorizza i precedenti atime */
  15. /* Per rendere accessibili argc and argv dal signal handler. */
  16. int nargs;              
  17. char ** argvcopy;
  18.  
  19. int main(int argc, char ** argv)
  20. {
  21.         int i;
  22.  
  23.         /* Ignoro momentaneamente il signal SIGUSR1 */
  24.         signal( SIGUSR1, SIG_IGN);
  25.  
  26.         if( argc < 2 )
  27.         {
  28.                 fprintf(stderr, "Sintassi: Esignal file1 file2 ...\n");
  29.                 exit(1);
  30.         }
  31.  
  32.         nargs = argc;
  33.         argvcopy = argv;
  34.  
  35.         /* Legge gli attributi iniziali dei file in argomento */
  36.         atimes = (time_t *) malloc ( (argc - 1)* sizeof(time_t) );
  37.         for ( i = 0; i < argc - 1; i++ )
  38.                 atimes[i] = get_atime(argv[i+1]);
  39.  
  40.         /* Pronto per ricevere il signal. */
  41.         signal( SIGUSR1, restat );
  42.  
  43.         while(1) pause();
  44. }
  45.  
  46. time_t get_atime(char * file)
  47. {
  48.         /* Return -1 se stat non e' possibile, st_atime altrimenti */
  49.         struct stat buffer;
  50.         return ( stat(file, &buffer) < 0) ? (time_t) -1 : buffer.st_atime;
  51. }
  52.  
  53. void restat()
  54. {
  55.         int i;
  56.         time_t newtime;
  57.  
  58.         /* Se c'e' stata una modifica in un file lo segnala e 
  59.            cambia la entry relativa in atime */
  60.         for (i = 0; i < nargs - 1; i++ )
  61.                 if( (newtime = get_atime(argvcopy[i+1])) != atimes[i] )
  62.                 {
  63.                         if( newtime == -1 )
  64.                                 printf("stat error per %s\n", argvcopy[i+1]);
  65.                         else
  66.                                 printf("Nuovi valori per %s\n", argvcopy[i+1]);
  67.                         atimes[i] = newtime;
  68.                 }
  69.         /* In-class exercise: what's wrong with this? */
  70. }
  71.  
  72. /*
  73. Prova di esecuzione:
  74.  
  75. krypton > Esignal Esignal.c &
  76. [1] 18882
  77. krypton > kill -USR1 18882
  78. krypton > touch Esignal.c
  79. krypton > kill -USR1 18882
  80. Nuovi valori per Esignal.c
  81. krypton >
  82.  
  83. */
  84.