Blame stdio-common/tst-unlockedio.c

Packit 6c4009
/* Test for some *_unlocked stdio interfaces.
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 <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <libc-diag.h>
Packit 6c4009
Packit 6c4009
int fd;
Packit 6c4009
static void do_prepare (void);
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define PREPARE(argc, argv) do_prepare ()
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  const char blah[] = "BLAH";
Packit 6c4009
  char buf[strlen (blah) + 1];
Packit 6c4009
  FILE *fp, *f;
Packit 6c4009
  const char *cp;
Packit 6c4009
  char *wp;
Packit 6c4009
Packit 6c4009
  if ((fp = fdopen (fd, "w+")) == NULL)
Packit 6c4009
    exit (1);
Packit 6c4009
Packit 6c4009
  flockfile (fp);
Packit 6c4009
Packit 6c4009
  f = fp;
Packit 6c4009
  cp = blah;
Packit 6c4009
  /* These tests deliberately use fwrite_unlocked with the size
Packit 6c4009
     argument specified as 0, which results in "division by zero"
Packit 6c4009
     warnings from the expansion of that macro (in code that is not
Packit 6c4009
     evaluated for a size of 0).  This applies to the tests of
Packit 6c4009
     fread_unlocked below as well.  */
Packit 6c4009
  DIAG_PUSH_NEEDS_COMMENT;
Packit 6c4009
  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero");
Packit 6c4009
  if (ftello (fp) != 0
Packit 6c4009
      || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0
Packit 6c4009
      || f != fp + 1
Packit 6c4009
      || fwrite_unlocked ("", 5.0, 0, --f) != 0
Packit 6c4009
      || f != fp
Packit 6c4009
      || fwrite_unlocked (cp++, 16, 0.25, fp) != 0
Packit 6c4009
      || cp != blah + 1
Packit 6c4009
      || fwrite_unlocked (--cp, 0.25, 16, fp) != 0
Packit 6c4009
      || cp != blah
Packit 6c4009
      || fwrite_unlocked (blah, 0, -0.0, fp) != 0
Packit 6c4009
      || ftello (fp) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("One of fwrite_unlocked tests failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  DIAG_POP_NEEDS_COMMENT;
Packit 6c4009
Packit 6c4009
  if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
Packit 6c4009
    {
Packit 6c4009
      puts ("Could not write string into file");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (putc_unlocked ('A' + 0x1000000, fp) != 'A')
Packit 6c4009
    {
Packit 6c4009
      puts ("putc_unlocked failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f = fp;
Packit 6c4009
  cp = blah + strlen (blah) - 1;
Packit 6c4009
  if (putc_unlocked (*cp++, f++) != 'H'
Packit 6c4009
      || f != fp + 1
Packit 6c4009
      || cp != strchr (blah, '\0'))
Packit 6c4009
    {
Packit 6c4009
      puts ("fputc_unlocked failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ftello (fp) != (off_t) strlen (blah))
Packit 6c4009
    {
Packit 6c4009
      printf ("Failed to write %zd bytes to temporary file", strlen (blah));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  rewind (fp);
Packit 6c4009
Packit 6c4009
  f = fp;
Packit 6c4009
  wp = buf;
Packit 6c4009
  memset (buf, ' ', sizeof (buf));
Packit 6c4009
  /* See explanation above.  */
Packit 6c4009
  DIAG_PUSH_NEEDS_COMMENT;
Packit 6c4009
  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero");
Packit 6c4009
  if (ftello (fp) != 0
Packit 6c4009
      || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0
Packit 6c4009
      || f != fp + 1
Packit 6c4009
      || fread_unlocked (buf, 5.0, 0, --f) != 0
Packit 6c4009
      || f != fp
Packit 6c4009
      || fread_unlocked (wp++, 16, 0.25, fp) != 0
Packit 6c4009
      || wp != buf + 1
Packit 6c4009
      || fread_unlocked (--wp, 0.25, 16, fp) != 0
Packit 6c4009
      || wp != buf
Packit 6c4009
      || fread_unlocked (buf, 0, -0.0, fp) != 0
Packit 6c4009
      || ftello (fp) != 0
Packit 6c4009
      || memcmp (buf, "     ", sizeof (buf)) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("One of fread_unlocked tests failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  DIAG_POP_NEEDS_COMMENT;
Packit 6c4009
Packit 6c4009
  if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
Packit 6c4009
    {
Packit 6c4009
      puts ("Could not read string from file");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (getc_unlocked (fp) != 'A')
Packit 6c4009
    {
Packit 6c4009
      puts ("getc_unlocked failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f = fp;
Packit 6c4009
  if (fgetc_unlocked (f++) != 'H'
Packit 6c4009
      || f != fp + 1
Packit 6c4009
      || fgetc_unlocked (--f) != EOF
Packit 6c4009
      || f != fp)
Packit 6c4009
    {
Packit 6c4009
      puts ("fgetc_unlocked failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ftello (fp) != (off_t) strlen (blah))
Packit 6c4009
    {
Packit 6c4009
      printf ("Failed to read %zd bytes from temporary file", strlen (blah));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  funlockfile (fp);
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  fd = create_temp_file ("tst-unlockedio.", NULL);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create temporary file: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}