tst-ext.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio_ext.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int
  5. main (void)
  6. {
  7. FILE *fp;
  8. const char teststring[] = "hello world";
  9. char buf[3072];
  10. int result = 0;
  11. char readbuf[256];
  12. /* Open a file. */
  13. fp = tmpfile ();
  14. /* Set a buffer. */
  15. if (setvbuf (fp, buf, _IOFBF, sizeof buf) == EOF)
  16. {
  17. printf ("setvbuf failed: %m\n");
  18. exit (1);
  19. }
  20. /* Get the buffer size. */
  21. if (__fbufsize (fp) != sizeof buf)
  22. {
  23. printf ("__fbusize() reported a buffer size of %Zd bytes;"
  24. " we installed a buffer with %Zd bytes\n",
  25. __fbufsize (fp), sizeof buf);
  26. result = 1;
  27. }
  28. /* Write something and read it back. */
  29. if (fputs (teststring, fp) == EOF)
  30. {
  31. printf ("writing to new stream failed: %m\n");
  32. exit (1);
  33. }
  34. rewind (fp);
  35. if (fgets (readbuf, sizeof readbuf, fp) == NULL)
  36. {
  37. printf ("reading from new stream failed: %m\n");
  38. exit (1);
  39. }
  40. if (strcmp (readbuf, teststring) != 0)
  41. {
  42. puts ("not the correct string read");
  43. exit (1);
  44. }
  45. /* The file must be opened for reading and writing. */
  46. if (__freading (fp) == 0)
  47. {
  48. puts ("__freading() reported stream is not last read from");
  49. result = 1;
  50. }
  51. if (__fwriting (fp) != 0)
  52. {
  53. puts ("__fwriting() reported stream is write-only or last written to");
  54. result = 1;
  55. }
  56. rewind (fp);
  57. if (fputs (teststring, fp) == EOF)
  58. {
  59. printf ("writing(2) to new stream failed: %m\n");
  60. exit (1);
  61. }
  62. if (__fwriting (fp) == 0)
  63. {
  64. puts ("__fwriting() doe snot reported stream is last written to");
  65. result = 1;
  66. }
  67. if (__freading (fp) != 0)
  68. {
  69. puts ("__freading() reported stream is last read from");
  70. result = 1;
  71. }
  72. if (__freadable (fp) == 0)
  73. {
  74. puts ("__freading() reported stream is last readable");
  75. result = 1;
  76. }
  77. if (__fwritable (fp) == 0)
  78. {
  79. puts ("__freading() reported stream is last writable");
  80. result = 1;
  81. }
  82. /* The string we wrote above should still be in the buffer. */
  83. if (__fpending (fp) != strlen (teststring))
  84. {
  85. printf ("__fpending() returned %Zd; expected %Zd\n",
  86. __fpending (fp), strlen (teststring));
  87. result = 1;
  88. }
  89. /* Discard all the output. */
  90. __fpurge (fp);
  91. /* And check again. */
  92. if (__fpending (fp) != 0)
  93. {
  94. printf ("__fpending() returned %Zd; expected 0\n",
  95. __fpending (fp));
  96. result = 1;
  97. }
  98. /* Find out whether buffer is line buffered. */
  99. if (__flbf (fp) != 0)
  100. {
  101. puts ("__flbf() reports line buffered but it is fully buffered");
  102. result = 1;
  103. }
  104. if (setvbuf (fp, buf, _IOLBF, sizeof buf) == EOF)
  105. {
  106. printf ("setvbuf(2) failed: %m\n");
  107. exit (1);
  108. }
  109. if (__flbf (fp) == 0)
  110. {
  111. puts ("__flbf() reports file is not line buffered");
  112. result = 1;
  113. }
  114. if (setvbuf (fp, NULL, _IONBF, 0) == EOF)
  115. {
  116. printf ("setvbuf(3) failed: %m\n");
  117. exit (1);
  118. }
  119. if (__flbf (fp) != 0)
  120. {
  121. puts ("__flbf() reports line buffered but it is not buffered");
  122. result = 1;
  123. }
  124. fclose (fp);
  125. return result;
  126. }