one too many levels of indirection (from ks@purdue)
[unix-history] / usr / src / lib / libc / stdio / freopen.c
CommitLineData
586c39b1
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)freopen.c 5.1 (Berkeley) %G%";
9#endif not lint
10
41e01b3e
S
11#include <sys/types.h>
12#include <sys/file.h>
13#include <stdio.h>
2de31004
BJ
14
15FILE *
16freopen(file, mode, iop)
41e01b3e
S
17 char *file;
18 register char *mode;
19 register FILE *iop;
2de31004 20{
41e01b3e 21 register f, rw, oflags;
d8af6b8b 22
41e01b3e 23 rw = (mode[1] == '+');
2de31004
BJ
24
25 fclose(iop);
41e01b3e
S
26
27 switch (*mode) {
28 case 'a':
29 oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
30 break;
31 case 'r':
32 oflags = rw ? O_RDWR : O_RDONLY;
33 break;
34 case 'w':
35 oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
36 break;
37 default:
38 return (NULL);
39 }
40
41 f = open(file, oflags, 0666);
2de31004 42 if (f < 0)
41e01b3e
S
43 return (NULL);
44
45 if (*mode == 'a')
46 lseek(f, (off_t)0, L_XTND);
47
d8af6b8b 48 iop->_cnt = 0;
2de31004 49 iop->_file = f;
41e01b3e 50 iop->_bufsiz = 0;
d8af6b8b 51 if (rw)
41e01b3e
S
52 iop->_flag = _IORW;
53 else if (*mode == 'r')
54 iop->_flag = _IOREAD;
2de31004 55 else
41e01b3e
S
56 iop->_flag = _IOWRT;
57 iop->_base = iop->_ptr = NULL;
58 return (iop);
2de31004 59}