xref: /plan9/sys/src/ape/lib/ap/stdio/tmpfile.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 /*
2  * This file not used on plan9: see ../plan9/tmpfile.c
3  */
4 /*
5  * pANS stdio -- tmpfile
6  *
7  * Bug: contains a critical section.  Two executions by the same
8  * user could interleave as follows, both yielding the same file:
9  *	access fails
10  *			access fails
11  *	fopen succeeds
12  *			fopen succeeds
13  *	unlink succeeds
14  *			unlink fails
15  * As I read the pANS, this can't reasonably use tmpnam to generate
16  * the name, so that code is duplicated.
17  */
18 #include "iolib.h"
tmpfile(void)19 FILE *tmpfile(void){
20 	FILE *f;
21 	static char name[]="/tmp/tf000000000000";
22 	char *p;
23 	while(access(name, 0)==0){
24 		p=name+7;
25 		while(*p=='9') *p++='0';
26 		if(*p=='\0') return NULL;
27 		++*p;
28 	}
29 	f=fopen(name, "wb+");
30 	unlink(name);
31 	return f;
32 }
33