lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <wchar.h> |
| 3 | |
| 4 | #define PASSED 0 |
| 5 | #define FAILED 3 |
| 6 | |
| 7 | |
| 8 | static int fd; |
| 9 | |
| 10 | static void prepare (void); |
| 11 | #define PREPARE(argc, argv) prepare () |
| 12 | |
| 13 | |
| 14 | #define TEST_FUNCTION do_test () |
| 15 | static int do_test (void); |
| 16 | #include "../test-skeleton.c" |
| 17 | |
| 18 | |
| 19 | static void |
| 20 | prepare (void) |
| 21 | { |
| 22 | fd = create_temp_file ("wrewind.", NULL); |
| 23 | if (fd == -1) |
| 24 | exit (3); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | static int |
| 29 | do_test (void) |
| 30 | { |
| 31 | FILE *fptr; |
| 32 | char arg1; |
| 33 | char arg2; |
| 34 | int ret1, ret2, result, num; |
| 35 | |
| 36 | ret1 = 0; |
| 37 | ret2 = 0; |
| 38 | |
| 39 | fptr = fdopen (fd, "w+"); |
| 40 | if (fptr == NULL) |
| 41 | { |
| 42 | printf ("Unable to open file.\n"); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | if (fwprintf (fptr, L"cderf") <= 0) |
| 47 | { |
| 48 | printf ("Unable to write to file with fwprintf().\n"); |
| 49 | fclose (fptr); |
| 50 | return 2; |
| 51 | } |
| 52 | |
| 53 | rewind (fptr); |
| 54 | ret1 = fwscanf (fptr, L"%c%c", &arg1, &arg2); |
| 55 | if (ret1 != 2) |
| 56 | { |
| 57 | printf ("first fwscanf returned %d, expected 2\n", ret1); |
| 58 | return 3; |
| 59 | } |
| 60 | |
| 61 | rewind (fptr); |
| 62 | ret2 = fwscanf (fptr, L"%c%n%c", &arg1, &num, &arg2); |
| 63 | if (ret2 != 2) |
| 64 | { |
| 65 | printf ("second fwscanf returned %d, expected 2\n", ret2); |
| 66 | return 4; |
| 67 | } |
| 68 | |
| 69 | if (arg2 != 'd') |
| 70 | { |
| 71 | result = FAILED; |
| 72 | printf ("rewind after first fwscanf failed\n"); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | printf ("Passed\n"); |
| 77 | result = PASSED; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | fclose (fptr); |
| 82 | return result; |
| 83 | } |