aboutsummaryrefslogtreecommitdiff
blob: f69fbe6f21cd91ca311de5000b199be642646b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>


int
main (void)
{
  FILE *fp;
  const char teststring[] = "hello world";
  char buf[3072];
  int result = 0;
  char readbuf[256];

  /* Open a file.  */
  fp = tmpfile ();

  /* Set a buffer.  */
  if (setvbuf (fp, buf, _IOFBF, sizeof buf) == EOF)
    {
      printf ("setvbuf failed: %m\n");
      exit (1);
    }

  /* Get the buffer size.  */
  if (__fbufsize (fp) != sizeof buf)
    {
      printf ("__fbusize() reported a buffer size of %Zd bytes;"
	      " we installed a buffer with %Zd bytes\n",
	      __fbufsize (fp), sizeof buf);
      result = 1;
    }

  /* Write something and read it back.  */
  if (fputs (teststring, fp) == EOF)
    {
      printf ("writing to new stream failed: %m\n");
      exit (1);
    }
  rewind (fp);
  if (fgets (readbuf, sizeof readbuf, fp) == NULL)
    {
      printf ("reading from new stream failed: %m\n");
      exit (1);
    }
  if (strcmp (readbuf, teststring) != 0)
    {
      puts ("not the correct string read");
      exit (1);
    }

  /* The file must be opened for reading and writing.  */
  if (__freading (fp) == 0)
    {
      puts ("__freading() reported stream is not last read from");
      result = 1;
    }
  if (__fwriting (fp) != 0)
    {
      puts ("__fwriting() reported stream is write-only or last written to");
      result = 1;
    }
  rewind (fp);
  if (fputs (teststring, fp) == EOF)
    {
      printf ("writing(2) to new stream failed: %m\n");
      exit (1);
    }
  if (__fwriting (fp) == 0)
    {
      puts ("__fwriting() doe snot reported stream is last written to");
      result = 1;
    }
  if (__freading (fp) != 0)
    {
      puts ("__freading() reported stream is last read from");
      result = 1;
    }

  if (__freadable (fp) == 0)
    {
      puts ("__freading() reported stream is last readable");
      result = 1;
    }
  if (__fwritable (fp) == 0)
    {
      puts ("__freading() reported stream is last writable");
      result = 1;
    }

  /* The string we wrote above should still be in the buffer.  */
  if (__fpending (fp) != strlen (teststring))
    {
      printf ("__fpending() returned %Zd; expected %Zd\n",
	      __fpending (fp), strlen (teststring));
      result = 1;
    }
  /* Discard all the output.  */
  __fpurge (fp);
  /* And check again.  */
  if (__fpending (fp) != 0)
    {
      printf ("__fpending() returned %Zd; expected 0\n",
	      __fpending (fp));
      result = 1;
    }


  /* Find out whether buffer is line buffered.  */
  if (__flbf (fp) != 0)
    {
      puts ("__flbf() reports line buffered but it is fully buffered");
      result = 1;
    }

  if (setvbuf (fp, buf, _IOLBF, sizeof buf) == EOF)
    {
      printf ("setvbuf(2) failed: %m\n");
      exit (1);
    }
  if (__flbf (fp) == 0)
    {
      puts ("__flbf() reports file is not line buffered");
      result = 1;
    }

  if (setvbuf (fp, NULL, _IONBF, 0) == EOF)
    {
      printf ("setvbuf(3) failed: %m\n");
      exit (1);
    }
  if (__flbf (fp) != 0)
    {
      puts ("__flbf() reports line buffered but it is not buffered");
      result = 1;
    }

  fclose (fp);

  return result;
}