home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / lib / lockfile.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  3KB  |  116 lines

  1.  
  2. /*
  3.  *  LOCKFILE.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/lib/RCS/lockfile.c,v 1.1 90/02/02 12:08:31 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  Lock and unlock a file.  Under AmigaDOS, openning a file mode 1006
  10.  *  (accomplished with fopen(,"w"), locks the file exclusively.  That
  11.  *  is, further fopen()s will fail.  Thus, we need only keep a live
  12.  *  file descriptor to 'lock' the file.
  13.  *
  14.  *  This is advantagious because if the program exits without removing
  15.  *  the lock file we are still ok... the file is unlocked by virtue of
  16.  *  the file descriptor getting closed.
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/lists.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "config.h"
  24.  
  25. typedef struct List LIST;
  26. typedef struct Node NODE;
  27.  
  28. typedef struct {
  29.     NODE    Node;
  30.     FILE    *Fi;
  31.     short   Refs;
  32. } LNode;
  33.  
  34. Prototype void LockFile(const char *);
  35. Prototype void UnLockFile(const char *);
  36. Prototype void UnLockFiles(void);
  37. Local void FreeLockNode(LNode *);
  38.  
  39. LIST LockList = { (NODE *)&LockList.lh_Tail, NULL, (NODE *)&LockList.lh_Head };
  40.  
  41.  
  42. void
  43. LockFile(file)
  44. const char *file;
  45. {
  46.     const char *ptr;
  47.  
  48.     LNode *node;
  49.     LNode *n;
  50.  
  51.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  52.     ++ptr;
  53.  
  54.  
  55.     if (node = malloc(sizeof(LNode) + strlen(ptr) + 16)) {
  56.     node->Node.ln_Name = (char *)(node + 1);
  57.     sprintf(node->Node.ln_Name, "T:%s.LOCK", ptr);
  58.  
  59.     for (n = (LNode *)LockList.lh_Head; n != (LNode *)&LockList.lh_Tail; n = (LNode *)n->Node.ln_Succ) {
  60.         if (strcmp(node->Node.ln_Name, n->Node.ln_Name) == 0) {
  61.         ++n->Refs;
  62.         free(node);
  63.         return;
  64.         }
  65.     }
  66.  
  67.     while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
  68.         sleep(2);
  69.         chkabort();
  70.     }
  71.     node->Refs = 1;
  72.     AddTail(&LockList, &node->Node);
  73.     }
  74. }
  75.  
  76. void
  77. UnLockFile(file)
  78. const char *file;
  79. {
  80.     LNode *node;
  81.     short len;
  82.     const char *ptr;
  83.  
  84.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  85.     ++ptr;
  86.     len = strlen(ptr);
  87.  
  88.     for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ) {
  89.     if (strncmp(ptr, node->Node.ln_Name + 2, len) == 0 && strlen(node->Node.ln_Name) == len + 7) {
  90.         if (--node->Refs == 0)
  91.         FreeLockNode(node);
  92.         return;
  93.     }
  94.     }
  95. }
  96.  
  97. void
  98. UnLockFiles()
  99. {
  100.     LNode *node;
  101.  
  102.     while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
  103.     FreeLockNode(node);
  104. }
  105.  
  106. static void
  107. FreeLockNode(node)
  108. LNode *node;
  109. {
  110.     Remove(node);
  111.     fclose(node->Fi);
  112.     unlink(node->Node.ln_Name);
  113.     free(node);
  114. }
  115.  
  116.