FreeBASIC  0.91.0
file_hlock.c
Go to the documentation of this file.
1 /* low-level lock and unlock functions */
2 
3 #include "../fb.h"
4 #include <fcntl.h>
5 
6 static int do_lock(FILE *f, int lock, fb_off_t inipos, fb_off_t size)
7 {
8  struct flock lck;
9  int fd;
10 
11  fd = fileno( f );
12 
13  if( lock ) {
14  if( fcntl( fd, F_GETFL ) & O_RDONLY )
15  lck.l_type = F_RDLCK;
16  else
17  lck.l_type = F_WRLCK;
18  } else {
19  lck.l_type = F_UNLCK;
20  }
21 
22  lck.l_whence = SEEK_SET;
23  lck.l_start = inipos;
24  lck.l_len = size;
25 
26  return fb_ErrorSetNum( fcntl( fd, F_SETLKW, &lck ) ? FB_RTERROR_FILEIO : FB_RTERROR_OK );
27 }
28 
29 int fb_hFileLock( FILE *f, fb_off_t inipos, fb_off_t size )
30 {
31  return do_lock(f, TRUE, inipos, size);
32 }
33 
34 int fb_hFileUnlock( FILE *f, fb_off_t inipos, fb_off_t size )
35 {
36  return do_lock(f, FALSE, inipos, size);
37 }