#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#define ATOF atol

main(int ac, char *av[])
{
        int fd;
        off_t offset,pos;
        unsigned long new;
        unsigned long old;
        char *endptr;

        fd = open(av[1],O_RDWR,0666);
        if (-1 == fd) { perror("open"); exit(1); }
        offset = ATOF(av[2]);
        new = strtoul (av[3], &endptr, 16);
        if (offset != (pos = lseek(fd,offset,SEEK_SET)))
                { perror("lseek"); exit(2); }
        if (-1 == read(fd, &old, sizeof(long)))
                { perror("read"); exit(2); }
        printf("OLD:%lX\n",old);
        printf("NEW:%lX\n",new);
        if (offset != (pos = lseek(fd,offset,SEEK_SET)))
                { perror("lseek"); exit(2); }
        printf("Writing: %lX instead of %lX : ", new, old);
        fflush(stdout);
        sleep(5);
        if (-1 == write(fd, &new, sizeof(long)))
                { perror("write"); exit(2); }
        if (offset != (pos = lseek(fd,offset,SEEK_SET)))
                { perror("lseek"); exit(2); }
        if (-1 == read(fd, &old, sizeof(long)))
                { perror("read"); exit(2); }
        printf("CHK:%lX\n",old);
        close(fd);
}
