home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / lib / lockfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-21  |  2.6 KB  |  118 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 "protos.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <dos.h>
  25. #include "config.h"
  26.  
  27. typedef struct List LIST;
  28. typedef struct Node NODE;
  29.  
  30. typedef struct {
  31.     NODE    Node;
  32.     FILE    *Fi;
  33.     short   Refs;
  34. } LNode;
  35.  
  36. Prototype void LockFile(const char *);
  37. Prototype void UnLockFile(const char *);
  38. Prototype void UnLockFiles(void);
  39. Local void FreeLockNode(LNode *);
  40.  
  41. LIST LockList = { (NODE *)&LockList.lh_Tail, NULL, (NODE *)&LockList.lh_Head };
  42.  
  43.  
  44. void
  45. LockFile(file)
  46. const char *file;
  47. {
  48.     const char *ptr;
  49.  
  50.     LNode *node;
  51.     LNode *n;
  52.  
  53.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  54.     ++ptr;
  55.  
  56.  
  57.     if (node = malloc(sizeof(LNode) + strlen(ptr) + 16)) {
  58.     node->Node.ln_Name = (char *)(node + 1);
  59.     sprintf(node->Node.ln_Name, "T:%s.LOCK", ptr);
  60.  
  61.     for (n = (LNode *)LockList.lh_Head; n != (LNode *)&LockList.lh_Tail; n = (LNode *)n->Node.ln_Succ) {
  62.         if (strcmp(node->Node.ln_Name, n->Node.ln_Name) == 0) {
  63.         ++n->Refs;
  64.         free(node);
  65.         return;
  66.         }
  67.     }
  68.  
  69.     while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
  70.         sleep(2);
  71.         chkabort();
  72.     }
  73.     node->Refs = 1;
  74.     AddTail(&LockList, &node->Node);
  75.     }
  76. }
  77.  
  78. void
  79. UnLockFile(file)
  80. const char *file;
  81. {
  82.     LNode *node;
  83.     short len;
  84.     const char *ptr;
  85.  
  86.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  87.     ++ptr;
  88.     len = strlen(ptr);
  89.  
  90.     for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ) {
  91.     if (strncmp(ptr, node->Node.ln_Name + 2, len) == 0 && strlen(node->Node.ln_Name) == len + 7) {
  92.         if (--node->Refs == 0)
  93.         FreeLockNode(node);
  94.         return;
  95.     }
  96.     }
  97. }
  98.  
  99. void
  100. UnLockFiles()
  101. {
  102.     LNode *node;
  103.  
  104.     while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
  105.     FreeLockNode(node);
  106. }
  107.  
  108. static void
  109. FreeLockNode(node)
  110. LNode *node;
  111. {
  112.     Remove(node);
  113.     fclose(node->Fi);
  114.     unlink(node->Node.ln_Name);
  115.     free(node);
  116. }
  117.  
  118.