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