bug-ungetwc2.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #define _XOPEN_SOURCE 500
  2. #include <errno.h>
  3. #include <error.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <locale.h>
  7. #include <wchar.h>
  8. const char test_locale[] = "de_DE.UTF-8";
  9. const wchar_t write_wchars[] = {L'A', 0x00C4, L'B', L'\0'};
  10. /* `0x00C4' is A with diaeresis. */
  11. size_t last_pos = 2; /* Which character is last one to read. */
  12. wint_t unget_wchar = L'C'; /* Ungotten ide characters. */
  13. char *fname;
  14. static int do_test (void);
  15. #define TEST_FUNCTION do_test ()
  16. #include "../test-skeleton.c"
  17. static int
  18. do_test (void)
  19. {
  20. size_t i;
  21. wint_t wc;
  22. FILE *fp;
  23. int fd;
  24. fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc2.XXXXXX");
  25. if (fname == NULL)
  26. {
  27. puts ("no memory");
  28. return 1;
  29. }
  30. strcpy (stpcpy (fname, test_dir), "/bug-ungetwc2.XXXXXX");
  31. fd = mkstemp (fname);
  32. if (fd == -1)
  33. {
  34. printf ("cannot open temporary file: %m\n");
  35. return 1;
  36. }
  37. add_temp_file (fname);
  38. printf ("\nNote: This program runs on %s locale.\n\n", test_locale);
  39. if (setlocale (LC_ALL, test_locale) == NULL)
  40. {
  41. fprintf (stderr, "Cannot use `%s' locale.\n", test_locale);
  42. exit (EXIT_FAILURE);
  43. }
  44. /* Output to the file. */
  45. if ((fp = fdopen (fd, "w")) == NULL)
  46. {
  47. setlocale (LC_ALL, "C");
  48. fprintf (stderr, "Cannot make `%s' file.\n", fname);
  49. exit (EXIT_FAILURE);
  50. }
  51. fprintf (fp, "%ls", write_wchars);
  52. fclose (fp);
  53. /* Read from the file. */
  54. fp = fopen (fname, "r");
  55. if (fp == NULL)
  56. {
  57. setlocale (LC_ALL, "C");
  58. error (EXIT_FAILURE, errno, "cannot open %s", fname);
  59. }
  60. printf ("%s is opened.\n", fname);
  61. for (i = 0; i < last_pos; i++)
  62. {
  63. wc = getwc (fp);
  64. printf ("> `%lc' is gotten.\n", wc);
  65. }
  66. /* Unget a wide character. */
  67. ungetwc (unget_wchar, fp);
  68. printf ("< `%lc' is ungotten.\n", unget_wchar);
  69. /* Reget the wide character. */
  70. wc = getwc (fp);
  71. printf ("> `%lc' is regotten.\n", wc);
  72. fflush (stdout);
  73. fclose (fp);
  74. return 0;
  75. }