home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3435 / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  1.7 KB  |  106 lines

  1. /*
  2. ** Lock 'file' using a 'file'.lck protocol.
  3. **
  4. ** This is public domain.
  5. ** Robert Osborne.
  6. **
  7. ** $Id: lock.c,v 1.1 1991/04/30 19:16:26 robert Exp $
  8. */
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12.  
  13. lock_file(path, oldest, sec)
  14.     char *path;                /* file we want to 'lock' */
  15.     unsigned int oldest;    /* delete file if older than oldest */
  16.     unsigned int sec;        /* seconds we are willing to wait */
  17. {
  18.     char lpath[1024];
  19.     int locked = 0;
  20.     struct stat buf;
  21.     time_t age = 0;
  22.     FILE *fp;
  23.     
  24.     sprintf(lpath, "%s.lck", path);
  25.     
  26.     while( ! locked )
  27.     {
  28.         if( stat(lpath, &buf) == -1 )
  29.         {
  30.             if( (fp = fopen(lpath, "w")) == (FILE *) 0 )
  31.             {
  32.                 return 0;
  33.             }
  34.             fclose(fp);
  35.             return 1;
  36.         }
  37.  
  38.         /* get the age of file */
  39.         if( age == 0 )
  40.         {
  41.             age = time((long *)0) - buf.st_mtime;
  42.         }
  43.  
  44. #ifdef TEST_LOCK
  45.         printf("age=%ld\n", age);
  46. #endif
  47.         if( age > oldest )
  48.         {
  49.             unlink(lpath);
  50.             continue;
  51.         }
  52.         
  53.         if( sec == 0 ) break;
  54.         sleep(1);
  55.         sec--;
  56.         age++;
  57.     }
  58.     return locked;
  59. }
  60.  
  61. unlock_file(path)
  62.     char *path;                /* file we want to 'unlock' */
  63. {
  64.     char lpath[1024];
  65.     struct stat buf;
  66.     
  67.     sprintf(lpath, "%s.lck", path);
  68.     unlink(lpath);
  69. }
  70. #ifdef TEST_LOCK
  71. main(argc,argv)
  72.     int argc;
  73.     char *argv[];
  74. {
  75.     extern char *optarg;
  76.     extern int optind;
  77.     int c;
  78.     int age = 100;
  79.     int sec = 2;
  80.  
  81.     while((c=getopt(argc, argv, "?Hs:a:LU")) != EOF ) {
  82.         switch(c) {
  83.         case 's':
  84.             sec = atoi(optarg);
  85.             break;
  86.         case 'a':
  87.             age = atoi(optarg);
  88.             break;
  89.         case 'L':
  90.             if( lock_file("test", age, sec) )
  91.                 printf("lock success\n");
  92.             else
  93.                 printf("lock failed\n");
  94.             break;
  95.         case 'U':
  96.             unlock_file("test");
  97.             break;
  98.         default:
  99.             fprintf(stderr, "usage: %s [ -s seconds -a age]  -L | -U\n",
  100.                     argv[0]);
  101.         }
  102.     }
  103.  
  104. }
  105. #endif
  106.