home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / U4LOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  1.5 KB  |  77 lines

  1.  
  2. /* u4lock.c    (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.    Returns
  5.       0   Normal
  6.      -1   Error
  7.      -2   Locked by other User
  8. */
  9.  
  10. #include "d4base.h"
  11. #include "u4error.h"
  12.  
  13. #ifndef UNIX
  14.    #include <io.h>
  15. #endif
  16. #ifndef TURBO
  17.    #ifdef UNIX
  18.       #include <sys/locking.h>
  19.    #else
  20.       #include <sys\locking.h>
  21.    #endif
  22. #endif
  23.  
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27.  
  28. extern long time( long *) ;
  29.  
  30.  
  31. u4lock( file_handle, o_set, num_bytes, do_wait )
  32. int  file_handle ;
  33. long o_set, num_bytes ;
  34. int  do_wait ;
  35. {
  36.    int    rc ;
  37.  
  38.    errno =  0 ;
  39.  
  40.    #ifdef TURBO
  41.       rc =  lock( file_handle, o_set, num_bytes ) ;
  42.    #else
  43.       lseek( file_handle, o_set, 0 ) ;
  44.       rc =  locking( file_handle, LK_NBLCK, num_bytes) ;
  45.    #endif
  46.  
  47.    if (rc == 0 ||  errno == EINVAL)   return( 0) ;  /* Single User or Success */
  48.  
  49.    #ifndef UNIX
  50.       if (errno != EACCES)
  51.       {
  52.          u4error( E_LOCK, (char *) 0 ) ;
  53.          return( -1) ;
  54.       }
  55.    #endif
  56.  
  57.    if ( ! do_wait )  return( -2 ) ;
  58.  
  59.    while (1)
  60.    {
  61.       #ifdef TURBO
  62.          long old_time ;
  63.  
  64.          time( &old_time) ;   /* wait a second & try lock again */
  65.          while (  time( (long*)0 ) <=  old_time)    ;
  66.  
  67.      if ( lock( file_handle, o_set, num_bytes ) == 0 )
  68.         return( 0) ;
  69.       #else
  70.      lseek( file_handle, o_set, 0 ) ;
  71.      if ( locking( file_handle, LK_LOCK, num_bytes) == 0)
  72.         return( 0) ;
  73.       #endif
  74.    }
  75. }
  76.  
  77.