home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / ListSERV1_4.lha / ListSERV / source / RCS / LockFile.c,v < prev    next >
Encoding:
Text File  |  1993-12-04  |  5.2 KB  |  216 lines

  1. head    0.2;
  2. access;
  3. symbols;
  4. locks
  5.     simons:0.2; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 0.2
  10. date    93.12.04.14.39.21;    author simons;    state Exp;
  11. branches;
  12. next    0.1;
  13.  
  14. 0.1
  15. date    93.12.04.14.02.50;    author simons;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @Set of routines to syncronise file access.
  22. @
  23.  
  24.  
  25. 0.2
  26. log
  27. @Removed a few routines, which would have required a whole bunch of
  28. additional sources and libraries.
  29. Reformatted the source using Indent.
  30. @
  31. text
  32. @/*
  33.  *      $Filename: LockFile.c $
  34.  *      $Revision: 0.1 $
  35.  *      $Date: 1993/12/04 14:02:50 $
  36.  *
  37.  *      Copyright (C) 1993 by Peter Simons <simons@@peti.GUN.de>
  38.  *
  39.  *      This program is free software; you can redistribute it and/or
  40.  *      modify it under the terms of the GNU General Public License as
  41.  *      published by the Free Software Foundation; either version 2 of
  42.  *      the License, or (at your option) any later version.
  43.  *
  44.  *      This program is distributed in the hope that it will be useful,
  45.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  46.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  47.  *      General Public License for more details.
  48.  *
  49.  *      You should have received a copy of the GNU General Public License
  50.  *      along with this program; if not, write to the Free Software
  51.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  52.  *
  53.  *      $Id: LockFile.c,v 0.1 1993/12/04 14:02:50 simons Exp simons $
  54.  *
  55.  * ------------------------------ log history ----------------------------
  56.  * $Log: LockFile.c,v $
  57.  * Revision 0.1  1993/12/04  14:02:50  simons
  58.  * Taken from Dillon UUCP 1.15.
  59.  */
  60.  
  61. #include <exec/types.h>
  62. #include <exec/lists.h>
  63. #include <libraries/dos.h>
  64. #include <proto/exec.h>
  65. #include <proto/dos.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69.  
  70. typedef struct List LIST;
  71. typedef struct Node NODE;
  72.  
  73. typedef struct {
  74.         NODE Node;
  75.         FILE *Fi;
  76.         short Refs;
  77. } LNode;
  78.  
  79. static void FreeLockNode(LNode *);
  80.  
  81. static char __RCSId[] = "$Id: LockFile.c,v 0.1 1993/12/04 14:02:50 simons Exp simons $";
  82.  
  83. LIST LockList =
  84. {(NODE *) & LockList.lh_Tail, NULL, (NODE *) & LockList.lh_Head};
  85. static char Buf[512];
  86.  
  87. void LockFile(char *file)
  88. {
  89.         char *ptr;
  90.         char *lockDir = "T:";
  91.         short lockLen = strlen(lockDir);
  92.  
  93.         LNode *node;
  94.         LNode *n;
  95.  
  96.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  97.         ++ptr;
  98.  
  99.         if (node = malloc(sizeof(LNode) + lockLen + 16 + strlen(ptr))) {
  100.                 node->Node.ln_Name = (char *) (node + 1);
  101.  
  102.                 strcpy(node->Node.ln_Name, "T:");
  103.                 strcat(node->Node.ln_Name, ptr);
  104.                 strcat(node->Node.ln_Name, ".LOCK");
  105.  
  106.                 for (n = (LNode *) LockList.lh_Head; n != (LNode *) & LockList.lh_Tail; n = (LNode *) n->Node.ln_Succ) {
  107.                         if (stricmp(node->Node.ln_Name, n->Node.ln_Name) == 0) {
  108.                                 ++n->Refs;
  109.                                 free(node);
  110.                                 return;
  111.                         }
  112.                 }
  113.  
  114.                 while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
  115.                         Delay(2*50);
  116.                 }
  117.                 node->Refs = 1;
  118.                 AddTail(&LockList, &node->Node);
  119.         }
  120. }
  121.  
  122. /*
  123.  *  Check to see whether a file is locked.  We could try to fopen the
  124.  *  file for 'w', but this causes unnecesary filesystem activity
  125.  */
  126.  
  127. int FileIsLocked(char *file)
  128. {
  129.         char *ptr;
  130.         char buf[128];
  131.         long lock;
  132.  
  133.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  134.         ++ptr;
  135.         sprintf(buf, "T:%s.LOCK", ptr);
  136.  
  137.         if (lock = Lock(buf, EXCLUSIVE_LOCK)) {
  138.                 UnLock(lock);
  139.                 return (0);
  140.         }
  141.         if (IoErr() == ERROR_OBJECT_IN_USE)
  142.                 return (1);
  143.         return (0);
  144. }
  145.  
  146. void UnLockFile(char *file)
  147. {
  148.         LNode *node;
  149.         char *ptr;
  150.  
  151.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  152.         ++ptr;
  153.  
  154.         sprintf(Buf, "T:%s.LOCK", ptr);
  155.  
  156.         for (node = (LNode *) LockList.lh_Head; node != (LNode *) & LockList.lh_Tail; node = (LNode *) node->Node.ln_Succ) {
  157.                 if (stricmp(Buf, node->Node.ln_Name) == 0) {
  158.                         if (--node->Refs == 0)
  159.                                 FreeLockNode(node);
  160.                         break;
  161.                 }
  162.         }
  163. }
  164.  
  165. void UnLockFiles(void)
  166. {
  167.         LNode *node;
  168.  
  169.         while ((node = (LNode *) LockList.lh_Head) != (LNode *) & LockList.lh_Tail)
  170.                 FreeLockNode(node);
  171. }
  172.  
  173. static void FreeLockNode(LNode *node)
  174. {
  175.         Remove(&(node->Node));
  176.         fclose(node->Fi);
  177.         unlink(node->Node.ln_Name);
  178.         free(node);
  179. }
  180. @
  181.  
  182.  
  183. 0.1
  184. log
  185. @Taken from Dillon UUCP 1.15.
  186. @
  187. text
  188. @d3 2
  189. a4 2
  190.  *      $Revision$
  191.  *      $Date$
  192. d22 1
  193. a22 1
  194.  *      $Id$
  195. d25 3
  196. a27 1
  197.  * $Log$
  198. d48 1
  199. a48 1
  200. static char __RCSId[] = "$Id$";
  201. d50 2
  202. d59 1
  203. a59 1
  204.         char *lockDir = GetConfigDir(LOCKDIR);
  205. d71 2
  206. a72 1
  207.                 strcpy(node->Node.ln_Name, MakeConfigPathBuf(Buf, LOCKDIR, ptr));
  208. d104 1
  209. a104 1
  210.         sprintf(buf, "%s.LOCK", MakeConfigPathBuf(Buf, LOCKDIR, ptr));
  211. d123 1
  212. a123 2
  213.         MakeConfigPathBuf(Buf, LOCKDIR, ptr);
  214.         strcat(Buf, ".LOCK");
  215. @
  216.