.
[glibc/history.git] / libio / tst-mmap-fflushsync.c
blob24ae33cae7f38645fa34b21a0435c0fde5b8b230
1 /* Test program for synchronization of stdio state with file after fflush. */
3 #include <stdio.h>
4 #include <error.h>
5 #include <errno.h>
7 static void do_prepare (void);
8 #define PREPARE(argc, argv) do_prepare ()
9 static int do_test (void);
10 #define TEST_FUNCTION do_test ()
11 #include <test-skeleton.c>
13 static char *temp_file;
14 static int temp_fd;
16 static char text1[] = "Line the first\n";
17 static char text2[] = "Line the second\n";
19 static void
20 do_prepare (void)
22 temp_fd = create_temp_file ("tst-mmap-eofsync.", &temp_file);
23 if (temp_fd == -1)
24 error (1, errno, "cannot create temporary file");
25 else
27 ssize_t cc = write (temp_fd, text1, sizeof text1 - 1);
28 if (cc != sizeof text1 - 1)
29 error (1, errno, "cannot write to temporary file");
33 static int
34 do_test (void)
36 FILE *f;
37 char buf[128];
38 int result = 0;
39 int c;
41 f = fopen (temp_file, "rm");
42 if (f == NULL)
44 perror (temp_file);
45 return 1;
48 if (fgets (buf, sizeof buf, f) == NULL)
50 perror ("fgets");
51 return 1;
54 if (strcmp (buf, text1))
56 printf ("read \"%s\", expected \"%s\"\n", buf, text1);
57 result = 1;
60 printf ("feof = %d, ferror = %d immediately after fgets\n",
61 feof (f), ferror (f));
63 if (fflush (f) != 0)
65 printf ("fflush failed! %m\n");
66 result = 1;
69 c = write (temp_fd, text2, sizeof text2 - 1);
70 if (c == sizeof text2 - 1)
71 printf ("wrote more to file\n");
72 else
74 printf ("wrote %d != %zd (%m)\n", c, sizeof text2 - 1);
75 result = 1;
78 if (fgets (buf, sizeof buf, f) == NULL)
80 printf ("second fgets fails: feof = %d, ferror = %d (%m)\n",
81 feof (f), ferror (f));
82 clearerr (f);
83 if (fgets (buf, sizeof buf, f) == NULL)
85 printf ("retry fgets fails: feof = %d, ferror = %d (%m)\n",
86 feof (f), ferror (f));
87 result = 1;
90 if (result == 0 && strcmp (buf, text2))
92 printf ("second time read \"%s\", expected \"%s\"\n", buf, text2);
93 result = 1;
96 fclose (f);
98 return result;