Back to index

glibc  2.9
bug5.c
Go to the documentation of this file.
00001 /* If stdio is working correctly, after this is run infile and outfile
00002    will have the same contents.  If the bug (found in GNU C library 0.3)
00003    exhibits itself, outfile will be missing the 2nd through 1023rd
00004    characters.  */
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <unistd.h>
00009 
00010 static char buf[8192];
00011 
00012 int
00013 main (void)
00014 {
00015   FILE *in;
00016   FILE *out;
00017   static char inname[] = "/tmp/bug5.in";
00018   static char outname[] = "/tmp/bug5.out";
00019   char *printbuf;
00020   size_t i;
00021   int result;
00022 
00023   /* Create a test file.  */
00024   in = fopen (inname, "w+");
00025   if (in == NULL)
00026     {
00027       perror (inname);
00028       return 1;
00029     }
00030   for (i = 0; i < 1000; ++i)
00031     fprintf (in, "%Zu\n", i);
00032 
00033   out = fopen (outname, "w");
00034   if (out == NULL)
00035     {
00036       perror (outname);
00037       return 1;
00038     }
00039   if (fseek (in, 0L, SEEK_SET) != 0)
00040     abort ();
00041   putc (getc (in), out);
00042   i = fread (buf, 1, sizeof (buf), in);
00043   if (i == 0)
00044     {
00045       perror ("fread");
00046       return 1;
00047     }
00048   if (fwrite (buf, 1, i, out) != i)
00049     {
00050       perror ("fwrite");
00051       return 1;
00052     }
00053   fclose (in);
00054   fclose (out);
00055 
00056   puts ("There should be no further output from this test.");
00057   fflush (stdout);
00058 
00059   /* We must remove this entry to assure the `cmp' binary does not use
00060      the perhaps incompatible new shared libraries.  */
00061   unsetenv ("LD_LIBRARY_PATH");
00062 
00063   asprintf (&printbuf, "cmp %s %s", inname, outname);
00064   result = system (printbuf);
00065   remove (inname);
00066   remove (outname);
00067 
00068   exit ((result != 0));
00069 }