#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <io.h>

int
main (int argc, char **argv)
{

  FILE *f;
  HANDLE mapping;
  unsigned char *p;

  f = fopen ("test.file", "rwb");

  if (f == NULL)
    fprintf (stderr, "Cannot open test.file\n"), exit (1);

  mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fileno (f)),
			       NULL,
			       PAGE_WRITECOPY,
			       0, 0,
			       NULL);

  if (mapping == NULL)
    fprintf (stderr, "CreateFileMapping failed: %d\n", GetLastError ()), exit (1);

  p = MapViewOfFile (mapping,
		     FILE_MAP_COPY,
		     0, 0,
		     0);
  
  if (p == NULL)
    fprintf (stderr, "MapViewOfFile failed: %d\n", GetLastError ()), exit (1);

  printf ("Start of file: %.10s\n", p);

  printf ("Bytes at 100000: %d %d %d\n", p[10000], p[10001], p[10002]);

  Sleep (10000);

  memcpy (p, "Huh", 3);

  strcpy (p + 10000, "Heipparallaa");

  Sleep (10000);

  printf ("Start of file now: %.10s\n", p);

  printf ("Bytes at 100000: %d %d %d\n", p[10000], p[10001], p[10002]);

  Sleep (10000);

  UnmapViewOfFile (p);

  CloseHandle (mapping);

  fclose (f);

  exit (0);
}
