Blame stdio-common/bug7.c

Packit 6c4009
/* Regression test for fseek and freopen bugs.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  int lose = 0;
Packit 6c4009
  char filename[] = "/tmp/bug7.XXXXXX";
Packit 6c4009
  FILE *fp;
Packit 6c4009
Packit 6c4009
  int fd = mkstemp (filename);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("mkstemp failed\n");
Packit 6c4009
      lose = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      close (fd);
Packit 6c4009
      fp = fopen (filename, "w+");
Packit 6c4009
      fprintf (fp, "Hello world!\n");
Packit 6c4009
      fflush (fp);
Packit 6c4009
      fseek (fp, 5L, SEEK_SET);
Packit 6c4009
      if (fseek (fp, -1L, SEEK_CUR) < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("seek failed\n");
Packit 6c4009
	  lose = 1;
Packit 6c4009
	}
Packit 6c4009
      fclose (fp);
Packit 6c4009
      remove (filename);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    FILE *file1;
Packit 6c4009
    FILE *file2;
Packit 6c4009
    char filename1[] = "/tmp/bug7.XXXXXX";
Packit 6c4009
    char filename2[] = "/tmp/bug7.XXXXXX";
Packit 6c4009
    int ch;
Packit 6c4009
Packit 6c4009
    int fd1 = mkstemp (filename1);
Packit 6c4009
    int fd2 = mkstemp (filename2);
Packit 6c4009
    if (fd1 == -1 || fd2 == -1)
Packit 6c4009
      {
Packit 6c4009
	printf ("mkstemp failed\n");
Packit 6c4009
	lose = 1;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
	close (fd1);
Packit 6c4009
	close (fd2);
Packit 6c4009
Packit 6c4009
	file1 = fopen (filename1, "w");
Packit 6c4009
	fclose (file1);
Packit 6c4009
Packit 6c4009
	file2 = fopen (filename2, "w");
Packit 6c4009
	fputc ('x', file2);
Packit 6c4009
	fclose (file2);
Packit 6c4009
Packit 6c4009
	file1 = fopen (filename1, "r");
Packit 6c4009
	file2 = freopen (filename2, "r", file1);
Packit 6c4009
	if ((ch = fgetc (file2)) != 'x')
Packit 6c4009
	  {
Packit 6c4009
	    printf ("wrong character in reopened file, value = %d\n", ch);
Packit 6c4009
	    lose = 1;
Packit 6c4009
	  }
Packit 6c4009
	fclose (file2);
Packit 6c4009
	remove (filename1);
Packit 6c4009
	remove (filename2);
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  puts (lose ? "Test FAILED!" : "Test succeeded.");
Packit 6c4009
  return lose;
Packit 6c4009
}