~ubuntu-branches/ubuntu/precise/eglibc/precise-201308281639

« back to all changes in this revision

Viewing changes to libio/freopen.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-02-08 01:58:09 UTC
  • mfrom: (1.5.3) (288.1.12 precise)
  • Revision ID: package-import@ubuntu.com-20120208015809-ulscst7uteq3e22z
Tags: 2.15~pre6-0ubuntu10
Merge from Debian (r5151, 2.13-26).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 1993,95,96,97,98,2000,2001,2002,2003,2008
 
1
/* Copyright (C) 1993,95,96,97,98,2000,2001,2002,2003,2008,2011
2
2
        Free Software Foundation, Inc.
3
3
   This file is part of the GNU C Library.
4
4
 
28
28
 
29
29
#include "libioP.h"
30
30
#include "stdio.h"
 
31
#include <fcntl.h>
31
32
#include <stdlib.h>
 
33
#include <unistd.h>
32
34
 
33
35
#include <shlib-compat.h>
34
36
#include <fd_to_filename.h>
40
42
     FILE* fp;
41
43
{
42
44
  FILE *result;
43
 
  int fd = -1;
44
45
  CHECK_FILE (fp, NULL);
45
46
  if (!(fp->_flags & _IO_IS_FILEBUF))
46
47
    return NULL;
47
48
  _IO_acquire_lock (fp);
48
 
  if (filename == NULL && _IO_fileno (fp) >= 0)
49
 
    {
50
 
      fd = __dup (_IO_fileno (fp));
51
 
      if (fd != -1)
52
 
        filename = fd_to_filename (fd);
53
 
    }
 
49
  int fd = _IO_fileno (fp);
 
50
  const char *gfilename = (filename == NULL && fd >= 0
 
51
                           ? fd_to_filename (fd) : filename);
 
52
  fp->_flags2 |= _IO_FLAGS2_NOCLOSE;
54
53
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
55
54
  if (&_IO_stdin_used == NULL)
56
55
    {
61
60
         up here. */
62
61
      _IO_old_file_close_it (fp);
63
62
      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_old_file_jumps;
64
 
      result = _IO_old_file_fopen (fp, filename, mode);
 
63
      result = _IO_old_file_fopen (fp, gfilename, mode);
65
64
    }
66
65
  else
67
66
#endif
70
69
      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_file_jumps;
71
70
      if (_IO_vtable_offset (fp) == 0 && fp->_wide_data != NULL)
72
71
        fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
73
 
      result = INTUSE(_IO_file_fopen) (fp, filename, mode, 1);
 
72
      result = INTUSE(_IO_file_fopen) (fp, gfilename, mode, 1);
74
73
      if (result != NULL)
75
74
        result = __fopen_maybe_mmap (result);
76
75
    }
 
76
  fp->_flags2 &= ~_IO_FLAGS2_NOCLOSE;
77
77
  if (result != NULL)
78
 
    /* unbound stream orientation */
79
 
    result->_mode = 0;
80
 
  if (fd != -1)
81
78
    {
82
 
      __close (fd);
83
 
      free ((char *) filename);
 
79
      /* unbound stream orientation */
 
80
      result->_mode = 0;
 
81
 
 
82
      if (fd != -1)
 
83
        {
 
84
#ifdef O_CLOEXEC
 
85
# ifndef __ASSUME_DUP3
 
86
          int newfd;
 
87
          if (__have_dup3 < 0)
 
88
            newfd = -1;
 
89
          else
 
90
            newfd =
 
91
# endif
 
92
              dup3 (_IO_fileno (result), fd,
 
93
                    (result->_flags2 & _IO_FLAGS2_CLOEXEC) != 0
 
94
                    ? O_CLOEXEC : 0);
 
95
#else
 
96
# define newfd 1
 
97
#endif
 
98
 
 
99
#ifndef __ASSUME_DUP3
 
100
          if (newfd < 0)
 
101
            {
 
102
              if (errno == ENOSYS)
 
103
                __have_dup3 = -1;
 
104
 
 
105
              __dup2 (_IO_fileno (result), fd);
 
106
              if ((result->_flags2 & _IO_FLAGS2_CLOEXEC) != 0)
 
107
                __fcntl (fd, F_SETFD, FD_CLOEXEC);
 
108
            }
 
109
#endif
 
110
          __close (_IO_fileno (result));
 
111
          _IO_fileno (result) = fd;
 
112
        }
84
113
    }
 
114
  else if (fd != -1)
 
115
    __close (fd);
 
116
  if (filename == NULL)
 
117
    free ((char *) gfilename);
 
118
 
85
119
  _IO_release_lock (fp);
86
120
  return result;
87
121
}