Blame stdio-common/tst-fmemopen.c

Packit 6c4009
/* basic fmemopen interface testing.
Packit 6c4009
   Copyright (C) 2014-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 <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
Packit 6c4009
static char *test_file;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  /* Construct the test file name based on ARGV[0], which will be
Packit 6c4009
     an absolute file name in the build directory.  Don't touch the
Packit 6c4009
     source directory, which might be read-only.  */
Packit 6c4009
  if (asprintf (&test_file, "%s.test", argv[0]) < 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("asprintf failed\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  const char blah[] = "BLAH";
Packit 6c4009
  FILE *fp;
Packit 6c4009
  char *mmap_data;
Packit 6c4009
  int ch, fd;
Packit 6c4009
  struct stat fs;
Packit 6c4009
  const char *cp;
Packit 6c4009
Packit 6c4009
  /* setup the physical file, and use it */
Packit 6c4009
  if ((fp = fopen (test_file, "w+")) == NULL)
Packit 6c4009
    return 1;
Packit 6c4009
  if (fwrite (blah, 1, strlen (blah), fp) != strlen (blah))
Packit 6c4009
    {
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 2;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  rewind (fp);
Packit 6c4009
  printf ("file: ");
Packit 6c4009
  cp = blah;
Packit 6c4009
  while ((ch = getc (fp)) != EOF)
Packit 6c4009
    {
Packit 6c4009
      fputc (ch, stdout);
Packit 6c4009
      if (ch != *cp)
Packit 6c4009
	{
Packit 6c4009
	  printf ("\ncharacter %td: '%c' instead of '%c'\n",
Packit 6c4009
		  cp - blah, ch, *cp);
Packit 6c4009
	  fclose (fp);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      ++cp;
Packit 6c4009
    }
Packit 6c4009
  fputc ('\n', stdout);
Packit 6c4009
  if (ferror (fp))
Packit 6c4009
    {
Packit 6c4009
      puts ("fp: error");
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (feof (fp))
Packit 6c4009
    printf ("fp: EOF\n");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      puts ("not EOF");
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  /* Now, mmap the file into a buffer, and do that too */
Packit 6c4009
  if ((fd = open (test_file, O_RDONLY)) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("open (%s, O_RDONLY) failed\n", test_file);
Packit 6c4009
      return 3;
Packit 6c4009
    }
Packit 6c4009
  if (fstat (fd, &fs) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("stat (%i)\n", fd);
Packit 6c4009
      return 4;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((mmap_data = (char *) mmap (NULL, fs.st_size, PROT_READ,
Packit 6c4009
				  MAP_SHARED, fd, 0)) == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      printf ("mmap (NULL, %zu, PROT_READ, MAP_SHARED, %i, 0) failed\n",
Packit 6c4009
	      (size_t) fs.st_size, fd);
Packit 6c4009
      return 5;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((fp = fmemopen (mmap_data, fs.st_size, "r")) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("fmemopen (%p, %zu) failed\n", mmap_data, (size_t) fs.st_size);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("mem: ");
Packit 6c4009
  cp = blah;
Packit 6c4009
  while ((ch = getc (fp)) != EOF)
Packit 6c4009
    {
Packit 6c4009
      fputc (ch, stdout);
Packit 6c4009
      if (ch != *cp)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%td character: '%c' instead of '%c'\n",
Packit 6c4009
		  cp - blah, ch, *cp);
Packit 6c4009
	  fclose (fp);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      ++cp;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fputc ('\n', stdout);
Packit 6c4009
Packit 6c4009
  if (ferror (fp))
Packit 6c4009
    {
Packit 6c4009
      puts ("fp: error");
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (feof (fp))
Packit 6c4009
    printf ("fp: EOF\n");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      puts ("not EOF");
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  munmap (mmap_data, fs.st_size);
Packit 6c4009
Packit 6c4009
  unlink (test_file);
Packit 6c4009
  free (test_file);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE(argc, argv) do_prepare (argc, argv)
Packit 6c4009
#define TEST_FUNCTION       do_test ()
Packit 6c4009
#include "../test-skeleton.c"