bug-wsetpos.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Test program for fsetpos on a wide character stream. */
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <wchar.h>
  5. static void do_prepare (void);
  6. #define PREPARE(argc, argv) do_prepare ()
  7. static int do_test (void);
  8. #define TEST_FUNCTION do_test ()
  9. #include <test-skeleton.c>
  10. static const char pattern[] = "12345";
  11. static char *temp_file;
  12. static void
  13. do_prepare (void)
  14. {
  15. int fd = create_temp_file ("bug-wsetpos.", &temp_file);
  16. if (fd == -1)
  17. {
  18. printf ("cannot create temporary file: %m\n");
  19. exit (1);
  20. }
  21. write (fd, pattern, sizeof (pattern));
  22. close (fd);
  23. }
  24. static int
  25. do_test (void)
  26. {
  27. FILE *fp = fopen (temp_file, "r");
  28. fpos_t pos;
  29. wchar_t c;
  30. if (fp == NULL)
  31. {
  32. printf ("fdopen: %m\n");
  33. return 1;
  34. }
  35. c = fgetwc (fp); assert (c == L'1');
  36. c = fgetwc (fp); assert (c == L'2');
  37. if (fgetpos (fp, &pos) == EOF)
  38. {
  39. printf ("fgetpos: %m\n");
  40. return 1;
  41. }
  42. rewind (fp);
  43. if (ferror (fp))
  44. {
  45. printf ("rewind: %m\n");
  46. return 1;
  47. }
  48. c = fgetwc (fp); assert (c == L'1');
  49. if (fsetpos (fp, &pos) == EOF)
  50. {
  51. printf ("fsetpos: %m\n");
  52. return 1;
  53. }
  54. c = fgetwc (fp);
  55. if (c != L'3')
  56. {
  57. puts ("fsetpos failed");
  58. return 1;
  59. }
  60. puts ("Test succeeded.");
  61. return 0;
  62. }