1 | /* Derived from the test case in |
2 | https://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 (base: "tst-fwrite." , NULL); |
25 | if (fd == -1) |
26 | { |
27 | puts (s: "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 (s: "cannot create stream" ); |
39 | return 1; |
40 | } |
41 | puts (s: "Opened temp file" ); |
42 | |
43 | if (fwrite ("a" , 1, 1, f) != 1) |
44 | { |
45 | puts (s: "1st fwrite failed" ); |
46 | return 1; |
47 | } |
48 | puts (s: "Wrote a byte" ); |
49 | fflush (f); |
50 | |
51 | char buffer[10000]; |
52 | size_t i = fread (ptr: buffer, size: 1, n: sizeof (buffer), stream: f); |
53 | printf (format: "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 (format: "fwrite in loop round %zu failed\n" , i); |
60 | return 1; |
61 | } |
62 | |
63 | if ((i + 1) % 1000 == 0) |
64 | printf (format: "wrote %zu bytes ...\n" , i + 1); |
65 | } |
66 | |
67 | printf (format: "Wrote %i bytes [done]\n" , OUT_SIZE); |
68 | |
69 | return 0; |
70 | } |
71 | |