Back to index

glibc  2.9
tst-mmap-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 
00007 static void do_prepare (void);
00008 #define PREPARE(argc, argv) do_prepare ()
00009 static int do_test (void);
00010 #define TEST_FUNCTION do_test ()
00011 #include <test-skeleton.c>
00012 
00013 static char *temp_file;
00014 static int temp_fd;
00015 
00016 static char text1[] = "Line the first\n";
00017 static char text2[] = "Line the second\n";
00018 
00019 static void
00020 do_prepare (void)
00021 {
00022   temp_fd = create_temp_file ("tst-mmap-eofsync.", &temp_file);
00023   if (temp_fd == -1)
00024     error (1, errno, "cannot create temporary file");
00025   else
00026     {
00027       ssize_t cc = write (temp_fd, text1, sizeof text1 - 1);
00028       if (cc != sizeof text1 - 1)
00029        error (1, errno, "cannot write to temporary file");
00030     }
00031 }
00032 
00033 static int
00034 do_test (void)
00035 {
00036   FILE *f;
00037   char buf[128];
00038   int result = 0;
00039   int c;
00040 
00041   f = fopen (temp_file, "rm");
00042   if (f == NULL)
00043     {
00044       perror (temp_file);
00045       return 1;
00046     }
00047 
00048   if (fgets (buf, sizeof buf, f) == NULL)
00049     {
00050       perror ("fgets");
00051       return 1;
00052     }
00053 
00054   if (strcmp (buf, text1))
00055     {
00056       printf ("read \"%s\", expected \"%s\"\n", buf, text1);
00057       result = 1;
00058     }
00059 
00060   printf ("feof = %d, ferror = %d immediately after fgets\n",
00061          feof (f), ferror (f));
00062 
00063 #if 1
00064   c = fgetc (f);
00065   if (c == EOF)
00066     printf ("fgetc -> EOF (feof = %d, ferror = %d)\n",
00067            feof (f), ferror (f));
00068   else
00069     {
00070       printf ("fgetc returned %o (feof = %d, ferror = %d)\n",
00071              c, feof (f), ferror (f));
00072       result = 1;
00073     }
00074 #endif
00075 
00076   c = write (temp_fd, text2, sizeof text2 - 1);
00077   if (c == sizeof text2 - 1)
00078     printf ("wrote more to file\n");
00079   else
00080     {
00081       printf ("wrote %d != %zd (%m)\n", c, sizeof text2 - 1);
00082       result = 1;
00083     }
00084 
00085   if (fgets (buf, sizeof buf, f) == NULL)
00086     {
00087       printf ("second fgets fails: feof = %d, ferror = %d (%m)\n",
00088              feof (f), ferror (f));
00089       clearerr (f);
00090       if (fgets (buf, sizeof buf, f) == NULL)
00091        {
00092          printf ("retry fgets fails: feof = %d, ferror = %d (%m)\n",
00093                 feof (f), ferror (f));
00094          result = 1;
00095        }
00096     }
00097   if (result == 0 && strcmp (buf, text2))
00098     {
00099       printf ("second time read \"%s\", expected \"%s\"\n", buf, text2);
00100       result = 1;
00101     }
00102 
00103   fclose (f);
00104 
00105   return result;
00106 }