bug-ungetwc1.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <wchar.h>
  6. const char write_chars[] = "ABC"; /* Characters on testfile. */
  7. const wint_t unget_wchar = L'A'; /* Ungotten wide character. */
  8. char *fname;
  9. static int do_test (void);
  10. #define TEST_FUNCTION do_test ()
  11. #include "../test-skeleton.c"
  12. static int
  13. do_test (void)
  14. {
  15. wint_t wc;
  16. FILE *fp;
  17. int fd;
  18. fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc1.XXXXXX");
  19. if (fname == NULL)
  20. {
  21. puts ("no memory");
  22. return 1;
  23. }
  24. strcpy (stpcpy (fname, test_dir), "/bug-ungetwc1.XXXXXX");
  25. fd = mkstemp (fname);
  26. if (fd == -1)
  27. {
  28. printf ("cannot open temporary file: %m\n");
  29. return 1;
  30. }
  31. add_temp_file (fname);
  32. setlocale(LC_ALL, "");
  33. /* Output to the file. */
  34. if ((fp = fdopen (fd, "w")) == NULL)
  35. {
  36. fprintf (stderr, "Cannot make `%s' file\n", fname);
  37. exit (EXIT_FAILURE);
  38. }
  39. fprintf (fp, "%s", write_chars);
  40. fclose (fp);
  41. /* Read from the file. */
  42. fp = fopen (fname, "r");
  43. size_t i = 0;
  44. while (!feof (fp))
  45. {
  46. wc = getwc (fp);
  47. if (i >= sizeof (write_chars))
  48. {
  49. printf ("Did not get end-of-file when expected.\n");
  50. return 1;
  51. }
  52. else if (wc != (write_chars[i] ? write_chars[i] : WEOF))
  53. {
  54. printf ("Unexpected %lu from getwc.\n", (unsigned long int) wc);
  55. return 1;
  56. }
  57. i++;
  58. }
  59. printf ("\nThe end-of-file indicator is set.\n");
  60. /* Unget a wide character. */
  61. ungetwc (unget_wchar, fp);
  62. printf ("< `%lc' is ungotten.\n", unget_wchar);
  63. /* Check the end-of-file indicator. */
  64. if (feof (fp))
  65. {
  66. printf ("The end-of-file indicator is still set.\n");
  67. return 1;
  68. }
  69. else
  70. printf ("The end-of-file flag is cleared.\n");
  71. fflush (stdout);
  72. fclose (fp);
  73. return 0;
  74. }