Blame stdio-common/tst-put-error.c

Packit 6c4009
/* Verify that print functions return error when there is an I/O error.
Packit 6c4009
Packit 6c4009
   Copyright (C) 2005-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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char tmpl[] = "/tmp/tst-put-error.XXXXXX";
Packit 6c4009
  int fd = mkstemp (tmpl);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot create temporary file");
Packit 6c4009
  FILE *fp = fdopen (fd, "w");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    error (EXIT_FAILURE, errno, "fdopen");
Packit 6c4009
Packit 6c4009
  /* All of the tests below verify that flushing buffers result in failure of
Packit 6c4009
     the fprintf calls.  We ensure that the buffer is flushed at the end of
Packit 6c4009
     each fprintf call by doing two things - setting the file pointer to
Packit 6c4009
     line-buffered so that it is flushed whenever it encounters a newline and
Packit 6c4009
     then ensuring that there is a newline in each of the format strings we
Packit 6c4009
     pass to fprintf.  */
Packit 6c4009
Packit 6c4009
  setlinebuf (fp);
Packit 6c4009
  close (fd);
Packit 6c4009
  unlink (tmpl);
Packit 6c4009
Packit 6c4009
  int n = fprintf (fp, "hello world\n");
Packit 6c4009
  printf ("fprintf = %d\n", n);
Packit 6c4009
  if (n >= 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "first fprintf succeeded");
Packit 6c4009
Packit 6c4009
  n = fprintf (fp, "hello world\n");
Packit 6c4009
  printf ("fprintf = %d\n", n);
Packit 6c4009
  if (n >= 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "second fprintf succeeded");
Packit 6c4009
Packit 6c4009
  /* Padded printing takes a different code path.  */
Packit 6c4009
  n = fprintf (fp, "%100s\n", "foo");
Packit 6c4009
  printf ("fprintf = %d\n", n);
Packit 6c4009
  if (n >= 0)
Packit 6c4009
    error (EXIT_FAILURE, 0, "padded fprintf succeeded");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"