2013-03-10 23:48:35 +00:00
|
|
|
/*
|
|
|
|
* pANS stdio -- fopen
|
|
|
|
*/
|
|
|
|
#include "iolib.h"
|
|
|
|
|
|
|
|
#define _PLAN9_SOURCE
|
|
|
|
#include <lock.h>
|
2014-05-28 22:34:47 +00:00
|
|
|
#include <errno.h>
|
2013-03-10 23:48:35 +00:00
|
|
|
|
|
|
|
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);
|
2014-05-28 22:34:47 +00:00
|
|
|
if(f->state!=CLOSED){
|
|
|
|
errno = EMFILE;
|
2013-03-10 23:48:35 +00:00
|
|
|
return NULL;
|
2014-05-28 22:34:47 +00:00
|
|
|
}
|
2013-03-10 23:48:35 +00:00
|
|
|
return f;
|
|
|
|
}
|