tst-ungetwc2.c 1.7 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;
  14. char fname[] = "/tmp/tst-ungetwc2.out.XXXXXX";
  15. int fd;
  16. long int pos;
  17. int result = 0;
  18. puts ("This program runs on de_DE.UTF-8 locale.");
  19. if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
  20. {
  21. fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale\n");
  22. exit (EXIT_FAILURE);
  23. }
  24. /* Write some characters to `testfile'. */
  25. fd = mkstemp (fname);
  26. if (fd == -1)
  27. {
  28. printf ("cannot open temp file: %m\n");
  29. exit (EXIT_FAILURE);
  30. }
  31. if ((fp = fdopen (fd, "w")) == NULL)
  32. {
  33. fprintf (stderr, "Cannot open 'testfile'.\n");
  34. exit (EXIT_FAILURE);
  35. }
  36. fputs (str, fp);
  37. fclose (fp);
  38. /* Open `testfile'. */
  39. if ((fp = fopen (fname, "r")) == NULL)
  40. {
  41. fprintf (stderr, "Cannot open 'testfile'.");
  42. exit (EXIT_FAILURE);
  43. }
  44. /* Get a character. */
  45. wc = getwc (fp);
  46. pos = ftell (fp);
  47. printf ("After get a character: %ld\n", pos);
  48. if (pos != 1)
  49. result = 1;
  50. /* Unget a character. */
  51. ret = ungetwc (wc, fp);
  52. if (ret == WEOF)
  53. {
  54. fprintf (stderr, "ungetwc() returns NULL.");
  55. exit (EXIT_FAILURE);
  56. }
  57. pos = ftell (fp);
  58. printf ("After unget a character: %ld\n", pos);
  59. if (pos != 0)
  60. result = 1;
  61. /* Reget a character. */
  62. wc = getwc (fp);
  63. pos = ftell (fp);
  64. printf ("After reget a character: %ld\n", pos);
  65. if (pos != 1)
  66. result = 1;
  67. fclose (fp);
  68. unlink (fname);
  69. return result;
  70. }
  71. #define TEST_FUNCTION do_test ()
  72. #include "../test-skeleton.c"