Blame libio/bug-ungetc4.c

Packit 6c4009
/* Test program for ungetc/fseekpos interaction.
Packit 6c4009
   Copyright (C) 2004-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>, 2004.
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
Packit 6c4009
static void do_prepare (void);
Packit 6c4009
#define PREPARE(argc, argv) do_prepare ()
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static const char pattern[] = "abcdefghijklmnopqrstuvwxyz";
Packit 6c4009
static char *temp_file;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  int fd = create_temp_file ("bug-ungetc.", &temp_file);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create temporary file: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  write (fd, pattern, sizeof (pattern) - 1);
Packit 6c4009
  close (fd);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_POS 5
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_one_test (int mode)
Packit 6c4009
{
Packit 6c4009
  FILE *f = fopen (temp_file, "r");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: could not open temporary file: %m\n", mode);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int i;
Packit 6c4009
  for (i = 0; i < TEST_POS; ++i)
Packit 6c4009
    getc (f);
Packit 6c4009
Packit 6c4009
  fpos_t p;
Packit 6c4009
  if (fgetpos (f, &p) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: fgetpos failed: %m\n", mode);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mode)
Packit 6c4009
    {
Packit 6c4009
      if (fseek (f, 0, SEEK_SET) != 0)
Packit 6c4009
        {
Packit 6c4009
          printf ("%d: fseek failed: %m\n", mode);
Packit 6c4009
          return 1;
Packit 6c4009
        }
Packit 6c4009
Packit 6c4009
      for (i = 0; i < TEST_POS - (mode >= 2); ++i)
Packit 6c4009
        getc (f);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mode != 2 && ungetc ('X', f) != 'X')
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: ungetc failed\n", mode);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mode == 3 && getc (f) != 'X')
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: getc after ungetc did not return X\n", mode);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fsetpos (f, &p) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: fsetpos failed: %m\n", mode);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getc (f) != pattern[TEST_POS])
Packit 6c4009
    {
Packit 6c4009
      printf ("%d: getc did not return %c\n", mode, pattern[TEST_POS]);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (f);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int mode, ret = 0;
Packit 6c4009
  for (mode = 0; mode <= 4; mode++)
Packit 6c4009
    ret |= do_one_test (mode);
Packit 6c4009
  return ret;
Packit 6c4009
}