#include <unistd.h>
#include <fcntl.h>
#include "file_lock.h"

static struct flock lock_it, unlock_it;

void file_lock_init(void) {
    lock_it.l_whence = SEEK_SET;
    lock_it.l_start = 0;
    lock_it.l_len = 0;
    lock_it.l_type = F_WRLCK;
    lock_it.l_pid = 0;
    unlock_it.l_whence = SEEK_SET;
    unlock_it.l_start = 0;
    unlock_it.l_len = 0;
    unlock_it.l_type = F_UNLCK;
    unlock_it.l_pid = 0;
}

int file_trylock(int fd) {
    return fcntl(fd, F_SETLK, &lock_it);
}

int file_lock(int fd) {
    return fcntl(fd, F_SETLKW, &lock_it);
}

int file_unlock(int fd) {
    return fcntl(fd, F_SETLK, &unlock_it);
}


syntax highlighted by Code2HTML, v. 0.9.1