home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-02  |  2.4 KB  |  90 lines

  1. /* LOCK.C illustrates network file sharing functions:
  2.  *      sopen       locking
  3.  *
  4.  * Also the global variable:
  5.  *      _osmajor
  6.  *
  7.  * The program requires DOS 3.0 or higher. The DOS SHARE command must
  8.  * be loaded. The locking mechanism will only work if the program is
  9.  * run from a network drive.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <io.h>
  15. #include <fcntl.h>
  16. #include <sys\types.h>
  17. #include <sys\stat.h>
  18. #include <sys\locking.h>
  19. #include <share.h>
  20. #include <stdlib.h>         /* For _osmajor and exit */
  21.  
  22. void error( char *msg );
  23.  
  24. main( int argc, char *argv[] )
  25. {
  26.     int handle, i;
  27.     static char msg[] = "Are any of these bytes locked?";
  28.     char buf[1];
  29.  
  30.     /* Check for DOS version >= 3.0 */
  31.     if( _osmajor < 3 )
  32.         error( "Must be DOS 3.0 or higher" );
  33.  
  34.     /* If no argument, write file and lock some bytes in it. */
  35.     if( argc == 1 )
  36.     {
  37.         /* Open file with deny none sharing. */
  38.         handle = sopen( "tmpfil", O_BINARY | O_RDWR | O_CREAT,
  39.                                   SH_DENYNO, S_IREAD | S_IWRITE );
  40.         if( handle == -1 )
  41.             error( "Can't open file\n" );
  42.  
  43.         write( handle, msg, sizeof( msg ) - 1 );
  44.  
  45.         /* Lock 10 bytes starting at byte 10. */
  46.         lseek( handle, 10L, SEEK_SET );
  47.         if( locking( handle, LK_LOCK, 10L ) )
  48.             error( "Locking failed\n" );
  49.         printf( "Locked 10 bytes starting at byte 10\n" );
  50.  
  51.         system( "LOCK read" );
  52.         getch();
  53.  
  54.         /* Unlock. */
  55.         lseek( handle, 10L, SEEK_SET );
  56.         locking( handle, LK_UNLCK, 10L );
  57.         printf( "\nUnlocked 10 bytes starting at byte 10\n" );
  58.  
  59.         system( "LOCK read" );
  60.         getch();
  61.     }
  62.  
  63.     /* If argument, Try to read some locked bytes. */
  64.     else
  65.     {
  66.         /* Open file with deny none sharing. */
  67.         handle = sopen( "tmpfil", O_BINARY | O_RDWR,
  68.                                   SH_DENYNO, S_IREAD | S_IWRITE );
  69.         if( handle == -1 )
  70.             error( "Can't open file\n" );
  71.  
  72.         for( i = 0; i < sizeof( msg ) - 1; i++ )
  73.         {
  74.             /* Print characters until locked bytes are reached. */
  75.             if( read( handle, buf, 1 ) == -1 )
  76.                 break;
  77.             else
  78.                 putchar( *buf );
  79.         }
  80.     }
  81.     close( handle );
  82.     exit( 0 );
  83. }
  84.  
  85. void error( char *errmsg )
  86. {
  87.     printf( errmsg );
  88.     exit( 1 );
  89. }
  90.