home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: LockFile.c $
- * $Revision: 0.2 $
- * $Date: 1993/12/04 14:39:21 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id: LockFile.c,v 0.2 1993/12/04 14:39:21 simons Exp simons $
- *
- * ------------------------------ log history ----------------------------
- * $Log: LockFile.c,v $
- * Revision 0.2 1993/12/04 14:39:21 simons
- * Removed a few routines, which would have required a whole bunch of
- * additional sources and libraries.
- * Reformatted the source using Indent.
- *
- * Revision 0.1 1993/12/04 14:02:50 simons
- * Taken from Dillon UUCP 1.15.
- */
-
- #include <exec/types.h>
- #include <exec/lists.h>
- #include <libraries/dos.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- typedef struct List LIST;
- typedef struct Node NODE;
-
- typedef struct {
- NODE Node;
- FILE *Fi;
- short Refs;
- } LNode;
-
- static void FreeLockNode(LNode *);
-
- static char __RCSId[] = "$Id: LockFile.c,v 0.2 1993/12/04 14:39:21 simons Exp simons $";
-
- LIST LockList =
- {(NODE *) & LockList.lh_Tail, NULL, (NODE *) & LockList.lh_Head};
- static char Buf[512];
-
- void LockFile(char *file)
- {
- char *ptr;
- char *lockDir = "T:";
- short lockLen = strlen(lockDir);
-
- LNode *node;
- LNode *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, "T:");
- strcat(node->Node.ln_Name, 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;
- }
- }
-
- while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
- Delay(2*50);
- }
- node->Refs = 1;
- AddTail(&LockList, &node->Node);
- }
- }
-
- /*
- * 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(char *file)
- {
- char *ptr;
- char buf[128];
- long lock;
-
- for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
- ++ptr;
- sprintf(buf, "T:%s.LOCK", ptr);
-
- if (lock = Lock(buf, EXCLUSIVE_LOCK)) {
- UnLock(lock);
- return (0);
- }
- if (IoErr() == ERROR_OBJECT_IN_USE)
- return (1);
- return (0);
- }
-
- void UnLockFile(char *file)
- {
- LNode *node;
- char *ptr;
-
- for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr) ;
- ++ptr;
-
- sprintf(Buf, "T:%s.LOCK", ptr);
-
- 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;
- }
- }
- }
-
- void UnLockFiles(void)
- {
- LNode *node;
-
- while ((node = (LNode *) LockList.lh_Head) != (LNode *) & LockList.lh_Tail)
- FreeLockNode(node);
- }
-
- static void FreeLockNode(LNode *node)
- {
- Remove(&(node->Node));
- fclose(node->Fi);
- unlink(node->Node.ln_Name);
- free(node);
- }
-