tst-fgetc-after-eof.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Bug 1190: EOF conditions are supposed to be sticky.
  2. Copyright (C) 2018 Free Software Foundation.
  3. Copying and distribution of this file, with or without modification,
  4. are permitted in any medium without royalty provided the copyright
  5. notice and this notice are preserved. This file is offered as-is,
  6. without any warranty. */
  7. /* ISO C1999 specification of fgetc:
  8. #include <stdio.h>
  9. int fgetc (FILE *stream);
  10. Description
  11. If the end-of-file indicator for the input stream pointed to by
  12. stream is not set and a next character is present, the fgetc
  13. function obtains that character as an unsigned char converted to
  14. an int and advances the associated file position indicator for
  15. the stream (if defined).
  16. Returns
  17. If the end-of-file indicator for the stream is set, or if the
  18. stream is at end-of-file, the end-of-file indicator for the
  19. stream is set and the fgetc function returns EOF. Otherwise, the
  20. fgetc function returns the next character from the input stream
  21. pointed to by stream. If a read error occurs, the error indicator
  22. for the stream is set and the fgetc function returns EOF.
  23. The requirement to return EOF "if the end-of-file indicator for the
  24. stream is set" was new in C99; the language in the 1989 edition of
  25. the standard was ambiguous. Historically, BSD-derived Unix always
  26. had the C99 behavior, whereas in System V fgetc would attempt to
  27. call read() again before returning EOF again. Prior to version 2.28,
  28. glibc followed the System V behavior even though this does not
  29. comply with C99.
  30. See
  31. <https://sourceware.org/bugzilla/show_bug.cgi?id=1190>,
  32. <https://sourceware.org/bugzilla/show_bug.cgi?id=19476>,
  33. and the thread at
  34. <https://sourceware.org/ml/libc-alpha/2012-09/msg00343.html>
  35. for more detail. */
  36. #include <support/tty.h>
  37. #include <support/check.h>
  38. #include <fcntl.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #define XWRITE(fd, s, msg) do { \
  44. if (write (fd, s, sizeof s - 1) != sizeof s - 1) \
  45. { \
  46. perror ("write " msg); \
  47. return 1; \
  48. } \
  49. } while (0)
  50. int
  51. do_test (void)
  52. {
  53. /* The easiest way to set up the conditions under which you can
  54. notice whether the end-of-file indicator is sticky, is with a
  55. pseudo-tty. This is also the case which applications are most
  56. likely to care about. And it avoids any question of whether and
  57. how it is legitimate to access the same physical file with two
  58. independent FILE objects. */
  59. int outer_fd, inner_fd;
  60. FILE *fp;
  61. support_openpty (&outer_fd, &inner_fd, 0, 0, 0);
  62. fp = fdopen (inner_fd, "r+");
  63. if (!fp)
  64. {
  65. perror ("fdopen");
  66. return 1;
  67. }
  68. XWRITE (outer_fd, "abc\n\004", "first line + EOF");
  69. TEST_COMPARE (fgetc (fp), 'a');
  70. TEST_COMPARE (fgetc (fp), 'b');
  71. TEST_COMPARE (fgetc (fp), 'c');
  72. TEST_COMPARE (fgetc (fp), '\n');
  73. TEST_COMPARE (fgetc (fp), EOF);
  74. TEST_VERIFY_EXIT (feof (fp));
  75. TEST_VERIFY_EXIT (!ferror (fp));
  76. XWRITE (outer_fd, "d\n", "second line");
  77. /* At this point, there is a new full line of input waiting in the
  78. kernelside input buffer, but we should still observe EOF from
  79. stdio, because the end-of-file indicator has not been cleared. */
  80. TEST_COMPARE (fgetc (fp), EOF);
  81. /* Clearing EOF should reveal the next line of input. */
  82. clearerr (fp);
  83. TEST_COMPARE (fgetc (fp), 'd');
  84. TEST_COMPARE (fgetc (fp), '\n');
  85. fclose (fp);
  86. close (outer_fd);
  87. return 0;
  88. }
  89. #include <support/test-driver.c>