Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mmaptest04.c « winsup.api « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8cc0a61fd124944e41ea54428948129fad031c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>

int
main ()
{
  char *data, *data2, *data3;
  int i, pagesize;
  int fd;

  pagesize = 65536;		//getpagesize();

  /*
   * First, make a file with some known garbage in it.
   */
  data = (char *) malloc (pagesize);
  if (!data)
    exit (1);
  for (i = 0; i < pagesize; ++i)
    *(data + i) = rand ();
  umask (0);
  fd = creat ("conftestmmap", 0600);
  if (fd < 0)
    {
      printf ("creat: %d\n", errno);
      exit (1);
    }
  if (write (fd, data, pagesize) != pagesize)
    {
      printf ("write: %d\n", errno);
      exit (1);
    }
  close (fd);

  /*
   * Next, try to mmap the file.
   */
  fd = open ("conftestmmap", O_RDWR);
  if (fd < 0)
    {
      printf ("write: %d\n", errno);
      exit (1);
    }
  if ((data2 = mmap (data2, pagesize, PROT_READ | PROT_WRITE,
		     MAP_PRIVATE, fd, 0L)) == MAP_FAILED)
    {
      printf ("mmap: %d\n", errno);
      exit (1);
    }
  for (i = 0; i < pagesize; ++i)
    if (*(data + i) != *(data2 + i))
      {
	printf ("check-if: %d\n", errno);
	exit (1);
      }

  /*
   * Finally, make sure that changes to the mapped area
   * do not percolate back to the file as seen by read().
   * (This is a bug on some variants of i386 svr4.0.)
   */
  for (i = 0; i < pagesize; ++i)
    *(data2 + i) = *(data2 + i) + 1;
  data3 = (char *) malloc (pagesize);
  if (!data3)
    {
      printf ("malloc2: %d\n", errno);
      exit (1);
    }
  if (read (fd, data3, pagesize) != pagesize)
    {
      printf ("read: %d\n", errno);
      exit (1);
    }
  for (i = 0; i < pagesize; ++i)
    if (*(data + i) != *(data3 + i))
      {
	printf ("check-if2: %d\n", errno);
	exit (1);
      }
  if (msync (data2, pagesize, MS_SYNC))
    {
      printf ("msync: %d\n", errno);
      exit (1);
    }
  if (munmap (data2, pagesize))
    {
      printf ("munmap: %d\n", errno);
      exit (1);
    }
  close (fd);
  unlink ("conftestmmap");
  exit (0);
}