NEWS: BZ#18319 was fixed in commit ed159672eb3cd650a32b7e5cb4d5ec1fe0e63802
[glibc.git] / libio / bug-memstream1.c
blob8af36fee2daa6560f0bbbd9675fe87b5af095d9c
1 #include <stdio.h>
2 #include <string.h>
5 static int
6 do_test (void)
8 size_t size;
9 char *buf;
10 FILE *fp = open_memstream (&buf, &size);
11 if (fp == NULL)
13 puts ("open_memstream failed");
14 return 1;
17 off64_t off = ftello64 (fp);
18 if (off != 0)
20 puts ("initial position wrong");
21 return 1;
24 if (fseek (fp, 32768, SEEK_SET) != 0)
26 puts ("fseek failed");
27 return 1;
30 if (fputs ("foo", fp) == EOF)
32 puts ("fputs failed");
33 return 1;
36 if (fclose (fp) == EOF)
38 puts ("fclose failed");
39 return 1;
42 if (size != 32768 + 3)
44 printf ("expected size %d, got %zu\n", 32768 + 3, size);
45 return 1;
48 for (int i = 0; i < 32768; ++i)
49 if (buf[i] != '\0')
51 printf ("byte at offset %d is %#hhx\n", i, buf[i]);
52 return 1;
55 if (memcmp (buf + 32768, "foo", 3) != 0)
57 puts ("written string incorrect");
58 return 1;
61 /* Mark the buffer. */
62 memset (buf, 'A', size);
63 free (buf);
65 /* Try again, this time with write mode enabled before the seek. */
66 fp = open_memstream (&buf, &size);
67 if (fp == NULL)
69 puts ("2nd open_memstream failed");
70 return 1;
73 off = ftello64 (fp);
74 if (off != 0)
76 puts ("2nd initial position wrong");
77 return 1;
80 if (fputs ("bar", fp) == EOF)
82 puts ("2nd fputs failed");
83 return 1;
86 if (fseek (fp, 32768, SEEK_SET) != 0)
88 puts ("2nd fseek failed");
89 return 1;
92 if (fputs ("foo", fp) == EOF)
94 puts ("3rd fputs failed");
95 return 1;
98 if (fclose (fp) == EOF)
100 puts ("2nd fclose failed");
101 return 1;
104 if (size != 32768 + 3)
106 printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
107 return 1;
110 if (memcmp (buf, "bar", 3) != 0)
112 puts ("initial string incorrect in 2nd try");
113 return 1;
116 for (int i = 3; i < 32768; ++i)
117 if (buf[i] != '\0')
119 printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
120 return 1;
123 if (memcmp (buf + 32768, "foo", 3) != 0)
125 puts ("written string incorrect in 2nd try");
126 return 1;
129 return 0;
132 #define TEST_FUNCTION do_test ()
133 #include "../test-skeleton.c"