Back to index

glibc  2.9
tst-fdopen.c
Go to the documentation of this file.
00001 /* Test for fdopen bugs.  */
00002 
00003 #include <stdio.h>
00004 #include <unistd.h>
00005 #include <fcntl.h>
00006 
00007 #undef assert
00008 #define assert(x) \
00009   if (!(x)) \
00010     { \
00011       fputs ("test failed: " #x "\n", stderr); \
00012       retval = 1; \
00013       goto the_end; \
00014     }
00015 
00016 char buffer[256];
00017 
00018 int
00019 main (int argc, char *argv[])
00020 {
00021   char *name;
00022   FILE *fp = NULL;
00023   int retval = 0;
00024   int fd;
00025 
00026   name = tmpnam (NULL);
00027   fp = fopen (name, "w");
00028   assert (fp != NULL)
00029   fputs ("foobar and baz", fp);
00030   fclose (fp);
00031   fp = NULL;
00032 
00033   fd = open (name, O_RDONLY);
00034   assert (fd != -1);
00035   assert (lseek (fd, 5, SEEK_SET) == 5);
00036   /* The file position indicator associated with the new stream is set to
00037      the position indicated by the file offset associated with the file
00038      descriptor.  */
00039   fp = fdopen (fd, "r");
00040   assert (fp != NULL);
00041   assert (getc (fp) == 'r');
00042   assert (getc (fp) == ' ');
00043 
00044 the_end:
00045   if (fp != NULL)
00046     fclose (fp);
00047   unlink (name);
00048 
00049   return retval;
00050 }