Blame gnulib-tests/fdopen.c

Packit 709fb3
/* Open a stream with a given file descriptor.
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 of the License, or
Packit 709fb3
   (at your option) 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
/* Specification.  */
Packit 709fb3
#include <stdio.h>
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
Packit 709fb3
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 709fb3
# include "msvc-inval.h"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#undef fdopen
Packit 709fb3
Packit 709fb3
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 709fb3
static FILE *
Packit 709fb3
fdopen_nothrow (int fd, const char *mode)
Packit 709fb3
{
Packit 709fb3
  FILE *result;
Packit 709fb3
Packit 709fb3
  TRY_MSVC_INVAL
Packit 709fb3
    {
Packit 709fb3
      result = fdopen (fd, mode);
Packit 709fb3
    }
Packit 709fb3
  CATCH_MSVC_INVAL
Packit 709fb3
    {
Packit 709fb3
      result = NULL;
Packit 709fb3
    }
Packit 709fb3
  DONE_MSVC_INVAL;
Packit 709fb3
Packit 709fb3
  return result;
Packit 709fb3
}
Packit 709fb3
#else
Packit 709fb3
# define fdopen_nothrow fdopen
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
FILE *
Packit 709fb3
rpl_fdopen (int fd, const char *mode)
Packit 709fb3
{
Packit 709fb3
  int saved_errno = errno;
Packit 709fb3
  FILE *fp;
Packit 709fb3
Packit 709fb3
  errno = 0;
Packit 709fb3
  fp = fdopen_nothrow (fd, mode);
Packit 709fb3
  if (fp == NULL)
Packit 709fb3
    {
Packit 709fb3
      if (errno == 0)
Packit 709fb3
        errno = EBADF;
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
    errno = saved_errno;
Packit 709fb3
Packit 709fb3
  return fp;
Packit 709fb3
}