Blame gnulib-tests/test-fgetc.c

Packit 709fb3
/* Test of fgetc() function.
Packit 709fb3
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software; you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3, or (at your option)
Packit 709fb3
   any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include <stdio.h>
Packit 709fb3
Packit 709fb3
#include "signature.h"
Packit 709fb3
SIGNATURE_CHECK (fgetc, int, (FILE *));
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <fcntl.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 709fb3
# include "msvc-inval.h"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (int argc, char **argv)
Packit 709fb3
{
Packit 709fb3
  const char *filename = "test-fgetc.txt";
Packit 709fb3
Packit 709fb3
  /* We don't have an fgetc() function that installs an invalid parameter
Packit 709fb3
     handler so far.  So install that handler here, explicitly.  */
Packit 709fb3
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER \
Packit 709fb3
    && MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
Packit 709fb3
  gl_msvc_inval_ensure_handler ();
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  /* Prepare a file.  */
Packit 709fb3
  {
Packit 709fb3
    const char text[] = "hello world";
Packit 709fb3
    int fd = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
Packit 709fb3
    ASSERT (fd >= 0);
Packit 709fb3
    ASSERT (write (fd, text, sizeof (text)) == sizeof (text));
Packit 709fb3
    ASSERT (close (fd) == 0);
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Test that fgetc() sets errno if someone else closes the stream
Packit 709fb3
     fd behind the back of stdio.  */
Packit 709fb3
  {
Packit 709fb3
    FILE *fp = fopen (filename, "r");
Packit 709fb3
    ASSERT (fp != NULL);
Packit 709fb3
    ASSERT (close (fileno (fp)) == 0);
Packit 709fb3
    errno = 0;
Packit 709fb3
    ASSERT (fgetc (fp) == EOF);
Packit 709fb3
    ASSERT (errno == EBADF);
Packit 709fb3
    ASSERT (ferror (fp));
Packit 709fb3
    fclose (fp);
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Test that fgetc() sets errno if the stream was constructed with
Packit 709fb3
     an invalid file descriptor.  */
Packit 709fb3
  {
Packit 709fb3
    FILE *fp = fdopen (-1, "r");
Packit 709fb3
    if (fp != NULL)
Packit 709fb3
      {
Packit 709fb3
        errno = 0;
Packit 709fb3
        ASSERT (fgetc (fp) == EOF);
Packit 709fb3
        ASSERT (errno == EBADF);
Packit 709fb3
        ASSERT (ferror (fp));
Packit 709fb3
        fclose (fp);
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
  {
Packit 709fb3
    FILE *fp;
Packit 709fb3
    close (99);
Packit 709fb3
    fp = fdopen (99, "r");
Packit 709fb3
    if (fp != NULL)
Packit 709fb3
      {
Packit 709fb3
        errno = 0;
Packit 709fb3
        ASSERT (fgetc (fp) == EOF);
Packit 709fb3
        ASSERT (errno == EBADF);
Packit 709fb3
        ASSERT (ferror (fp));
Packit 709fb3
        fclose (fp);
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Clean up.  */
Packit 709fb3
  unlink (filename);
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}