home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / lockfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  3.1 KB  |  163 lines

  1. /*
  2.  *  LOCKFILE.C
  3.  *
  4.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  *  Now uses OwnDevUnit.Library
  7.  *  Caller should install exit handler to deal with program exit
  8.  *
  9.  *  WARNING:    cannot call MakeConfigPath() since static buffer will
  10.  *        be overwritten and caller might expect it not to be
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/lists.h>
  15. #include <libraries/dos.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <config.h>
  20. #include <OwnDevUnit.h>
  21. #include <log.h>
  22.  
  23. typedef struct {
  24.     struct Node Node;
  25.     FILE    *Fi;
  26.     short   Refs;
  27. } LNode;
  28.  
  29. Prototype void LockFile (const char *);
  30. Prototype void UnLockFile (const char *);
  31. Prototype void UnLockFiles (void);
  32. Prototype int FileIsLocked (const char *);
  33. static void FreeLockNode (LNode *);
  34.  
  35. struct List
  36.     LockList = { (struct Node *) &LockList.lh_Tail,
  37.              NULL,
  38.              (struct Node *) &LockList.lh_Head
  39.            };
  40. static char
  41.     Buf [512];
  42. extern struct Library
  43.     *OwnDevUnitBase;
  44.  
  45. void
  46. LockFile (const char *file)
  47. {
  48.     const char
  49.         *ptr,
  50.         *lockDir = GetConfigDir (LOCKDIR);
  51.     int
  52.         lockLen = strlen (lockDir);
  53.     LNode
  54.         *node,
  55.         *n;
  56.  
  57.     for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
  58.         ;
  59.     ++ptr;
  60.  
  61.     if (node = malloc (sizeof (LNode) + lockLen + 16 + strlen (ptr))) {
  62.         node->Node.ln_Name = (char *) (node + 1);
  63.  
  64.         strcpy (node->Node.ln_Name, MakeConfigPathBuf (Buf, LOCKDIR, ptr));
  65.         strcat (node->Node.ln_Name, ".LOCK");
  66.  
  67.         for (n = (LNode *) LockList.lh_Head; n != (LNode *) &LockList.lh_Tail; n = (LNode *) n->Node.ln_Succ) {
  68.             if (stricmp (node->Node.ln_Name, n->Node.ln_Name) == 0) {
  69.                 ++n->Refs;
  70.                 free (node);
  71.                 return;
  72.             }
  73.         }
  74.  
  75.         if (OwnDevUnitBase) {
  76.             if (ptr = (char *) LockDevUnit ((UBYTE *) node->Node.ln_Name, 0, (UBYTE *) LogProgram, 0)) {
  77.                 ulog (-1, "LockDevUnit() failed: %s", ptr);
  78.             }
  79.         }
  80.         node->Refs = 1;
  81.         AddTail (&LockList, &node->Node);
  82.     }
  83.  
  84.     return;
  85. }
  86.  
  87. /*
  88.  *  Check to see whether a file is locked.  We could try to fopen the
  89.  *  file for 'w', but this causes unnecesary filesystem activity
  90.  */
  91.  
  92. int
  93. FileIsLocked (const char *file)
  94. {
  95.     const char
  96.         *ptr;
  97.     char
  98.         buf [128];
  99.  
  100.     for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
  101.         ;
  102.     ++ptr;
  103.  
  104.     sprintf (buf, "%s.LOCK", MakeConfigPathBuf (Buf, LOCKDIR, ptr));
  105.  
  106.     if (OwnDevUnitBase) {
  107.         if (AvailDevUnit ((UBYTE *) buf, 0))
  108.             return 0;
  109.  
  110.         return 1;
  111.     }
  112.  
  113.     return 0;
  114. }
  115.  
  116. void
  117. UnLockFile(const char *file)
  118. {
  119.     LNode
  120.         *node;
  121.     const char
  122.         *ptr;
  123.  
  124.     for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
  125.         ;
  126.     ++ptr;
  127.  
  128.     MakeConfigPathBuf (Buf, LOCKDIR, ptr);
  129.     strcat (Buf, ".LOCK");
  130.  
  131.     for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ) {
  132.         if (stricmp(Buf, node->Node.ln_Name) == 0) {
  133.             if (--node->Refs == 0)
  134.                 FreeLockNode (node);
  135.             break;
  136.         }
  137.     }
  138.  
  139.     return;
  140. }
  141.  
  142. void
  143. UnLockFiles (void)
  144. {
  145.     LNode
  146.         *node;
  147.  
  148.     while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
  149.         FreeLockNode (node);
  150. }
  151.  
  152. static void
  153. FreeLockNode (LNode *node)
  154. {
  155.     Remove ((struct Node *) node);
  156.  
  157.     if (OwnDevUnitBase)
  158.         FreeDevUnit ((UBYTE *) node->Node.ln_Name, 0);
  159.  
  160.     free (node);
  161.     return;
  162. }
  163.