tst-ungetwc1.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Taken from the Li18nux base test suite. */
  2. #define _XOPEN_SOURCE 500
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <wchar.h>
  8. static int
  9. do_test (void)
  10. {
  11. FILE *fp;
  12. const char *str = "abcdef";
  13. wint_t ret, wc, ungetone = 0x00E4; /* 0x00E4 means `a umlaut'. */
  14. char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
  15. int fd;
  16. int result = 0;
  17. puts ("This program runs on de_DE.UTF-8 locale.");
  18. if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
  19. {
  20. fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
  21. exit (EXIT_FAILURE);
  22. }
  23. fd = mkstemp (fname);
  24. if (fd == -1)
  25. {
  26. printf ("cannot open temp file: %m\n");
  27. exit (EXIT_FAILURE);
  28. }
  29. /* Write some characters to `testfile'. */
  30. if ((fp = fdopen (fd, "w")) == NULL)
  31. {
  32. fprintf (stderr, "Cannot open 'testfile'.");
  33. exit (EXIT_FAILURE);
  34. }
  35. fputs (str, fp);
  36. fclose (fp);
  37. /* Open `testfile'. */
  38. if ((fp = fopen (fname, "r")) == NULL)
  39. {
  40. fprintf (stderr, "Cannot open 'testfile'.");
  41. exit (EXIT_FAILURE);
  42. }
  43. /* Unget a character. */
  44. ret = ungetwc (ungetone, fp);
  45. printf ("Unget a character (0x%04x)\n", (unsigned int) ungetone);
  46. fflush (stdout);
  47. if (ret == WEOF)
  48. {
  49. puts ("ungetwc() returns NULL.");
  50. exit (EXIT_SUCCESS);
  51. }
  52. /* Reget a character. */
  53. wc = getwc (fp);
  54. printf ("Reget a character (0x%04x)\n", (unsigned int) wc);
  55. fflush (stdout);
  56. if (wc == ungetone)
  57. {
  58. puts ("The ungotten character is equal to the regotten character.");
  59. fflush (stdout);
  60. }
  61. else
  62. {
  63. puts ("The ungotten character is not equal to the regotten character.");
  64. printf ("ungotten one: %04x, regetone: %04x", ungetone, wc);
  65. fflush (stdout);
  66. result = 1;
  67. }
  68. fclose (fp);
  69. unlink (fname);
  70. return result;
  71. }
  72. #define TEST_FUNCTION do_test ()
  73. #include "../test-skeleton.c"