bug-wfflush.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Test program for bug in wide streams. */
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. static void do_prepare (void);
  5. #define PREPARE(argc, argv) do_prepare ()
  6. static int do_test (void);
  7. #define TEST_FUNCTION do_test ()
  8. #include <test-skeleton.c>
  9. static char *temp_file;
  10. static void
  11. do_prepare (void)
  12. {
  13. int fd = create_temp_file ("bug-ungetc.", &temp_file);
  14. if (fd == -1)
  15. {
  16. printf ("cannot create temporary file: %m\n");
  17. exit (1);
  18. }
  19. write (fd, "1!", 2);
  20. close (fd);
  21. }
  22. static int
  23. do_test (void)
  24. {
  25. FILE *f = fopen (temp_file, "r+");
  26. if (f == NULL)
  27. {
  28. printf ("fopen: %m\n");
  29. return 1;
  30. }
  31. #define L_(s) L##s
  32. //#define fwscanf fscanf
  33. //#define fwprintf fprintf
  34. int i;
  35. if (fwscanf (f, L_("%d!"), &i) != 1)
  36. {
  37. printf ("fwscanf failed\n");
  38. return 1;
  39. }
  40. rewind (f);
  41. if (ferror (f))
  42. {
  43. printf ("rewind: %m\n");
  44. return 1;
  45. }
  46. if (fputws (L_("1!"), f) == EOF)
  47. {
  48. printf ("fputws: %m\n");
  49. return 1;
  50. }
  51. if (fflush (f) != 0)
  52. {
  53. printf ("fflush: %m\n");
  54. return 1;
  55. }
  56. if (fclose (f) != 0)
  57. {
  58. printf ("fclose: %m\n");
  59. return 1;
  60. }
  61. puts ("Test succeeded.");
  62. return 0;
  63. }