Blame gnulib-tests/test-fdopen.c

Packit Service 2723c6
/* Test opening a stream with a file descriptor.
Packit Service 2723c6
   Copyright (C) 2011-2018 Free Software Foundation, Inc.
Packit Service 2723c6
Packit Service 2723c6
   This program is free software: you can redistribute it and/or modify
Packit Service 2723c6
   it under the terms of the GNU General Public License as published by
Packit Service 2723c6
   the Free Software Foundation; either version 3 of the License, or
Packit Service 2723c6
   (at your option) any later version.
Packit Service 2723c6
Packit Service 2723c6
   This program is distributed in the hope that it will be useful,
Packit Service 2723c6
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2723c6
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 2723c6
   GNU General Public License for more details.
Packit Service 2723c6
Packit Service 2723c6
   You should have received a copy of the GNU General Public License
Packit Service 2723c6
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 2723c6
Packit Service 2723c6
#include <config.h>
Packit Service 2723c6
Packit Service 2723c6
#include <stdio.h>
Packit Service 2723c6
Packit Service 2723c6
#include "signature.h"
Packit Service 2723c6
SIGNATURE_CHECK (fdopen, FILE *, (int, const char *));
Packit Service 2723c6
Packit Service 2723c6
#include <errno.h>
Packit Service 2723c6
#include <unistd.h>
Packit Service 2723c6
Packit Service 2723c6
#include "macros.h"
Packit Service 2723c6
Packit Service 2723c6
int
Packit Service 2723c6
main (void)
Packit Service 2723c6
{
Packit Service 2723c6
  /* Test behavior on failure.  POSIX makes it hard to check for
Packit Service 2723c6
     failure, since the behavior is not well-defined on invalid file
Packit Service 2723c6
     descriptors, so try fdopen 1000 times and if that's not enough to
Packit Service 2723c6
     fail due to EMFILE, so be it.  */
Packit Service 2723c6
Packit Service 2723c6
  int i;
Packit Service 2723c6
  for (i = 0; i < 1000; i++)
Packit Service 2723c6
    {
Packit Service 2723c6
      errno = 0;
Packit Service 2723c6
      if (! fdopen (STDOUT_FILENO, "w"))
Packit Service 2723c6
        {
Packit Service 2723c6
          ASSERT (errno != 0);
Packit Service 2723c6
          break;
Packit Service 2723c6
        }
Packit Service 2723c6
    }
Packit Service 2723c6
Packit Service 2723c6
  return 0;
Packit Service 2723c6
}