Blame posix/tst-preadwrite-common.c

Packit 6c4009
/* Common definitions for pread and pwrite.
Packit 6c4009
   Copyright (C) 2016-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 <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.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
Packit 6c4009
/* We might need a bit longer timeout.  */
Packit 6c4009
#define TIMEOUT 20 /* sec */
Packit 6c4009
Packit 6c4009
/* This defines the `main' function and some more.  */
Packit 6c4009
#include <test-skeleton.c>
Packit 6c4009
Packit 6c4009
/* These are for the temporary file we generate.  */
Packit 6c4009
static char *name;
Packit 6c4009
static int fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  fd = create_temp_file ("tst-preadwrite.", &name);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot create temporary file");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static ssize_t
Packit 6c4009
do_test_with_offset (off_t offset)
Packit 6c4009
{
Packit 6c4009
  char buf[1000];
Packit 6c4009
  char res[1000];
Packit 6c4009
  int i;
Packit 6c4009
  ssize_t ret;
Packit 6c4009
Packit 6c4009
  memset (buf, '\0', sizeof (buf));
Packit 6c4009
  memset (res, '\xff', sizeof (res));
Packit 6c4009
Packit 6c4009
  if (write (fd, buf, sizeof (buf)) != sizeof (buf))
Packit 6c4009
    error (EXIT_FAILURE, errno, "during write");
Packit 6c4009
Packit 6c4009
  for (i = 100; i < 200; ++i)
Packit 6c4009
    buf[i] = i;
Packit 6c4009
  ret = pwrite (fd, buf + 100, 100, offset + 100);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "during pwrite");
Packit 6c4009
Packit 6c4009
  for (i = 450; i < 600; ++i)
Packit 6c4009
    buf[i] = i;
Packit 6c4009
  ret = pwrite (fd, buf + 450, 150, offset + 450);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "during pwrite");
Packit 6c4009
Packit 6c4009
  ret = pread (fd, res, sizeof (buf) - 50, offset + 50);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "during pread");
Packit 6c4009
Packit 6c4009
  if (memcmp (buf + 50, res, ret) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: read of pread != write of pwrite\n");
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}