posix: Add invalid flags test for p{write,read}v2
[glibc.git] / libio / bug-memstream1.c
blobd1ecc79564df76797b305fb5f7936d7e7d6de87f
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
6 static int
7 do_test (void)
9 size_t size;
10 char *buf;
11 FILE *fp = open_memstream (&buf, &size);
12 if (fp == NULL)
14 puts ("open_memstream failed");
15 return 1;
18 off64_t off = ftello64 (fp);
19 if (off != 0)
21 puts ("initial position wrong");
22 return 1;
25 if (fseek (fp, 32768, SEEK_SET) != 0)
27 puts ("fseek failed");
28 return 1;
31 if (fputs ("foo", fp) == EOF)
33 puts ("fputs failed");
34 return 1;
37 if (fclose (fp) == EOF)
39 puts ("fclose failed");
40 return 1;
43 if (size != 32768 + 3)
45 printf ("expected size %d, got %zu\n", 32768 + 3, size);
46 return 1;
49 for (int i = 0; i < 32768; ++i)
50 if (buf[i] != '\0')
52 printf ("byte at offset %d is %#hhx\n", i, buf[i]);
53 return 1;
56 if (memcmp (buf + 32768, "foo", 3) != 0)
58 puts ("written string incorrect");
59 return 1;
62 /* Mark the buffer. */
63 memset (buf, 'A', size);
64 free (buf);
66 /* Try again, this time with write mode enabled before the seek. */
67 fp = open_memstream (&buf, &size);
68 if (fp == NULL)
70 puts ("2nd open_memstream failed");
71 return 1;
74 off = ftello64 (fp);
75 if (off != 0)
77 puts ("2nd initial position wrong");
78 return 1;
81 if (fputs ("bar", fp) == EOF)
83 puts ("2nd fputs failed");
84 return 1;
87 if (fseek (fp, 32768, SEEK_SET) != 0)
89 puts ("2nd fseek failed");
90 return 1;
93 if (fputs ("foo", fp) == EOF)
95 puts ("3rd fputs failed");
96 return 1;
99 if (fclose (fp) == EOF)
101 puts ("2nd fclose failed");
102 return 1;
105 if (size != 32768 + 3)
107 printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
108 return 1;
111 if (memcmp (buf, "bar", 3) != 0)
113 puts ("initial string incorrect in 2nd try");
114 return 1;
117 for (int i = 3; i < 32768; ++i)
118 if (buf[i] != '\0')
120 printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
121 return 1;
124 if (memcmp (buf + 32768, "foo", 3) != 0)
126 puts ("written string incorrect in 2nd try");
127 return 1;
130 return 0;
133 #define TEST_FUNCTION do_test ()
134 #include "../test-skeleton.c"