Blame libio/tst-freopen.c

Packit 6c4009
/* Test freopen with mmap stdio.
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
static int fd;
Packit 6c4009
static char *name;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  fd = create_temp_file ("tst-freopen.", &name);
Packit 6c4009
  TEST_VERIFY_EXIT (fd != -1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
/* Basic tests for freopen.  */
Packit 6c4009
static void
Packit 6c4009
do_test_basic (void)
Packit 6c4009
{
Packit 6c4009
  const char * const test = "Let's test freopen.\n";
Packit 6c4009
  char temp[strlen (test) + 1];
Packit 6c4009
Packit 6c4009
  FILE *f = fdopen (fd, "w");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    FAIL_EXIT1 ("fdopen: %m");
Packit 6c4009
Packit 6c4009
  fputs (test, f);
Packit 6c4009
  fclose (f);
Packit 6c4009
Packit 6c4009
  f = fopen (name, "r");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    FAIL_EXIT1 ("fopen: %m");
Packit 6c4009
Packit 6c4009
  if (fread (temp, 1, strlen (test), f) != strlen (test))
Packit 6c4009
    FAIL_EXIT1 ("fread: %m");
Packit 6c4009
  temp [strlen (test)] = '\0';
Packit 6c4009
Packit 6c4009
  if (strcmp (test, temp))
Packit 6c4009
    FAIL_EXIT1 ("read different string than was written: (%s, %s)",
Packit 6c4009
	        test, temp);
Packit 6c4009
Packit 6c4009
  f = freopen (name, "r+", f);
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    FAIL_EXIT1 ("freopen: %m");
Packit 6c4009
Packit 6c4009
  if (fseek (f, 0, SEEK_SET) != 0)
Packit 6c4009
    FAIL_EXIT1 ("fseek: %m");
Packit 6c4009
Packit 6c4009
  if (fread (temp, 1, strlen (test), f) != strlen (test))
Packit 6c4009
    FAIL_EXIT1 ("fread: %m");
Packit 6c4009
  temp [strlen (test)] = '\0';
Packit 6c4009
Packit 6c4009
  if (strcmp (test, temp))
Packit 6c4009
    FAIL_EXIT1 ("read different string than was written: (%s, %s)",
Packit 6c4009
	        test, temp);
Packit 6c4009
Packit 6c4009
  fclose (f);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Test for BZ#21398, where it tries to freopen stdio after the close
Packit 6c4009
   of its file descriptor.  */
Packit 6c4009
static void
Packit 6c4009
do_test_bz21398 (void)
Packit 6c4009
{
Packit 6c4009
  (void) close (STDIN_FILENO);
Packit 6c4009
Packit 6c4009
  FILE *f = freopen (name, "r", stdin);
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    FAIL_EXIT1 ("freopen: %m");
Packit 6c4009
Packit 6c4009
  TEST_VERIFY_EXIT (ferror (f) == 0);
Packit 6c4009
Packit 6c4009
  char buf[128];
Packit 6c4009
  char *ret = fgets (buf, sizeof (buf), stdin);
Packit 6c4009
  TEST_VERIFY_EXIT (ret != NULL);
Packit 6c4009
  TEST_VERIFY_EXIT (ferror (f) == 0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  do_test_basic ();
Packit 6c4009
  do_test_bz21398 ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>