tst-fgetws.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Taken from the Li18nux base test suite. */
  2. #define _XOPEN_SOURCE 500
  3. #include <errno.h>
  4. #include <locale.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <wchar.h>
  9. #define WIDE_STR_LEN 32
  10. int
  11. main (int argc, char *argv[])
  12. {
  13. size_t i;
  14. FILE *fp;
  15. wchar_t *ret, wcs[WIDE_STR_LEN];
  16. int result = 0;
  17. const char il_str1[] = {0xe3, 0x81, '\0'};
  18. const char il_str2[] = {'0', '\n', 'A', 'B', 0xe3, 0x81, 'E', '\0'};
  19. char name1[] = "/tmp/tst-fgetws.out.XXXXXX";
  20. char name2[] = "/tmp/tst-fgetws.out.XXXXXX";
  21. int fd;
  22. puts ("This program runs on de_DE.UTF-8 locale.");
  23. if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
  24. {
  25. fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
  26. exit (EXIT_FAILURE);
  27. }
  28. /* Make a file `il_str1'. */
  29. fd = mkstemp (name1);
  30. if (fd == -1)
  31. {
  32. printf ("cannot open temp file: %m\n");
  33. exit (EXIT_FAILURE);
  34. }
  35. if ((fp = fdopen (fd, "w")) == NULL)
  36. {
  37. printf ("Can't open %s.\n", argv[1]);
  38. exit (EXIT_FAILURE);
  39. }
  40. fwrite (il_str1, sizeof (char), sizeof (il_str1), fp);
  41. fclose (fp);
  42. /* Make a file `il_str2'. */
  43. fd = mkstemp (name2);
  44. if (fd == -1)
  45. {
  46. printf ("cannot open temp file: %m\n");
  47. exit (EXIT_FAILURE);
  48. }
  49. if ((fp = fdopen (fd, "w")) == NULL)
  50. {
  51. fprintf (stderr, "Can't open %s.\n", argv[1]);
  52. exit (EXIT_FAILURE);
  53. }
  54. fwrite (il_str2, sizeof (char), sizeof (il_str2), fp);
  55. fclose (fp);
  56. /* Test for il_str1. */
  57. if ((fp = fopen (name1, "r")) == NULL)
  58. {
  59. fprintf (stderr, "Can't open %s.\n", argv[1]);
  60. exit (EXIT_FAILURE);
  61. }
  62. puts ("--");
  63. puts ("Read a byte sequence which is invalid as a wide character string.");
  64. puts (" bytes: 0xe3, 0x81, '\\0'");
  65. errno = 0;
  66. ret = fgetws (wcs, WIDE_STR_LEN, fp);
  67. if (ret == NULL)
  68. {
  69. puts ("Return Value: NULL");
  70. if (errno == EILSEQ)
  71. puts ("errno = EILSEQ");
  72. else
  73. {
  74. printf ("errno = %d\n", errno);
  75. result = 1;
  76. }
  77. }
  78. else
  79. {
  80. printf ("Return Value: %p\n", ret);
  81. for (i = 0; i < wcslen (wcs) + 1; i++)
  82. printf (" wcs[%zd] = %04x", i, (unsigned int)wcs[i]);
  83. printf ("\n");
  84. result = 1;
  85. }
  86. /* Test for il_str2. */
  87. if ((fp = fopen (name2, "r")) == NULL)
  88. {
  89. fprintf (stderr, "Can't open %s.\n", argv[1]);
  90. exit (EXIT_FAILURE);
  91. }
  92. puts ("--");
  93. puts ("Read a byte sequence which is invalid as a wide character string.");
  94. puts (" bytes: '0', '\\n', 'A', 'B', 0xe3, 0x81, 'c', '\\0'");
  95. errno = 0;
  96. ret = fgetws (wcs, WIDE_STR_LEN, fp);
  97. if (ret == NULL)
  98. {
  99. puts ("Return Value: NULL");
  100. if (errno == EILSEQ)
  101. puts ("errno = EILSEQ");
  102. else
  103. printf ("errno = %d\n", errno);
  104. result = 1;
  105. }
  106. else
  107. {
  108. size_t i;
  109. printf ("Return Value: %p\n", ret);
  110. for (i = 0; i < wcslen (wcs) + 1; i++)
  111. printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
  112. printf ("\n");
  113. for (i = 0; il_str2[i] != '\n'; ++i)
  114. if ((wchar_t) il_str2[i] != wcs[i])
  115. {
  116. puts ("read string not correct");
  117. result = 1;
  118. break;
  119. }
  120. if (il_str2[i] == '\n')
  121. {
  122. if (wcs[i] != L'\n')
  123. {
  124. puts ("newline missing");
  125. result = 1;
  126. }
  127. else if (wcs[i + 1] != L'\0')
  128. {
  129. puts ("read string not NUL-terminated");
  130. result = 1;
  131. }
  132. }
  133. }
  134. puts ("\nsecond line");
  135. errno = 0;
  136. ret = fgetws (wcs, WIDE_STR_LEN, fp);
  137. if (ret == NULL)
  138. {
  139. puts ("Return Value: NULL");
  140. if (errno == EILSEQ)
  141. puts ("errno = EILSEQ");
  142. else
  143. {
  144. printf ("errno = %d\n", errno);
  145. result = 1;
  146. }
  147. }
  148. else
  149. {
  150. printf ("Return Value: %p\n", ret);
  151. for (i = 0; i < wcslen (wcs) + 1; i++)
  152. printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
  153. printf ("\n");
  154. }
  155. fclose (fp);
  156. unlink (name1);
  157. unlink (name2);
  158. return result;
  159. }