Blame lib/freopen.c

Packit Service fdd496
/* Open a stream to a file.
Packit Service fdd496
   Copyright (C) 2007-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit Service fdd496
Packit Service fdd496
/* If the user's config.h happens to include <stdio.h>, let it include only
Packit Service fdd496
   the system's <stdio.h> here, so that orig_freopen doesn't recurse to
Packit Service fdd496
   rpl_freopen.  */
Packit Service fdd496
#define _GL_ALREADY_INCLUDING_STDIO_H
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
/* Get the original definition of freopen.  It might be defined as a macro.  */
Packit Service fdd496
#include <stdio.h>
Packit Service fdd496
#undef _GL_ALREADY_INCLUDING_STDIO_H
Packit Service fdd496
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
Packit Service fdd496
static FILE *
Packit Service fdd496
orig_freopen (const char *filename, const char *mode, FILE *stream)
Packit Service fdd496
{
Packit Service fdd496
  return freopen (filename, mode, stream);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
/* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
Packit Service fdd496
   this include because of the preliminary #include <stdio.h> above.  */
Packit Service fdd496
#include "stdio.h"
Packit Service fdd496
Packit Service fdd496
#include <fcntl.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
#include <unistd.h>
Packit Service fdd496
Packit Service fdd496
FILE *
Packit Service fdd496
rpl_freopen (const char *filename, const char *mode, FILE *stream)
Packit Service fdd496
{
Packit Service fdd496
  FILE *result;
Packit Service fdd496
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit Service fdd496
  char const *null_device = "NUL";
Packit Service fdd496
  if (filename && strcmp (filename, "/dev/null") == 0)
Packit Service fdd496
    filename = null_device;
Packit Service fdd496
#else
Packit Service fdd496
  char const *null_device = "/dev/null";
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#ifdef __KLIBC__
Packit Service fdd496
  errno = 0;
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  result = orig_freopen (filename, mode, stream);
Packit Service fdd496
Packit Service fdd496
  if (!result)
Packit Service fdd496
    {
Packit Service fdd496
#ifdef __KLIBC__
Packit Service fdd496
      /* On OS/2 kLIBC, freopen returns NULL even if it is successful
Packit Service fdd496
         if filename is NULL. */
Packit Service fdd496
      if (!filename && !errno)
Packit Service fdd496
        result = stream;
Packit Service fdd496
#endif
Packit Service fdd496
    }
Packit Service fdd496
  else if (filename)
Packit Service fdd496
    {
Packit Service fdd496
      int fd = fileno (result);
Packit Service fdd496
      if (dup2 (fd, fd) < 0 && errno == EBADF)
Packit Service fdd496
        {
Packit Service fdd496
          int nullfd = open (null_device, O_RDONLY | O_CLOEXEC);
Packit Service fdd496
          int err = 0;
Packit Service fdd496
          if (nullfd != fd)
Packit Service fdd496
            {
Packit Service fdd496
              if (dup2 (nullfd, fd) < 0)
Packit Service fdd496
                err = 1;
Packit Service fdd496
              close (nullfd);
Packit Service fdd496
            }
Packit Service fdd496
          if (!err)
Packit Service fdd496
            result = orig_freopen (filename, mode, result);
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  return result;
Packit Service fdd496
}