bug-wfflush.c 1.2 KB

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