Update.
[platform/upstream/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 int do_test (void);
8 #define TEST_FUNCTION do_test ()
9 #include <test-skeleton.c>
10
11
12 static int
13 do_test (void)
14 {
15   char *buf;
16   int fd;
17   FILE *fp;
18   int ch;
19   char tm[20];
20
21   buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-eof.XXXXXX");
22   if (buf == NULL)
23     {
24       printf ("cannot allocate memory: %m\n");
25       return 1;
26     }
27   stpcpy (stpcpy (buf, test_dir), "/tst-eof.XXXXXX");
28
29   fd = mkstemp (buf);
30   if (fd == -1)
31     {
32       printf ("cannot open temporary file: %m\n");
33       return 1;
34     }
35
36   /* Make sure it gets removed.  */
37   add_temp_file (buf);
38
39   if (write (fd, "some string\n", 12) != 12)
40     {
41       printf ("cannot write temporary file: %m\n");
42       return 1;
43     }
44
45   if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
46     {
47       printf ("cannot reposition temporary file: %m\n");
48       return 1;
49     }
50
51   fp = fdopen (fd, "r");
52   if (fp == NULL)
53     {
54       printf ("cannot create stream: %m\n");
55       return 1;
56     }
57
58   if (feof (fp))
59     {
60       puts ("EOF set after fdopen");
61       return 1;
62     }
63
64   if (fread (buf, 1, 20, fp) != 12)
65     {
66       puts ("didn't read the correct number of bytes");
67       return 1;
68     }
69
70   if (! feof (fp))
71     {
72       puts ("EOF not set after fread");
73       return 1;
74     }
75
76   fclose (fp);
77
78   return 0;
79 }