From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- sys/libc/freopen.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 sys/libc/freopen.c (limited to 'sys/libc/freopen.c') diff --git a/sys/libc/freopen.c b/sys/libc/freopen.c new file mode 100644 index 00000000..4d4ed997 --- /dev/null +++ b/sys/libc/freopen.c @@ -0,0 +1,56 @@ +/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc. +*/ + +#define import_spp +#define import_libc +#define import_stdio +#include + + +/* FREOPEN -- Close a stream and reopen it upon the named file. This is +** commonly used to redirect one of the standard streams stdin, stdout, +** or stderr to a named file. +*/ +FILE * +freopen ( + char *fname, /* vfn of file to be opened */ + char *modestr, /* access mode [and type] */ + FILE *fp /* stream to be reopened */ +) +{ + register XINT fd = fileno(fp); + register int status, filetype; + + + /* Determine the file type of the file to be opened. This is given + * by an optional second character in the mode string. Default is + * text file if absent. + */ + switch (modestr[1]) { + case 't': + case EOS: + filetype = TEXT_FILE; + break; + case 'b': + filetype = BINARY_FILE; + break; + default: + return (NULL); + } + + switch (modestr[0]) { + case 'r': + status = c_fredir (fd, fname, READ_ONLY, filetype); + break; + case 'w': + status = c_fredir (fd, fname, NEW_FILE, filetype); + break; + case 'a': + status = c_fredir (fd, fname, APPEND, filetype); + break; + default: + return (NULL); + } + + return (status == ERR ? NULL : fp); +} -- cgit