#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <aio.h>
#include <signal.h>
#include <errno.h>

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

int main(void) {
	int fd, i, syncer, syncerr;
	void *buf;
	struct aiocb aio;
	aio.aio_sigevent.sigev_notify = SIGEV_NONE;

	fd = open("foo", O_CREAT | O_RDWR, S_IRWXU);
	aio.aio_fildes = fd;
	buf = malloc(512);
	for (i = 0; i < 10240; i++) write(fd, buf, 512);

	syncer = aio_fsync(O_SYNC, &aio);
	syncerr = errno;
	printf("%d %d\n", syncer, syncerr);

	i = 0;
	while((syncerr = aio_error(&aio)) == EINPROGRESS) {
		printf("Round %d, in progress\n", ++i);
	}
	printf("Final error: %d\n", syncerr);
	printf("Return: %d\n", (int) aio_return(&aio));
}
