#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

main(int argc, char **argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s FILE\n", argv[0]);
        exit(1);
    }
    int fd = open(argv[1], O_WRONLY);
    if (fd < 0)
    {
        perror("Could not open file");
        exit(1);
    }
    struct flock fl;
    fl.l_type = F_WRLCK;
    fl.l_whence=SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    int ret = fcntl(fd, F_GETLK, &fl);
    if (ret < 0)
    {
        perror("F_GETLK failed");
        exit(1);
    }
    if (fl.l_type == F_UNLCK)
    {
        printf("requested lock is attainable\n");
    }
    else
    {
        printf("Lock held by pid %d\n", fl.l_pid);
    }
}
