xref: /plan9/sys/src/ape/lib/ap/stdio/freopen.c (revision d9306527b4a7229dcf0cf3c58aed36bb9da82854)
1 /*
2  * pANS stdio -- freopen
3  */
4 #include "iolib.h"
5 /*
6  * Open the named file with the given mode, using the given FILE
7  * Legal modes are given below, `additional characters may follow these sequences':
8  * r rb		open to read
9  * w wb		open to write, truncating
10  * a ab		open to write positioned at eof, creating if non-existant
11  * r+ r+b rb+	open to read and write, creating if non-existant
12  * w+ w+b wb+	open to read and write, truncating
13  * a+ a+b ab+	open to read and write, positioned at eof, creating if non-existant.
14  */
freopen(const char * name,const char * mode,FILE * f)15 FILE *freopen(const char *name, const char *mode, FILE *f){
16 	int m;
17 
18 	if(f->state!=CLOSED){
19 		fclose(f);
20 /* premature; fall through and see what happens */
21 /*		f->state=OPEN; */
22 	}
23 
24 	m = *mode++;
25 	if(m == 0)
26 		return NULL;
27 	if(*mode == 'b')
28 		mode++;
29 	switch(m){
30 	default:
31 		return NULL;
32 	case 'r':
33 		f->fd=open(name, (*mode == '+'? O_RDWR: O_RDONLY));
34 		break;
35 	case 'w':
36 		f->fd=creat(name, 0666);	/* implicitly O_WRONLY */
37 		/* for O_RDWR, have to creat, close, open */
38 		if(*mode == '+' && f->fd >= 0) {
39 			close(f->fd);
40 			f->fd=open(name, O_RDWR);
41 		}
42 		break;
43 	case 'a':
44 		f->fd=open(name, (*mode == '+'? O_RDWR: O_WRONLY));
45 		if(f->fd<0) {
46 			f->fd=creat(name, 0666);
47 			/* for O_RDWR, have to creat, close, open */
48 			if(*mode == '+' && f->fd >= 0) {
49 				close(f->fd);
50 				f->fd=open(name, O_RDWR);
51 			}
52 		}
53 		lseek(f->fd, 0L, 2);
54 		break;
55 	}
56 
57 	if(f->fd==-1)
58 		return NULL;
59 	f->flags=(mode[0]=='a')? APPEND : 0;
60 	f->state=OPEN;
61 	f->buf=0;
62 	f->rp=0;
63 	f->wp=0;
64 	f->lp=0;
65 	return f;
66 }
67