Back to index

glibc  2.9
bug-memstream1.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 
00004 
00005 static int
00006 do_test (void)
00007 {
00008   size_t size;
00009   char *buf;
00010   FILE *fp = open_memstream (&buf, &size);
00011   if (fp == NULL)
00012     {
00013       puts ("open_memstream failed");
00014       return 1;
00015     }
00016 
00017   off64_t off = ftello64 (fp);
00018   if (off != 0)
00019     {
00020       puts ("initial position wrong");
00021       return 1;
00022     }
00023 
00024   if (fseek (fp, 32768, SEEK_SET) != 0)
00025     {
00026       puts ("fseek failed");
00027       return 1;
00028     }
00029 
00030   if (fputs ("foo", fp) == EOF)
00031     {
00032       puts ("fputs failed");
00033       return 1;
00034     }
00035 
00036   if (fclose (fp) == EOF)
00037     {
00038       puts ("fclose failed");
00039       return 1;
00040     }
00041 
00042   if (size != 32768 + 3)
00043     {
00044       printf ("expected size %d, got %zu\n", 32768 + 3, size);
00045       return 1;
00046     }
00047 
00048   for (int i = 0; i < 32768; ++i)
00049     if (buf[i] != '\0')
00050       {
00051        printf ("byte at offset %d is %#hhx\n", i, buf[i]);
00052        return 1;
00053       }
00054 
00055   if (memcmp (buf + 32768, "foo", 3) != 0)
00056     {
00057       puts ("written string incorrect");
00058       return 1;
00059     }
00060 
00061   /* Mark the buffer.  */
00062   memset (buf, 'A', size);
00063   free (buf);
00064 
00065   /* Try again, this time with write mode enabled before the seek.  */
00066   fp = open_memstream (&buf, &size);
00067   if (fp == NULL)
00068     {
00069       puts ("2nd open_memstream failed");
00070       return 1;
00071     }
00072 
00073   off = ftello64 (fp);
00074   if (off != 0)
00075     {
00076       puts ("2nd initial position wrong");
00077       return 1;
00078     }
00079 
00080   if (fputs ("bar", fp) == EOF)
00081     {
00082       puts ("2nd fputs failed");
00083       return 1;
00084     }
00085 
00086   if (fseek (fp, 32768, SEEK_SET) != 0)
00087     {
00088       puts ("2nd fseek failed");
00089       return 1;
00090     }
00091 
00092   if (fputs ("foo", fp) == EOF)
00093     {
00094       puts ("3rd fputs failed");
00095       return 1;
00096     }
00097 
00098   if (fclose (fp) == EOF)
00099     {
00100       puts ("2nd fclose failed");
00101       return 1;
00102     }
00103 
00104   if (size != 32768 + 3)
00105     {
00106       printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
00107       return 1;
00108     }
00109 
00110   if (memcmp (buf, "bar", 3) != 0)
00111     {
00112       puts ("initial string incorrect in 2nd try");
00113       return 1;
00114     }
00115 
00116   for (int i = 3; i < 32768; ++i)
00117     if (buf[i] != '\0')
00118       {
00119        printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
00120        return 1;
00121       }
00122 
00123   if (memcmp (buf + 32768, "foo", 3) != 0)
00124     {
00125       puts ("written string incorrect in 2nd try");
00126       return 1;
00127     }
00128 
00129   return 0;
00130 }
00131 
00132 #define TEST_FUNCTION do_test ()
00133 #include "../test-skeleton.c"