home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / internet / wsplug31.zip / TEST&SET.C < prev    next >
Text File  |  1996-02-06  |  2KB  |  91 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys\stat.h>
  8. #include <share.h>
  9. #include <dos.h>
  10.  
  11.  
  12. extern int errno;
  13. void Delay(long t) ;
  14.  
  15. main(argc,argv)
  16. int argc;
  17. char **argv;
  18. { int   h,more=1,nb  ;
  19.   char  state[80]    ;
  20.  
  21.   if (argc < 3)
  22.   { printf("Missing arguments ...\n")     ;
  23.     printf("Test&Set dos_file value   \n");
  24.     return(0);
  25.   }
  26.  
  27.  
  28.   while(more)
  29.   {
  30.   h = sopen(argv[1],O_RDWR|O_CREAT,SH_DENYRW,S_IREAD|S_IWRITE) ;
  31.   if (h == -1)
  32.   { if (errno != EACCES)
  33.       { printf("Unable to Open [errno=%d]%s...\n",errno,argv[1]);
  34.     return(0) ;
  35.       }
  36.       else printf("Test&set, File %s Permision denied...\n",argv[1]) ;
  37.  
  38.   }
  39.   else
  40.    {
  41.      nb = read(h,state,79) ;
  42.      if ((nb<=0) || (nb>79) ) {nb=0;printf("OK, Empty File\n");}
  43.      state[nb] = 0 ;
  44.      if                                       (nb == 0) more = 0;
  45.      else   { printf("File %s [%s]...\n",argv[1],state);
  46.           close(h);  }
  47.    }
  48.  
  49.    if (more)
  50.    Delay(500L+(long)(random(1000)));
  51.  
  52.   }
  53.  
  54.   write(h,argv[2],strlen(argv[2]));
  55.   close(h) ;
  56.   return(0);
  57.  
  58. }
  59.  
  60. void Delay(long t)
  61. { struct time dtime   ;
  62.   int more =1         ;
  63.   unsigned long v1,v2 ;
  64.  
  65.   gettime(&dtime) ;
  66.   v1  =  (3600000L)*(unsigned long)dtime.ti_hour  ;
  67.   v1 +=  (60000L)  *(unsigned long)dtime.ti_min   ;
  68.   v1 +=  (1000L)   *(unsigned long)dtime.ti_sec   ;
  69.   v1 +=  (10L)     *(unsigned long)dtime.ti_hund  ;
  70.  
  71.  
  72.   while(more)
  73.   {
  74.   gettime(&dtime) ;
  75.   v2  =  (3600000L)*(unsigned long)dtime.ti_hour  ;
  76.   v2 +=  (60000L)  *(unsigned long)dtime.ti_min   ;
  77.   v2 +=  (1000L)   *(unsigned long)dtime.ti_sec   ;
  78.   v2 +=  (10L)     *(unsigned long)dtime.ti_hund  ;
  79.  
  80.   if (v2 < v1) v1 += (3600000L + 60000L + 1000L + 10L) ;
  81.  
  82.   if ((v2-v1)>t) more=0 ;
  83.   else
  84.   /* printf("%05lu\b\b\b\b\b",(v2-v1)) */;
  85.  
  86.   }
  87.  
  88.   return ;
  89.   }
  90.  
  91.