bug-ungetc4.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Test program for ungetc/fseekpos interaction.
  2. Copyright (C) 2004-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. static void do_prepare (void);
  18. #define PREPARE(argc, argv) do_prepare ()
  19. static int do_test (void);
  20. #define TEST_FUNCTION do_test ()
  21. #include "../test-skeleton.c"
  22. static const char pattern[] = "abcdefghijklmnopqrstuvwxyz";
  23. static char *temp_file;
  24. static void
  25. do_prepare (void)
  26. {
  27. int fd = create_temp_file ("bug-ungetc.", &temp_file);
  28. if (fd == -1)
  29. {
  30. printf ("cannot create temporary file: %m\n");
  31. exit (1);
  32. }
  33. write (fd, pattern, sizeof (pattern) - 1);
  34. close (fd);
  35. }
  36. #define TEST_POS 5
  37. static int
  38. do_one_test (int mode)
  39. {
  40. FILE *f = fopen (temp_file, "r");
  41. if (f == NULL)
  42. {
  43. printf ("%d: could not open temporary file: %m\n", mode);
  44. return 1;
  45. }
  46. int i;
  47. for (i = 0; i < TEST_POS; ++i)
  48. getc (f);
  49. fpos_t p;
  50. if (fgetpos (f, &p) != 0)
  51. {
  52. printf ("%d: fgetpos failed: %m\n", mode);
  53. return 1;
  54. }
  55. if (mode)
  56. {
  57. if (fseek (f, 0, SEEK_SET) != 0)
  58. {
  59. printf ("%d: fseek failed: %m\n", mode);
  60. return 1;
  61. }
  62. for (i = 0; i < TEST_POS - (mode >= 2); ++i)
  63. getc (f);
  64. }
  65. if (mode != 2 && ungetc ('X', f) != 'X')
  66. {
  67. printf ("%d: ungetc failed\n", mode);
  68. return 1;
  69. }
  70. if (mode == 3 && getc (f) != 'X')
  71. {
  72. printf ("%d: getc after ungetc did not return X\n", mode);
  73. return 1;
  74. }
  75. if (fsetpos (f, &p) != 0)
  76. {
  77. printf ("%d: fsetpos failed: %m\n", mode);
  78. return 1;
  79. }
  80. if (getc (f) != pattern[TEST_POS])
  81. {
  82. printf ("%d: getc did not return %c\n", mode, pattern[TEST_POS]);
  83. return 1;
  84. }
  85. fclose (f);
  86. return 0;
  87. }
  88. static int
  89. do_test (void)
  90. {
  91. int mode, ret = 0;
  92. for (mode = 0; mode <= 4; mode++)
  93. ret |= do_one_test (mode);
  94. return ret;
  95. }