home *** CD-ROM | disk | FTP | other *** search
- /*
- * LOCKFILE.C
- *
- * (C) Copyright 1989-1990 by Matthew Dillon, All Rights Reserved.
- *
- * Now uses OwnDevUnit.Library
- * Caller should install exit handler to deal with program exit
- *
- * WARNING: cannot call MakeConfigPath() since static buffer will
- * be overwritten and caller might expect it not to be
- */
-
- #include <exec/types.h>
- #include <exec/lists.h>
- #include <libraries/dos.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <config.h>
- #include <OwnDevUnit.h>
- #include <log.h>
-
- typedef struct {
- struct Node Node;
- FILE *Fi;
- short Refs;
- } LNode;
-
- Prototype void LockFile (const char *);
- Prototype void UnLockFile (const char *);
- Prototype void UnLockFiles (void);
- Prototype int FileIsLocked (const char *);
- static void FreeLockNode (LNode *);
-
- struct List
- LockList = { (struct Node *) &LockList.lh_Tail,
- NULL,
- (struct Node *) &LockList.lh_Head
- };
- static char
- Buf [512];
- extern struct Library
- *OwnDevUnitBase;
-
- void
- LockFile (const char *file)
- {
- const char
- *ptr,
- *lockDir = GetConfigDir (LOCKDIR);
- int
- lockLen = strlen (lockDir);
- LNode
- *node,
- *n;
-
- for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
- ;
- ++ptr;
-
- if (node = malloc (sizeof (LNode) + lockLen + 16 + strlen (ptr))) {
- node->Node.ln_Name = (char *) (node + 1);
-
- strcpy (node->Node.ln_Name, MakeConfigPathBuf (Buf, LOCKDIR, ptr));
- strcat (node->Node.ln_Name, ".LOCK");
-
- for (n = (LNode *) LockList.lh_Head; n != (LNode *) &LockList.lh_Tail; n = (LNode *) n->Node.ln_Succ) {
- if (stricmp (node->Node.ln_Name, n->Node.ln_Name) == 0) {
- ++n->Refs;
- free (node);
- return;
- }
- }
-
- if (OwnDevUnitBase) {
- if (ptr = (char *) LockDevUnit ((UBYTE *) node->Node.ln_Name, 0, (UBYTE *) LogProgram, 0)) {
- ulog (-1, "LockDevUnit() failed: %s", ptr);
- }
- }
- node->Refs = 1;
- AddTail (&LockList, &node->Node);
- }
-
- return;
- }
-
- /*
- * Check to see whether a file is locked. We could try to fopen the
- * file for 'w', but this causes unnecesary filesystem activity
- */
-
- int
- FileIsLocked (const char *file)
- {
- const char
- *ptr;
- char
- buf [128];
-
- for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
- ;
- ++ptr;
-
- sprintf (buf, "%s.LOCK", MakeConfigPathBuf (Buf, LOCKDIR, ptr));
-
- if (OwnDevUnitBase) {
- if (AvailDevUnit ((UBYTE *) buf, 0))
- return 0;
-
- return 1;
- }
-
- return 0;
- }
-
- void
- UnLockFile(const char *file)
- {
- LNode
- *node;
- const char
- *ptr;
-
- for (ptr = file + strlen (file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
- ;
- ++ptr;
-
- MakeConfigPathBuf (Buf, LOCKDIR, ptr);
- strcat (Buf, ".LOCK");
-
- for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ) {
- if (stricmp(Buf, node->Node.ln_Name) == 0) {
- if (--node->Refs == 0)
- FreeLockNode (node);
- break;
- }
- }
-
- return;
- }
-
- void
- UnLockFiles (void)
- {
- LNode
- *node;
-
- while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
- FreeLockNode (node);
- }
-
- static void
- FreeLockNode (LNode *node)
- {
- Remove ((struct Node *) node);
-
- if (OwnDevUnitBase)
- FreeDevUnit ((UBYTE *) node->Node.ln_Name, 0);
-
- free (node);
- return;
- }
-