home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name FLDOLOCK -- Lock or unlock a section of an open file,
- * wait intelligently if busy.
- *
- * Synopsis ercode = fldolock (handle, offset, length,
- * seconds, option);
- *
- * int ercode DOS function error code.
- * int handle File handle of open file.
- * unsigned long offset
- * Offset (in bytes) of beginning of
- * region to lock/unlock.
- * unsigned long length
- * Length (in bytes) of region to
- * lock/unlock.
- * int seconds Number of seconds to wait
- * if lock call fails due to a
- * previous lock.
- * int option FL_LOCK or FL_UNLOCK.
- *
- * Description This function locks or unlocks a section of an open file
- * associated with the specified handle. If ticks is not 0,
- * FLDOLOCK retries the locking/unlocking operation
- * constantly until either (1) it succeeds, or (2) it
- * runs out of time.
- *
- * DOS 3.10 or later is required. Earlier versions of DOS
- * return a value of 1 indicating that the function number
- * is unknown.
- *
- * The designated region of the file may extend beyond the
- * end of the file.
- *
- * The recommended technique for using locks is as follows:
- *
- * 1) Lock the region using FLDOLOCK, using a
- * reasonable number of ticks. If the lock
- * attempt fails because the region was already
- * locked (ercode = 0x21), either keep trying or
- * report an error.
- * 2) After locking the region, read or write the data.
- * 3) Unlock the region using FLDOLOCK.
- *
- * Do not close the file or allow the program to terminate
- * without removing all locks.
- *
- * Returns ercode DOS function return code
- *
- * Version 6.00 (C)Copyright Blaise Computing Inc. 1987, 1989
- *
- **/
-
- #include <dos.h>
- #include <errno.h>
- #include <io.h>
- #include <time.h>
-
- #include <bfiles.h>
-
- #define ALREADY_LOCKED 0x21
-
- int fldolock (handle, offset, length, seconds, option)
- int handle, option, seconds;
- unsigned long offset, length;
- {
- int result;
- long start_second, now_second;
-
- /* DOS 2.x does not know about file locking. */
- if (utdosmajor < 3)
- return (1);
-
- time (&start_second);
-
- do
- { /* (Note: DOS 3.00 does not support function 0x5c */
- /* but at least it reports a valid error code & */
- /* extended error information.) */
-
- /* If there was an error, and it was not a "Lock */
- /* Violation" error, return immediately -- If there */
- /* wasn't an error, return. */
- if ((result = fllock (handle, option, offset, length)) == 0)
- return (0);
- else
- if (result != ALREADY_LOCKED)
- return ((int) result);
-
- time (&now_second);
- } while ((now_second - start_second) < (long) seconds);
-
- /* If we got an error on our last call (and we got */
- /* to here so it must be error 0x21), return it. */
- return (result);
- }