|
Packit |
6c4009 |
/* Test program for synchronization of stdio state with file after EOF. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <error.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void do_prepare (void);
|
|
Packit |
6c4009 |
#define PREPARE(argc, argv) do_prepare ()
|
|
Packit |
6c4009 |
static int do_test (void);
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include <test-skeleton.c>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char *temp_file;
|
|
Packit |
6c4009 |
static int temp_fd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char *pages;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
do_prepare (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
pages = xmalloc (getpagesize () * 2);
|
|
Packit |
6c4009 |
memset (pages, 'a', getpagesize ());
|
|
Packit |
6c4009 |
memset (pages + getpagesize (), 'b', getpagesize ());
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
temp_fd = create_temp_file ("tst-mmap2-eofsync.", &temp_file);
|
|
Packit |
6c4009 |
if (temp_fd == -1)
|
|
Packit |
6c4009 |
error (1, errno, "cannot create temporary file");
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ssize_t cc = write (temp_fd, pages, getpagesize ());
|
|
Packit |
6c4009 |
if (cc != getpagesize ())
|
|
Packit |
6c4009 |
error (1, errno, "cannot write to temporary file");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const size_t pagesize = getpagesize ();
|
|
Packit |
6c4009 |
FILE *f;
|
|
Packit |
6c4009 |
char buf[pagesize];
|
|
Packit |
6c4009 |
int result = 0;
|
|
Packit |
6c4009 |
int c;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
f = fopen (temp_file, "rm");
|
|
Packit |
6c4009 |
if (f == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
perror (temp_file);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (fread (buf, pagesize, 1, f) != 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
perror ("fread");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (memcmp (buf, pages, pagesize))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("data mismatch in page 1");
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("feof = %d, ferror = %d immediately after fread\n",
|
|
Packit |
6c4009 |
feof (f), ferror (f));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
c = fgetc (f);
|
|
Packit |
6c4009 |
if (c == EOF)
|
|
Packit |
6c4009 |
printf ("fgetc -> EOF (feof = %d, ferror = %d)\n",
|
|
Packit |
6c4009 |
feof (f), ferror (f));
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fgetc returned %o (feof = %d, ferror = %d)\n",
|
|
Packit |
6c4009 |
c, feof (f), ferror (f));
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
c = write (temp_fd, pages + pagesize, pagesize);
|
|
Packit |
6c4009 |
if (c == (ssize_t) pagesize)
|
|
Packit |
6c4009 |
printf ("wrote more to file\n");
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("wrote %d != %zd (%m)\n", c, pagesize);
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (fread (buf, pagesize, 1, f) != 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("second fread fails: feof = %d, ferror = %d (%m)\n",
|
|
Packit |
6c4009 |
feof (f), ferror (f));
|
|
Packit |
6c4009 |
clearerr (f);
|
|
Packit |
6c4009 |
if (fread (buf, pagesize, 1, f) != 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("retry fread fails: feof = %d, ferror = %d (%m)\n",
|
|
Packit |
6c4009 |
feof (f), ferror (f));
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (result == 0 && memcmp (buf, pages + pagesize, pagesize))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("data mismatch in page 2");
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fseek (f, pagesize - 1, SEEK_SET);
|
|
Packit |
6c4009 |
c = fgetc (f);
|
|
Packit |
6c4009 |
if (c != 'a')
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fgetc at end of page 1 read '%c' (%m)\n", c);
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ftruncate (temp_fd, pagesize) < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("ftruncate failed: %m\n");
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fflush (f);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
c = fgetc (f);
|
|
Packit |
6c4009 |
if (c == EOF)
|
|
Packit |
6c4009 |
printf ("after truncate fgetc -> EOF (feof = %d, ferror = %d)\n",
|
|
Packit |
6c4009 |
feof (f), ferror (f));
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("after truncate fgetc returned '%c' (feof = %d, ferror = %d)\n",
|
|
Packit |
6c4009 |
c, feof (f), ferror (f));
|
|
Packit |
6c4009 |
result = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fclose (f);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|