Back to index

glibc  2.9
bug7.c
Go to the documentation of this file.
00001 /* Regression test for fseek and freopen bugs.  */
00002 
00003 #include <stdio.h>
00004 
00005 int
00006 main (int argc, char *argv[])
00007 {
00008   int lose = 0;
00009   char filename[L_tmpnam];
00010   FILE *fp;
00011 
00012   if (tmpnam (filename) == NULL)
00013     {
00014       printf ("tmpnam failed\n");
00015       lose = 1;
00016     }
00017   else
00018     {
00019       fp = fopen (filename, "w+");
00020       fprintf (fp, "Hello world!\n");
00021       fflush (fp);
00022       fseek (fp, 5L, SEEK_SET);
00023       if (fseek (fp, -1L, SEEK_CUR) < 0)
00024        {
00025          printf ("seek failed\n");
00026          lose = 1;
00027        }
00028       fclose (fp);
00029       remove (filename);
00030     }
00031 
00032   {
00033     FILE *file1;
00034     FILE *file2;
00035     char filename1[L_tmpnam];
00036     char filename2[L_tmpnam];
00037     int ch;
00038 
00039     if (tmpnam (filename1) == NULL || tmpnam (filename2) == NULL)
00040       {
00041        printf ("tmpnam failed\n");
00042        lose = 1;
00043       }
00044     else
00045       {
00046 
00047        file1 = fopen (filename1, "w");
00048        fclose (file1);
00049 
00050        file2 = fopen (filename2, "w");
00051        fputc ('x', file2);
00052        fclose (file2);
00053 
00054        file1 = fopen (filename1, "r");
00055        file2 = freopen (filename2, "r", file1);
00056        if ((ch = fgetc (file2)) != 'x')
00057          {
00058            printf ("wrong character in reopened file, value = %d\n", ch);
00059            lose = 1;
00060          }
00061        fclose (file2);
00062        remove (filename1);
00063        remove (filename2);
00064       }
00065   }
00066 
00067   puts (lose ? "Test FAILED!" : "Test succeeded.");
00068   return lose;
00069 }