#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	int error, fd;
	char *data;

	chflags("/tmp/wotcha", 0);
	unlink("/tmp/wotcha");
	fd = open("/tmp/wotcha", O_CREAT|O_WRONLY|O_TRUNC, 0666);
	if (fd == -1) {
		perror("open");
		return (-1);
	}

	error = ftruncate(fd, 1024);
	if (error == -1) {
		perror("close");
		return (-1);
	}

/*
	error = fchflags(fd, UF_APPEND);
	if (error == -1) {
		perror("fchflags");
		return (-1);
	}
*/

/*
	fprintf(stderr, "close the 1\n");
	error = close(fd);
	if (error == -1) {
		perror("close");
		return (-1);
	}
*/

	fd = open("/tmp/wotcha", O_RDWR, 0);
	if (fd == -1) {
		perror("open2");
		return (-1);
	}

	error = fchflags(fd, UF_APPEND);
	if (error == -1) {
		perror("fchflags");
		return (-1);
	}

	fprintf(stderr, "mmap\n");
	data = mmap(NULL, 1024, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
	if (data == NULL) {
		perror("mmap");
		return (-1);
	}

	fprintf(stderr, "read\n");
	printf("data[0]: %x\n", data[0]);

/*
	error = fchflags(fd, UF_APPEND);
	if (error == -1) {
		perror("fchflags");
		return (-1);
	}
*/

	fprintf(stderr, "write\n");
	data[0] = 'f';
	fprintf(stderr, "read2\n");
	printf("data[0]: %x\n", data[0]);

	fprintf(stderr, "close the 2\n");
	error = close(fd);
	if (error == -1) {
		perror("close2");
		return (-1);
	}

	return (0);
}
