Back to index

glibc  2.9
tst-mmap2-eofsync.c
Go to the documentation of this file.
00001 /* Test program for synchronization of stdio state with file after EOF.  */
00002 
00003 #include <stdio.h>
00004 #include <error.h>
00005 #include <errno.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 
00009 static void do_prepare (void);
00010 #define PREPARE(argc, argv) do_prepare ()
00011 static int do_test (void);
00012 #define TEST_FUNCTION do_test ()
00013 #include <test-skeleton.c>
00014 
00015 static char *temp_file;
00016 static int temp_fd;
00017 
00018 static char *pages;
00019 
00020 static void
00021 do_prepare (void)
00022 {
00023   pages = malloc (getpagesize () * 2);
00024   memset (pages, 'a', getpagesize ());
00025   memset (pages + getpagesize (), 'b', getpagesize ());
00026 
00027   temp_fd = create_temp_file ("tst-mmap2-eofsync.", &temp_file);
00028   if (temp_fd == -1)
00029     error (1, errno, "cannot create temporary file");
00030   else
00031     {
00032       ssize_t cc = write (temp_fd, pages, getpagesize ());
00033       if (cc != getpagesize ())
00034        error (1, errno, "cannot write to temporary file");
00035     }
00036 }
00037 
00038 static int
00039 do_test (void)
00040 {
00041   const size_t pagesize = getpagesize ();
00042   FILE *f;
00043   char buf[pagesize];
00044   int result = 0;
00045   int c;
00046 
00047   f = fopen (temp_file, "rm");
00048   if (f == NULL)
00049     {
00050       perror (temp_file);
00051       return 1;
00052     }
00053 
00054   if (fread (buf, pagesize, 1, f) != 1)
00055     {
00056       perror ("fread");
00057       return 1;
00058     }
00059 
00060   if (memcmp (buf, pages, pagesize))
00061     {
00062       puts ("data mismatch in page 1");
00063       result = 1;
00064     }
00065 
00066   printf ("feof = %d, ferror = %d immediately after fread\n",
00067          feof (f), ferror (f));
00068 
00069   c = fgetc (f);
00070   if (c == EOF)
00071     printf ("fgetc -> EOF (feof = %d, ferror = %d)\n",
00072            feof (f), ferror (f));
00073   else
00074     {
00075       printf ("fgetc returned %o (feof = %d, ferror = %d)\n",
00076              c, feof (f), ferror (f));
00077       result = 1;
00078     }
00079 
00080   c = write (temp_fd, pages + pagesize, pagesize);
00081   if (c == (ssize_t) pagesize)
00082     printf ("wrote more to file\n");
00083   else
00084     {
00085       printf ("wrote %d != %zd (%m)\n", c, pagesize);
00086       result = 1;
00087     }
00088 
00089   if (fread (buf, pagesize, 1, f) != 1)
00090     {
00091       printf ("second fread fails: feof = %d, ferror = %d (%m)\n",
00092              feof (f), ferror (f));
00093       clearerr (f);
00094       if (fread (buf, pagesize, 1, f) != 1)
00095        {
00096          printf ("retry fread fails: feof = %d, ferror = %d (%m)\n",
00097                 feof (f), ferror (f));
00098          result = 1;
00099        }
00100     }
00101   if (result == 0 && memcmp (buf, pages + pagesize, pagesize))
00102     {
00103       puts ("data mismatch in page 2");
00104       result = 1;
00105     }
00106 
00107   fseek (f, pagesize - 1, SEEK_SET);
00108   c = fgetc (f);
00109   if (c != 'a')
00110     {
00111       printf ("fgetc at end of page 1 read '%c' (%m)\n", c);
00112       result = 1;
00113     }
00114 
00115   if (ftruncate (temp_fd, pagesize) < 0)
00116     {
00117       printf ("ftruncate failed: %m\n");
00118       result = 1;
00119     }
00120 
00121   fflush (f);
00122 
00123   c = fgetc (f);
00124   if (c == EOF)
00125     printf ("after truncate fgetc -> EOF (feof = %d, ferror = %d)\n",
00126            feof (f), ferror (f));
00127   else
00128     {
00129       printf ("after truncate fgetc returned '%c' (feof = %d, ferror = %d)\n",
00130              c, feof (f), ferror (f));
00131       result = 1;
00132     }
00133 
00134   fclose (f);
00135 
00136   return result;
00137 }