Borland C++ reference

locking

raw OCR

Sets or resets file-sharing locks.

Defined in header <io.h>

Syntax

#include <io.h>
#include <sys\locking.h>
int locking(int handle, int cmd, long length);

locking provides  an interface to the file-sharing mechanism of DOS  3.0 or
later. SHARE.EXE   must  be loaded before using  locking. The file to be
locked or unlocked  is the open file specified by handle. The region to be
locked or unlocked  starts at the current fileposition, and islength bytes
long.

Locks  can be placed on arbitrary, nonoverlapping  regions of any file. A
program   attempting to read or write into a locked region will retry the
operation  three times. If allthree retriesfail,thecallfails
with an error.

The  cmd specifies the action to be taken (the values are defined in sys\
locking.h):

LKLOCK             Lock  the region. If the lock isunsuccessful, try once a
                   second  for 10 seconds before giving up.

LK_RLCK            Same  as LK_LOCK.

LKNBLCK            Lock  the region. If the lock ifunsuccessful, give up
                   immediately.

LK  NBRLCK         Same  as LK  NBLCK.      .
LK_UNLCK           Unlock  the region, which must  have been  previously
                   locked.

Portability

DOSUNIXWindowsANSI CC++ only
■■

Return value

On successful operations, locking returns 0. Otherwise, it returns -1, and the global variable errno is set to one of the following:

EBADFBad file number

EACCESS File already locked or unlocked

EDEADLOCKFile cannot be locked after 10 retries (cmd is
LK_LOCKor LK_RLCK)
EINVALInvalid cmd, or SHARE.EXEnot loaded

See also

Example

#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
#include <sys\locking.h>
int main (void)
{
   int handle, status;
   long length;

   /* must have DOS SHARE.EXE  loaded for file locking to function  */
   handle = sopen ("c:\\autoexec.bat", 0_RDONLY,SH_DENYNO) ;
   if (handle < 0)  {
      printf ("sopen failed\n");
      exit(l);
   }
   length = filelength (handle);
   status = locking(handle,LK_LOCK, length/2);
   if (status == 0)
      printf ("lock succeeded\n");
   else
      perror("lock failed");
   status = locking (handle,LK_UNLCK,length/2);
   if (status == 0)
      printf ("unlock succeeded\n");
   else
      perror("unlock  failed");
   close(handle);
   return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.