#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>

static void die(const char *msg, int code)
{
    perror(msg);
    exit(code);
}

int main(int argc, char **argv)
{
    int fd, size, res;
    char *p;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file name>", argv[0]);
        exit(1);
    }

    fd = open(argv[1], O_RDWR);
    if (fd == -1)
        die("open", 2);
    size = lseek(fd, 0, SEEK_END);

    p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED)
        die("mmap", 2);

    memcpy(p, "john  ", 6);

    munmap(p, size);
    res = close(fd);
    if (res == -1)
        die("close", 2);
    return 0;
}
