文件更改提示

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <errno.h>
  4. /*static volatile atomic variable*/
  5. static volatile sig_atomic_t sigflag = 0;
  6. /*private signal control handler*/
  7. static void TurnFlag(int no){
  8.     sigflag = (sigflag == 0) ? 1 : 0;
  9. }
  10. /**
  11.  * set signal handler
  12.  * if error return -1
  13.  * or return 0
  14.  */
  15. int InitHandler(int signo){ /*signal macro*/
  16.     /*using sigaction function need this struct*/
  17.     struct sigaction act;  
  18.         
  19.     act.sa_handler = TurnFlag; /*set signal function*/
  20.     act.sa_flags = 0;/*meaning sigaction is pure action*/
  21.     if( (sigemptyset(&act.sa_mask) == -1) || /*get empty mask*/
  22.         (sigaddset(&act.sa_mask,signo) == -1) ||/*add signo into mark*/
  23.         (sigaction(signo,&act,NULL) == -1) ) /*execute function*/
  24.         return -1;
  25.     return 0;
  26. }
  27. /*control signal,main method is suspeed signal and restore signal 
  28.  * if error return -1,or return 0*/
  29. int ControlSig(int signo){
  30.     /*actunblock variable mean no block mask
  31.      * actblock variable meaning need to block mask
  32.      * actold is original mask for restore
  33.      * */
  34.     sigset_t actunblock,actblock,actold;
  35.     
  36.     if( (sigprocmask(SIG_SETMASK,NULL,&actunblock) == -1)   ||
  37.         (sigprocmask(SIG_SETMASK,NULL,&actblock) == -1) ||
  38.         (sigaddset(&actblock,signo) == -1)  ||
  39.         (sigdelset(&actunblock,signo) == -1) ||
  40.         (sigprocmask(SIG_BLOCK,&actblock,&actold) == -1)    )
  41.         return -1;
  42.     while(sigflag != 0)
  43.         sigsuspend(&actunblock);
  44.     if(sigprocmask(SIG_SETMASK,&actold,NULL) == -1)
  45.         return -1;
  46.     return 0;
  47. }
  48. 测试运行
  49. #include <stdio.h>
  50. #include <fcntl.h>
  51. #include <errno.h>
  52. #include <stdlib.h>
  53. #include <unistd.h>
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56. #include <signal.h>
  57. #include "mystring.h"
  58. #include "file.h"
  59. #include "tools.h"
  60. #include "myerror.h"
  61. #include "restart.h"
  62. #include "passwd.h"
  63. #include "sig.h"
  64. int main(int argc,char *argv[]){
  65.     static struct stat oldsize,newsize;
  66.     const char *filepath = "makefile";
  67.     const int sleepn = 2;
  68.     int signo = SIGINT;
  69.     if(stat(filepath,&oldsize) == -1)
  70.         return -1;
  71.     for(; ;){
  72.         if(InitHandler(signo) == -1){
  73.             MyError("init handler error");
  74.             return -1;
  75.         }
  76.         if(ControlSig(signo) == -1){
  77.             MyError("control sig error");
  78.             return -1;
  79.         }
  80.         if(stat(filepath,&newsize) == -1)
  81.             return -1;
  82.         if(newsize.st_size != oldsize.st_size){
  83.             oldsize.st_size = newsize.st_size;
  84.             fprintf(stderr,"check your file!%s/n","/007");
  85.         }
  86.         sleep(sleepn);
  87.     }
  88. }

你可能感兴趣的:(文件更改提示)