aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/freopen.c
blob: 4d4ed997b5fff49243b23f8041ed84cf86771a02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define	import_spp
#define	import_libc
#define	import_stdio
#include <iraf.h>


/* 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);
}