lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Derived from the test case in |
| 2 | http://sourceware.org/bugzilla/show_bug.cgi?id=1078. */ |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #define OUT_SIZE 10000 |
| 8 | |
| 9 | |
| 10 | static int fd; |
| 11 | |
| 12 | static void prepare (void); |
| 13 | #define PREPARE(argc, argv) prepare () |
| 14 | |
| 15 | static int do_test (void); |
| 16 | #define TEST_FUNCTION do_test () |
| 17 | |
| 18 | #include "../test-skeleton.c" |
| 19 | |
| 20 | |
| 21 | static void |
| 22 | prepare (void) |
| 23 | { |
| 24 | fd = create_temp_file ("tst-fwrite.", NULL); |
| 25 | if (fd == -1) |
| 26 | { |
| 27 | puts ("cannot create temporary file"); |
| 28 | exit (1); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | static int |
| 34 | do_test (void) |
| 35 | { |
| 36 | FILE* f = fdopen (fd, "w+"); |
| 37 | if (f == NULL) { |
| 38 | puts ("cannot create stream"); |
| 39 | return 1; |
| 40 | } |
| 41 | puts ("Opened temp file"); |
| 42 | |
| 43 | if (fwrite ("a", 1, 1, f) != 1) |
| 44 | { |
| 45 | puts ("1st fwrite failed"); |
| 46 | return 1; |
| 47 | } |
| 48 | puts ("Wrote a byte"); |
| 49 | fflush (f); |
| 50 | |
| 51 | char buffer[10000]; |
| 52 | size_t i = fread (buffer, 1, sizeof (buffer), f); |
| 53 | printf ("Read %zu bytes\n", i); |
| 54 | |
| 55 | for (i = 0; i < OUT_SIZE; i++) |
| 56 | { |
| 57 | if (fwrite ("n", 1, 1, f) != 1) |
| 58 | { |
| 59 | printf ("fwrite in loop round %zu failed\n", i); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | if ((i + 1) % 1000 == 0) |
| 64 | printf ("wrote %zu bytes ...\n", i + 1); |
| 65 | } |
| 66 | |
| 67 | printf ("Wrote %i bytes [done]\n", OUT_SIZE); |
| 68 | |
| 69 | return 0; |
| 70 | } |