1#include <fcntl.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
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
13
14int fd;
15
16
17static void
18do_prepare (void)
19{
20 fd = create_temp_file (base: "tst-eof.", NULL);
21 if (fd == -1)
22 {
23 printf (format: "cannot create temporary file: %m\n");
24 exit (1);
25 }
26}
27
28
29static int
30do_test (void)
31{
32 char buf[40];
33 FILE *fp;
34
35 if (write (fd, "some string\n", 12) != 12)
36 {
37 printf (format: "cannot write temporary file: %m\n");
38 return 1;
39 }
40
41 if (lseek (fd: fd, offset: 0, SEEK_SET) == (off_t) -1)
42 {
43 printf (format: "cannot reposition temporary file: %m\n");
44 return 1;
45 }
46
47 fp = fdopen (fd, "r");
48 if (fp == NULL)
49 {
50 printf (format: "cannot create stream: %m\n");
51 return 1;
52 }
53
54 if (feof (stream: fp))
55 {
56 puts (s: "EOF set after fdopen");
57 return 1;
58 }
59
60 if (fread (ptr: buf, size: 1, n: 20, stream: fp) != 12)
61 {
62 puts (s: "didn't read the correct number of bytes");
63 return 1;
64 }
65
66 if (! feof (stream: fp))
67 {
68 puts (s: "EOF not set after fread");
69 return 1;
70 }
71
72 fclose (fp);
73
74 return 0;
75}
76

source code of glibc/libio/tst-eof.c