Back to index

glibc  2.9
bug-fopena+.c
Go to the documentation of this file.
00001 #include <fcntl.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 
00006 static int fd;
00007 static char *fname;
00008 
00009 
00010 static void prepare (void);
00011 #define PREPARE(argc, argv) prepare ()
00012 
00013 
00014 #define TEST_FUNCTION do_test ()
00015 static int do_test (void);
00016 #include "../test-skeleton.c"
00017 
00018 
00019 static void
00020 prepare (void)
00021 {
00022   fd = create_temp_file ("wrewind.", &fname);
00023   if (fd == -1)
00024     exit (3);
00025 }
00026 
00027 
00028 static int
00029 do_test (void)
00030 {
00031   char buf[100];
00032   FILE *fp;
00033   int result = 0;
00034 
00035   fp = fdopen (fd, "w");
00036   if (fp == NULL)
00037     {
00038       puts ("cannot create file");
00039       exit (1);
00040     }
00041 
00042   if (fputs ("one\n", fp) == EOF || fputs ("two\n", fp) == EOF)
00043     {
00044       puts ("cannot create filec content");
00045       exit (1);
00046     }
00047 
00048   fclose (fp);
00049 
00050   fp = fopen (fname, "a+");
00051   if (fp == NULL)
00052     {
00053       puts ("cannot fopen a+");
00054       exit (1);
00055     }
00056 
00057   if (fgets (buf, sizeof (buf), fp) == NULL)
00058     {
00059       puts ("cannot read after fopen a+");
00060       exit (1);
00061     }
00062 
00063   if (strcmp (buf, "one\n") != 0)
00064     {
00065       puts ("read after fopen a+ produced wrong result");
00066       result = 1;
00067     }
00068 
00069   fclose (fp);
00070 
00071   fd = open (fname, O_RDWR);
00072   if (fd == -1)
00073     {
00074       puts ("open failed");
00075       exit (1);
00076     }
00077 
00078   fp = fdopen (fd, "a+");
00079   if (fp == NULL)
00080     {
00081       puts ("fopen after open failed");
00082       exit (1);
00083     }
00084 
00085   if (fgets (buf, sizeof (buf), fp) == NULL)
00086     {
00087       puts ("cannot read after fdopen a+");
00088       exit (1);
00089     }
00090 
00091   if (strcmp (buf, "one\n") != 0)
00092     {
00093       puts ("read after fdopen a+ produced wrong result");
00094       result = 1;
00095     }
00096 
00097   fclose (fp);
00098 
00099   return result;
00100 }