1 | /* Test program for synchronization of stdio state with file after EOF. */
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <error.h>
|
---|
5 | #include <errno.h>
|
---|
6 |
|
---|
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>
|
---|
12 |
|
---|
13 | static char *temp_file;
|
---|
14 | static int temp_fd;
|
---|
15 |
|
---|
16 | static char text1[] = "Line the first\n";
|
---|
17 | static char text2[] = "Line the second\n";
|
---|
18 |
|
---|
19 | static void
|
---|
20 | do_prepare (void)
|
---|
21 | {
|
---|
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
|
---|
26 | {
|
---|
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");
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | static int
|
---|
34 | do_test (void)
|
---|
35 | {
|
---|
36 | FILE *f;
|
---|
37 | char buf[128];
|
---|
38 | int result = 0;
|
---|
39 | int c;
|
---|
40 |
|
---|
41 | f = fopen (temp_file, "rm");
|
---|
42 | if (f == NULL)
|
---|
43 | {
|
---|
44 | perror (temp_file);
|
---|
45 | return 1;
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (fgets (buf, sizeof buf, f) == NULL)
|
---|
49 | {
|
---|
50 | perror ("fgets");
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (strcmp (buf, text1))
|
---|
55 | {
|
---|
56 | printf ("read \"%s\", expected \"%s\"\n", buf, text1);
|
---|
57 | result = 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | printf ("feof = %d, ferror = %d immediately after fgets\n",
|
---|
61 | feof (f), ferror (f));
|
---|
62 |
|
---|
63 | #if 1
|
---|
64 | c = fgetc (f);
|
---|
65 | if (c == EOF)
|
---|
66 | printf ("fgetc -> EOF (feof = %d, ferror = %d)\n",
|
---|
67 | feof (f), ferror (f));
|
---|
68 | else
|
---|
69 | {
|
---|
70 | printf ("fgetc returned %o (feof = %d, ferror = %d)\n",
|
---|
71 | c, feof (f), ferror (f));
|
---|
72 | result = 1;
|
---|
73 | }
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | c = write (temp_fd, text2, sizeof text2 - 1);
|
---|
77 | if (c == sizeof text2 - 1)
|
---|
78 | printf ("wrote more to file\n");
|
---|
79 | else
|
---|
80 | {
|
---|
81 | printf ("wrote %d != %zd (%m)\n", c, sizeof text2 - 1);
|
---|
82 | result = 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (fgets (buf, sizeof buf, f) == NULL)
|
---|
86 | {
|
---|
87 | printf ("second fgets fails: feof = %d, ferror = %d (%m)\n",
|
---|
88 | feof (f), ferror (f));
|
---|
89 | clearerr (f);
|
---|
90 | if (fgets (buf, sizeof buf, f) == NULL)
|
---|
91 | {
|
---|
92 | printf ("retry fgets fails: feof = %d, ferror = %d (%m)\n",
|
---|
93 | feof (f), ferror (f));
|
---|
94 | result = 1;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if (result == 0 && strcmp (buf, text2))
|
---|
98 | {
|
---|
99 | printf ("second time read \"%s\", expected \"%s\"\n", buf, text2);
|
---|
100 | result = 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | fclose (f);
|
---|
104 |
|
---|
105 | return result;
|
---|
106 | }
|
---|