glibc/stdio/freopen.c

68 lines
1.9 KiB
C
Raw Normal View History

1994-05-27 06:42:35 +00:00
/* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
1991-06-12 18:24:28 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
1994-05-27 06:42:35 +00:00
/* Defined in fopen.c. */
extern int __getmode __P ((const char *, __io_mode *));
1991-06-12 18:24:28 +00:00
/* Replace STREAM, opening it on FILENAME. */
FILE *
DEFUN(freopen, (filename, mode, stream),
CONST char *filename AND CONST char *mode AND register FILE *stream)
{
1994-05-27 06:42:35 +00:00
__io_mode m;
PTR cookie;
1991-06-12 18:24:28 +00:00
1994-05-27 06:42:35 +00:00
if (!__getmode (mode, &m))
1991-06-12 18:24:28 +00:00
{
1994-05-27 06:42:35 +00:00
(void) fclose (stream);
1991-06-12 18:24:28 +00:00
errno = EINVAL;
return NULL;
}
1994-05-27 06:42:35 +00:00
if (stream->__mode.__write)
/* Flush the stream. */
(void) fflush (stream);
1991-06-12 18:24:28 +00:00
1994-05-27 06:42:35 +00:00
/* Open the file, attempting to preserve the old cookie value. */
cookie = stream->__cookie;
if (__stdio_reopen (filename, m, &cookie, stream->__io_funcs.__close))
{
int save = errno;
(void) fclose (stream);
errno = save;
return NULL;
}
1991-06-12 18:24:28 +00:00
1994-05-27 06:42:35 +00:00
/* Close the stream, first disabling its cookie close function because
__stdio_reopen has already dealt with closing the old cookie. */
stream->__io_funcs.__close = NULL;
(void) fclose (stream);
1991-06-12 18:24:28 +00:00
1994-06-01 00:10:52 +00:00
stream->__magic = _IOMAGIC;
1994-05-27 06:42:35 +00:00
stream->__cookie = cookie;
stream->__mode = m;
1991-06-12 18:24:28 +00:00
return stream;
}