98acaed83f7759a41e2fae9605e68cf4ac2fc027
[platform/upstream/glibc.git] / stdio-common / tst-fdopen.c
1 /* Test for fdopen bugs.  */
2
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6
7 #define assert(x) \
8   if (!(x)) \
9     { \
10       fputs ("test failed: " #x "\n", stderr); \
11       retval = 1; \
12       goto the_end; \
13     }
14
15 char buffer[256];
16
17 int
18 main (int argc, char *argv[])
19 {
20   char *name;
21   FILE *fp = NULL;
22   int retval = 0;
23   int fd;
24
25   name = tmpnam (NULL);
26   fp = fopen (name, "w");
27   assert (fp != NULL)
28   fputs ("foobar and baz", fp);
29   fclose (fp);
30   fp = NULL;
31
32   fd = open (name, O_RDONLY);
33   assert (fd != -1);
34   assert (lseek (fd, 5, SEEK_SET) == 5);
35   /* The file position indicator associated with the new stream is set to
36      the position indicated by the file offset associated with the file
37      descriptor.  */
38   fp = fdopen (fd, "r");
39   assert (fp != NULL);
40   assert (getc (fp) == 'r');
41   assert (getc (fp) == ' ');
42
43 the_end:
44   if (fp != NULL)
45     fclose (fp);
46   unlink (name);
47
48   return retval;
49 }