home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP0_22.lha / LharcUUCP / source / LockFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-07  |  4.5 KB  |  154 lines

  1. /*
  2.  *      $Filename: LockFile.c $
  3.  *      $Revision: 0.2 $
  4.  *      $Date: 1993/12/04 14:39:21 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: LockFile.c,v 0.2 1993/12/04 14:39:21 simons Exp simons $
  23.  *
  24.  * ------------------------------ log history ----------------------------
  25.  * $Log: LockFile.c,v $
  26.  * Revision 0.2  1993/12/04  14:39:21  simons
  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.  * Revision 0.1  1993/12/04  14:02:50  simons
  32.  * Taken from Dillon UUCP 1.15.
  33.  */
  34.  
  35. #include <exec/types.h>
  36. #include <exec/lists.h>
  37. #include <libraries/dos.h>
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. typedef struct List LIST;
  45. typedef struct Node NODE;
  46.  
  47. typedef struct {
  48.         NODE Node;
  49.         FILE *Fi;
  50.         short Refs;
  51. } LNode;
  52.  
  53. static void FreeLockNode(LNode *);
  54.  
  55. static char __RCSId[] = "$Id: LockFile.c,v 0.2 1993/12/04 14:39:21 simons Exp simons $";
  56.  
  57. LIST LockList =
  58. {(NODE *) & LockList.lh_Tail, NULL, (NODE *) & LockList.lh_Head};
  59. static char Buf[512];
  60.  
  61. void LockFile(char *file)
  62. {
  63.         char *ptr;
  64.         char *lockDir = "T:";
  65.         short lockLen = strlen(lockDir);
  66.  
  67.         LNode *node;
  68.         LNode *n;
  69.  
  70.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  71.         ++ptr;
  72.  
  73.         if (node = malloc(sizeof(LNode) + lockLen + 16 + strlen(ptr))) {
  74.                 node->Node.ln_Name = (char *) (node + 1);
  75.  
  76.                 strcpy(node->Node.ln_Name, "T:");
  77.                 strcat(node->Node.ln_Name, ptr);
  78.                 strcat(node->Node.ln_Name, ".LOCK");
  79.  
  80.                 for (n = (LNode *) LockList.lh_Head; n != (LNode *) & LockList.lh_Tail; n = (LNode *) n->Node.ln_Succ) {
  81.                         if (stricmp(node->Node.ln_Name, n->Node.ln_Name) == 0) {
  82.                                 ++n->Refs;
  83.                                 free(node);
  84.                                 return;
  85.                         }
  86.                 }
  87.  
  88.                 while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
  89.                         Delay(2*50);
  90.                 }
  91.                 node->Refs = 1;
  92.                 AddTail(&LockList, &node->Node);
  93.         }
  94. }
  95.  
  96. /*
  97.  *  Check to see whether a file is locked.  We could try to fopen the
  98.  *  file for 'w', but this causes unnecesary filesystem activity
  99.  */
  100.  
  101. int FileIsLocked(char *file)
  102. {
  103.         char *ptr;
  104.         char buf[128];
  105.         long lock;
  106.  
  107.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  108.         ++ptr;
  109.         sprintf(buf, "T:%s.LOCK", ptr);
  110.  
  111.         if (lock = Lock(buf, EXCLUSIVE_LOCK)) {
  112.                 UnLock(lock);
  113.                 return (0);
  114.         }
  115.         if (IoErr() == ERROR_OBJECT_IN_USE)
  116.                 return (1);
  117.         return (0);
  118. }
  119.  
  120. void UnLockFile(char *file)
  121. {
  122.         LNode *node;
  123.         char *ptr;
  124.  
  125.         for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
  126.         ++ptr;
  127.  
  128.         sprintf(Buf, "T:%s.LOCK", ptr);
  129.  
  130.         for (node = (LNode *) LockList.lh_Head; node != (LNode *) & LockList.lh_Tail; node = (LNode *) node->Node.ln_Succ) {
  131.                 if (stricmp(Buf, node->Node.ln_Name) == 0) {
  132.                         if (--node->Refs == 0)
  133.                                 FreeLockNode(node);
  134.                         break;
  135.                 }
  136.         }
  137. }
  138.  
  139. void UnLockFiles(void)
  140. {
  141.         LNode *node;
  142.  
  143.         while ((node = (LNode *) LockList.lh_Head) != (LNode *) & LockList.lh_Tail)
  144.                 FreeLockNode(node);
  145. }
  146.  
  147. static void FreeLockNode(LNode *node)
  148. {
  149.         Remove(&(node->Node));
  150.         fclose(node->Fi);
  151.         unlink(node->Node.ln_Name);
  152.         free(node);
  153. }
  154.