Blame libio/tst-fseek.c

Packit 6c4009
/* Verify that fseek/ftell combination works for wide chars.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <locale.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_seek_end (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  long save;
Packit 6c4009
Packit 6c4009
  if (fputws (L"abc\n", fp) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("do_seek_end: fputws: %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  save = ftell (fp);
Packit 6c4009
  rewind (fp);
Packit 6c4009
Packit 6c4009
  if (fseek (fp, 0, SEEK_END) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("do_seek_end: fseek: %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (save != ftell (fp))
Packit 6c4009
    {
Packit 6c4009
      printf ("save = %ld, ftell = %ld\n", save, ftell (fp));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_seek_set (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  long save1, save2;
Packit 6c4009
Packit 6c4009
  if (fputws (L"ゅう\n", fp) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fputws(1): %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  save1 = ftell (fp);
Packit 6c4009
Packit 6c4009
  if (fputws (L"ゅう\n", fp) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fputws(2): %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  save2 = ftell (fp);
Packit 6c4009
Packit 6c4009
  if (fputws (L"ゅう\n", fp) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fputws(3): %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fseek (fp, save1, SEEK_SET) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fseek(1): %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (save1 != ftell (fp))
Packit 6c4009
    {
Packit 6c4009
      printf ("save1 = %ld, ftell = %ld\n", save1, ftell (fp));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fseek (fp, save2, SEEK_SET) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fseek(2): %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (save2 != ftell (fp))
Packit 6c4009
    {
Packit 6c4009
      printf ("save2 = %ld, ftell = %ld\n", save2, ftell (fp));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (setlocale (LC_ALL, "ja_JP.UTF-8") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot set ja_JP.UTF-8 locale.\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Retain messages in English.  */
Packit 6c4009
  if (setlocale (LC_MESSAGES, "en_US.ISO-8859-1") == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot set LC_MESSAGES to en_US.ISO-8859-1 locale.\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int ret = 0;
Packit 6c4009
  char *filename;
Packit 6c4009
  int fd = create_temp_file ("tst-fseek.out", &filename);
Packit 6c4009
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  FILE *fp = fdopen (fd, "w+");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_set: fopen: %s\n", strerror (errno));
Packit 6c4009
      close (fd);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (do_seek_set (fp))
Packit 6c4009
    {
Packit 6c4009
      printf ("SEEK_SET test failed\n");
Packit 6c4009
      ret = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Reopen the file.  */
Packit 6c4009
  fclose (fp);
Packit 6c4009
  fp = fopen (filename, "w+");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("seek_end: fopen: %s\n", strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (do_seek_end (fp))
Packit 6c4009
    {
Packit 6c4009
      printf ("SEEK_END test failed\n");
Packit 6c4009
      ret = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>