Back to index

glibc  2.9
tst-ext.c
Go to the documentation of this file.
00001 #include <stdio_ext.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 
00006 int
00007 main (void)
00008 {
00009   FILE *fp;
00010   const char teststring[] = "hello world";
00011   char buf[3072];
00012   int result = 0;
00013   char readbuf[256];
00014 
00015   /* Open a file.  */
00016   fp = tmpfile ();
00017 
00018   /* Set a buffer.  */
00019   if (setvbuf (fp, buf, _IOFBF, sizeof buf) == EOF)
00020     {
00021       printf ("setvbuf failed: %m\n");
00022       exit (1);
00023     }
00024 
00025   /* Get the buffer size.  */
00026   if (__fbufsize (fp) != sizeof buf)
00027     {
00028       printf ("__fbusize() reported a buffer size of %Zd bytes;"
00029              " we installed a buffer with %Zd bytes\n",
00030              __fbufsize (fp), sizeof buf);
00031       result = 1;
00032     }
00033 
00034   /* Write something and read it back.  */
00035   if (fputs (teststring, fp) == EOF)
00036     {
00037       printf ("writing to new stream failed: %m\n");
00038       exit (1);
00039     }
00040   rewind (fp);
00041   if (fgets (readbuf, sizeof readbuf, fp) == NULL)
00042     {
00043       printf ("reading from new stream failed: %m\n");
00044       exit (1);
00045     }
00046   if (strcmp (readbuf, teststring) != 0)
00047     {
00048       puts ("not the correct string read");
00049       exit (1);
00050     }
00051 
00052   /* The file must be opened for reading and writing.  */
00053   if (__freading (fp) == 0)
00054     {
00055       puts ("__freading() reported stream is not last read from");
00056       result = 1;
00057     }
00058   if (__fwriting (fp) != 0)
00059     {
00060       puts ("__fwriting() reported stream is write-only or last written to");
00061       result = 1;
00062     }
00063   rewind (fp);
00064   if (fputs (teststring, fp) == EOF)
00065     {
00066       printf ("writing(2) to new stream failed: %m\n");
00067       exit (1);
00068     }
00069   if (__fwriting (fp) == 0)
00070     {
00071       puts ("__fwriting() doe snot reported stream is last written to");
00072       result = 1;
00073     }
00074   if (__freading (fp) != 0)
00075     {
00076       puts ("__freading() reported stream is last read from");
00077       result = 1;
00078     }
00079 
00080   if (__freadable (fp) == 0)
00081     {
00082       puts ("__freading() reported stream is last readable");
00083       result = 1;
00084     }
00085   if (__fwritable (fp) == 0)
00086     {
00087       puts ("__freading() reported stream is last writable");
00088       result = 1;
00089     }
00090 
00091   /* The string we wrote above should still be in the buffer.  */
00092   if (__fpending (fp) != strlen (teststring))
00093     {
00094       printf ("__fpending() returned %Zd; expected %Zd\n",
00095              __fpending (fp), strlen (teststring));
00096       result = 1;
00097     }
00098   /* Discard all the output.  */
00099   __fpurge (fp);
00100   /* And check again.  */
00101   if (__fpending (fp) != 0)
00102     {
00103       printf ("__fpending() returned %Zd; expected 0\n",
00104              __fpending (fp));
00105       result = 1;
00106     }
00107 
00108 
00109   /* Find out whether buffer is line buffered.  */
00110   if (__flbf (fp) != 0)
00111     {
00112       puts ("__flbf() reports line buffered but it is fully buffered");
00113       result = 1;
00114     }
00115 
00116   if (setvbuf (fp, buf, _IOLBF, sizeof buf) == EOF)
00117     {
00118       printf ("setvbuf(2) failed: %m\n");
00119       exit (1);
00120     }
00121   if (__flbf (fp) == 0)
00122     {
00123       puts ("__flbf() reports file is not line buffered");
00124       result = 1;
00125     }
00126 
00127   if (setvbuf (fp, NULL, _IONBF, 0) == EOF)
00128     {
00129       printf ("setvbuf(3) failed: %m\n");
00130       exit (1);
00131     }
00132   if (__flbf (fp) != 0)
00133     {
00134       puts ("__flbf() reports line buffered but it is not buffered");
00135       result = 1;
00136     }
00137 
00138   fclose (fp);
00139 
00140   return result;
00141 }