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
7static void do_prepare (void);
8#define PREPARE(argc, argv) do_prepare ()
9static int do_test (void);
10#define TEST_FUNCTION do_test ()
11#include <test-skeleton.c>
12
13static char *temp_file;
14static int temp_fd;
15
16static char text1[] = "Line the first\n";
17static char text2[] = "Line the second\n";
18
19static void
20do_prepare (void)
21{
22 temp_fd = create_temp_file (base: "tst-mmap-eofsync.", filename: &temp_file);
23 if (temp_fd == -1)
24 error (status: 1, errno, format: "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 (status: 1, errno, format: "cannot write to temporary file");
30 }
31}
32
33static int
34do_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 (s: buf, n: sizeof buf, stream: f) == NULL)
49 {
50 perror ("fgets");
51 return 1;
52 }
53
54 if (strcmp (buf, text1))
55 {
56 printf (format: "read \"%s\", expected \"%s\"\n", buf, text1);
57 result = 1;
58 }
59
60 printf (format: "feof = %d, ferror = %d immediately after fgets\n",
61 feof (stream: f), ferror (stream: f));
62
63 c = fgetc (stream: f);
64 if (c == EOF)
65 printf (format: "fgetc -> EOF (feof = %d, ferror = %d)\n",
66 feof (stream: f), ferror (stream: f));
67 else
68 {
69 printf (format: "fgetc returned %o (feof = %d, ferror = %d)\n",
70 c, feof (stream: f), ferror (stream: f));
71 result = 1;
72 }
73
74 c = write (temp_fd, text2, sizeof text2 - 1);
75 if (c == sizeof text2 - 1)
76 printf (format: "wrote more to file\n");
77 else
78 {
79 printf (format: "wrote %d != %zd (%m)\n", c, sizeof text2 - 1);
80 result = 1;
81 }
82
83 if (fgets (s: buf, n: sizeof buf, stream: f) == NULL)
84 {
85 printf (format: "second fgets fails: feof = %d, ferror = %d (%m)\n",
86 feof (stream: f), ferror (stream: f));
87 clearerr (stream: f);
88 if (fgets (s: buf, n: sizeof buf, stream: f) == NULL)
89 {
90 printf (format: "retry fgets fails: feof = %d, ferror = %d (%m)\n",
91 feof (stream: f), ferror (stream: f));
92 result = 1;
93 }
94 }
95 if (result == 0 && strcmp (buf, text2))
96 {
97 printf (format: "second time read \"%s\", expected \"%s\"\n", buf, text2);
98 result = 1;
99 }
100
101 fclose (f);
102
103 return result;
104}
105

source code of glibc/libio/tst-mmap-eofsync.c