| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h> | 
 | 2 | #include <string.h> | 
 | 3 |  | 
 | 4 |  | 
 | 5 | static int | 
 | 6 | do_test (void) | 
 | 7 | { | 
 | 8 |   size_t size; | 
 | 9 |   char *buf; | 
 | 10 |   FILE *fp = open_memstream (&buf, &size); | 
 | 11 |   if (fp == NULL) | 
 | 12 |     { | 
 | 13 |       puts ("open_memstream failed"); | 
 | 14 |       return 1; | 
 | 15 |     } | 
 | 16 |  | 
 | 17 |   off64_t off = ftello64 (fp); | 
 | 18 |   if (off != 0) | 
 | 19 |     { | 
 | 20 |       puts ("initial position wrong"); | 
 | 21 |       return 1; | 
 | 22 |     } | 
 | 23 |  | 
 | 24 |   if (fseek (fp, 32768, SEEK_SET) != 0) | 
 | 25 |     { | 
 | 26 |       puts ("fseek failed"); | 
 | 27 |       return 1; | 
 | 28 |     } | 
 | 29 |  | 
 | 30 |   if (fputs ("foo", fp) == EOF) | 
 | 31 |     { | 
 | 32 |       puts ("fputs failed"); | 
 | 33 |       return 1; | 
 | 34 |     } | 
 | 35 |  | 
 | 36 |   if (fclose (fp) == EOF) | 
 | 37 |     { | 
 | 38 |       puts ("fclose failed"); | 
 | 39 |       return 1; | 
 | 40 |     } | 
 | 41 |  | 
 | 42 |   if (size != 32768 + 3) | 
 | 43 |     { | 
 | 44 |       printf ("expected size %d, got %zu\n", 32768 + 3, size); | 
 | 45 |       return 1; | 
 | 46 |     } | 
 | 47 |  | 
 | 48 |   for (int i = 0; i < 32768; ++i) | 
 | 49 |     if (buf[i] != '\0') | 
 | 50 |       { | 
 | 51 | 	printf ("byte at offset %d is %#hhx\n", i, buf[i]); | 
 | 52 | 	return 1; | 
 | 53 |       } | 
 | 54 |  | 
 | 55 |   if (memcmp (buf + 32768, "foo", 3) != 0) | 
 | 56 |     { | 
 | 57 |       puts ("written string incorrect"); | 
 | 58 |       return 1; | 
 | 59 |     } | 
 | 60 |  | 
 | 61 |   /* Mark the buffer.  */ | 
 | 62 |   memset (buf, 'A', size); | 
 | 63 |   free (buf); | 
 | 64 |  | 
 | 65 |   /* Try again, this time with write mode enabled before the seek.  */ | 
 | 66 |   fp = open_memstream (&buf, &size); | 
 | 67 |   if (fp == NULL) | 
 | 68 |     { | 
 | 69 |       puts ("2nd open_memstream failed"); | 
 | 70 |       return 1; | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |   off = ftello64 (fp); | 
 | 74 |   if (off != 0) | 
 | 75 |     { | 
 | 76 |       puts ("2nd initial position wrong"); | 
 | 77 |       return 1; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |   if (fputs ("bar", fp) == EOF) | 
 | 81 |     { | 
 | 82 |       puts ("2nd fputs failed"); | 
 | 83 |       return 1; | 
 | 84 |     } | 
 | 85 |  | 
 | 86 |   if (fseek (fp, 32768, SEEK_SET) != 0) | 
 | 87 |     { | 
 | 88 |       puts ("2nd fseek failed"); | 
 | 89 |       return 1; | 
 | 90 |     } | 
 | 91 |  | 
 | 92 |   if (fputs ("foo", fp) == EOF) | 
 | 93 |     { | 
 | 94 |       puts ("3rd fputs failed"); | 
 | 95 |       return 1; | 
 | 96 |     } | 
 | 97 |  | 
 | 98 |   if (fclose (fp) == EOF) | 
 | 99 |     { | 
 | 100 |       puts ("2nd fclose failed"); | 
 | 101 |       return 1; | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |   if (size != 32768 + 3) | 
 | 105 |     { | 
 | 106 |       printf ("2nd expected size %d, got %zu\n", 32768 + 3, size); | 
 | 107 |       return 1; | 
 | 108 |     } | 
 | 109 |  | 
 | 110 |   if (memcmp (buf, "bar", 3) != 0) | 
 | 111 |     { | 
 | 112 |       puts ("initial string incorrect in 2nd try"); | 
 | 113 |       return 1; | 
 | 114 |     } | 
 | 115 |  | 
 | 116 |   for (int i = 3; i < 32768; ++i) | 
 | 117 |     if (buf[i] != '\0') | 
 | 118 |       { | 
 | 119 | 	printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]); | 
 | 120 | 	return 1; | 
 | 121 |       } | 
 | 122 |  | 
 | 123 |   if (memcmp (buf + 32768, "foo", 3) != 0) | 
 | 124 |     { | 
 | 125 |       puts ("written string incorrect in 2nd try"); | 
 | 126 |       return 1; | 
 | 127 |     } | 
 | 128 |  | 
 | 129 |   return 0; | 
 | 130 | } | 
 | 131 |  | 
 | 132 | #define TEST_FUNCTION do_test () | 
 | 133 | #include "../test-skeleton.c" |