summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/stdio/_IO_newfile.c
blob: 6a016ec5d42e13b796b0a839f768993d7586de3b (plain)
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
/*
 * pANS stdio -- fopen
 */
#include "iolib.h"

#define _PLAN9_SOURCE
#include <lock.h>

FILE *_IO_newfile(void)
{
	static FILE *fx=0;
	static Lock fl;
	FILE *f;
	int i;

	lock(&fl);
	for(i=0; i<FOPEN_MAX; i++){
		if(fx==0 || ++fx >= &_IO_stream[FOPEN_MAX]) fx=_IO_stream;
		if(fx->state==CLOSED)
			break;
	}
	f = fx;
	unlock(&fl);
	if(f->state!=CLOSED)
		return NULL;
	return f;
}