home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / network / amigaelm.lzh / lockfile.c < prev    next >
C/C++ Source or Header  |  1992-02-15  |  3KB  |  170 lines

  1.  
  2. /*
  3.  *  Lock and unlock a file.  Under AmigaDOS, openning a file mode 1006
  4.  *  (accomplished with fopen(,"w"), locks the file exclusively.  That
  5.  *  is, further fopen()s will fail.  Thus, we need only keep a live
  6.  *  file descriptor to 'lock' the file.
  7.  *
  8.  *  This is advantagious because if the program exits without removing
  9.  *  the lock file we are still ok... the file is unlocked by virtue of
  10.  *  the file descriptor getting closed.
  11.  */
  12.  
  13. #include "prefs.h"
  14. #ifdef LATTICE
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #endif
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22.  
  23.  
  24. #define LOCKDIR        "T:"
  25.  
  26.  
  27. struct LockNode {
  28.     struct LockNode *next;
  29.     FILE *fp;
  30.     char *file;
  31.     long refs;
  32. };
  33.  
  34.  
  35.  
  36. Prototype void LockFile(char *);
  37. Prototype void UnLockFile(char *);
  38. Prototype void UnLockFiles(void);
  39. Prototype BOOL FileIsLocked(char *);
  40. Local void FreeLockNode(struct LockNode *);
  41.  
  42.  
  43.  
  44. struct LockNode *LockList = NULL;
  45.  
  46.  
  47.  
  48. void LockFile(char *file)
  49. {
  50.   char *ptr;
  51.   struct LockNode *node;
  52.   struct LockNode *n;
  53.  
  54.   for (ptr = file+strlen(file); ptr >= file && *ptr!='/' && *ptr!=':'; --ptr);
  55.   ++ptr;
  56.  
  57.   if (node=malloc(sizeof(struct LockNode))) {
  58.     node->next = LockList;
  59.     node->refs = 1L;
  60.     if (node->file=malloc(strlen(LOCKDIR)+strlen(ptr)+6)) {
  61.       sprintf(node->file,"%s%s.LOCK",LOCKDIR,ptr);
  62.       for (n=LockList;n;n=n->next) {
  63.         if (!stricmp(node->file,n->file)) {
  64.           n->refs = n->refs + 1L;
  65.           free(node->file);
  66.           free(node);
  67.           return;
  68.         }
  69.       }
  70.       while (!(node->fp=fopen(node->file, "w")))
  71.         Delay(100L);
  72.  
  73.       LockList = node;
  74.     }
  75.   }
  76. }
  77.  
  78.  
  79.  
  80. /*
  81.  *  Check to see whether a file is locked.  We could try to fopen the
  82.  *  file for 'w', but this causes unnecesary filesystem activity
  83.  */
  84.  
  85. BOOL FileIsLocked(char *file)
  86. {
  87.   char *ptr;
  88.   char *buf;
  89.   BPTR lock;
  90.  
  91.   for (ptr = file+strlen(file); ptr >= file && *ptr!='/' && *ptr!=':'; --ptr);
  92.   ++ptr;
  93.  
  94.   if (buf=malloc(strlen(LOCKDIR)+strlen(ptr)+6)) {
  95.     sprintf(buf,"%s%s.LOCK",LOCKDIR,ptr);
  96.  
  97.     if (lock=Lock(buf,EXCLUSIVE_LOCK)) {
  98.       UnLock(lock);
  99.       free(buf);
  100.       return(FALSE);
  101.     }
  102.  
  103.     free(buf);
  104.  
  105.     if (IoErr()==ERROR_OBJECT_IN_USE)
  106.       return(TRUE);
  107.   }
  108.   return(FALSE);
  109. }
  110.  
  111.  
  112.  
  113. void UnLockFile(char *file)
  114. {
  115.   struct LockNode *node,*last;
  116.   char *buf;
  117.   char *ptr;
  118.  
  119.   for (ptr = file+strlen(file); ptr >= file && *ptr!='/' && *ptr!=':'; --ptr);
  120.   ++ptr;
  121.  
  122.   if (buf=malloc(strlen(LOCKDIR)+strlen(ptr)+6)) {
  123.     sprintf(buf,"%s%s.LOCK",LOCKDIR,ptr);
  124.  
  125.     for (node=LockList,last=NULL;node;node=node->next) {
  126.       if (!stricmp(buf,node->file)) {
  127.         node->refs = node->refs - 1L;
  128.         if (!node->refs) {
  129.           if (last)
  130.             last->next = node->next;
  131.           else
  132.             LockList = node->next;
  133.           FreeLockNode(node);
  134.           free(buf);
  135.           return;
  136.         }
  137.       }
  138.       last = node;
  139.     }
  140.  
  141.     free(buf);
  142.   }
  143. }
  144.  
  145.  
  146.  
  147. void UnLockFiles(void)
  148. {
  149.   struct LockNode *node,*next;
  150.  
  151.   node = LockList;
  152.  
  153.   while (node) {
  154.     next = node->next;
  155.     FreeLockNode(node);
  156.     node = next;
  157.   }
  158. }
  159.  
  160.  
  161.  
  162. static void FreeLockNode(struct LockNode *node)
  163. {
  164.   fclose(node->fp);
  165.   unlink(node->file);
  166.   free(node->file);
  167.   free(node);
  168. }
  169.  
  170.