1 /* Test program for synchronization of stdio state with file after EOF. */
9 static void do_prepare (void);
10 #define PREPARE(argc, argv) do_prepare ()
11 static int do_test (void);
12 #define TEST_FUNCTION do_test ()
13 #include <test-skeleton.c>
15 static char *temp_file;
23 pages = malloc (getpagesize () * 2);
24 memset (pages, 'a', getpagesize ());
25 memset (pages + getpagesize (), 'b', getpagesize ());
27 temp_fd = create_temp_file ("tst-mmap2-eofsync.", &temp_file);
29 error (1, errno, "cannot create temporary file");
32 ssize_t cc = write (temp_fd, pages, getpagesize ());
33 if (cc != getpagesize ())
34 error (1, errno, "cannot write to temporary file");
41 const size_t pagesize = getpagesize ();
47 f = fopen (temp_file, "rm");
54 if (fread (buf, pagesize, 1, f) != 1)
60 if (memcmp (buf, pages, pagesize))
62 puts ("data mismatch in page 1");
66 printf ("feof = %d, ferror = %d immediately after fread\n",
67 feof (f), ferror (f));
71 printf ("fgetc -> EOF (feof = %d, ferror = %d)\n",
72 feof (f), ferror (f));
75 printf ("fgetc returned %o (feof = %d, ferror = %d)\n",
76 c, feof (f), ferror (f));
80 c = write (temp_fd, pages + pagesize, pagesize);
81 if (c == (ssize_t) pagesize)
82 printf ("wrote more to file\n");
85 printf ("wrote %d != %zd (%m)\n", c, pagesize);
89 if (fread (buf, pagesize, 1, f) != 1)
91 printf ("second fread fails: feof = %d, ferror = %d (%m)\n",
92 feof (f), ferror (f));
94 if (fread (buf, pagesize, 1, f) != 1)
96 printf ("retry fread fails: feof = %d, ferror = %d (%m)\n",
97 feof (f), ferror (f));
101 if (result == 0 && memcmp (buf, pages + pagesize, pagesize))
103 puts ("data mismatch in page 2");
107 fseek (f, pagesize - 1, SEEK_SET);
111 printf ("fgetc at end of page 1 read '%c' (%m)\n", c);
115 if (ftruncate (temp_fd, pagesize) < 0)
117 printf ("ftruncate failed: %m\n");
125 printf ("after truncate fgetc -> EOF (feof = %d, ferror = %d)\n",
126 feof (f), ferror (f));
129 printf ("after truncate fgetc returned '%c' (feof = %d, ferror = %d)\n",
130 c, feof (f), ferror (f));