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