Now calls tmpnam.

This commit is contained in:
jshepher 2004-12-04 00:52:04 +00:00
parent 8acaa7445a
commit 6a0d455dd0
1 changed files with 24 additions and 29 deletions

View File

@ -1,34 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <stdio.h>
#include <sys/syslimits.h>
/* NOTE: NOT thread-safe */
FILE *tmpfile(void)
{ char *name;
FILE *file;
BPTR dirlock,filelock,cd;
static unsigned long filecount=0;
name=malloc(32); /* buffer for filename */
if(name!=NULL)
{ dirlock=Lock("T:",ACCESS_READ); /* lock T: directory */
if(dirlock!=0)
{ cd=CurrentDir(dirlock); /* cd T: */
do /* generate a filename that doesn't exist */
{ sprintf(name,"tempfile_1_%p_%lu",FindTask(NULL),filecount++);
filelock=Lock(name,ACCESS_WRITE);
if(filelock!=0)
UnLock(filelock);
}while(filelock!=0||IoErr()==ERROR_OBJECT_IN_USE);
file=fopen(name,"wb+");
CurrentDir(cd);
if(file!=NULL)
{ file->tmpdir=dirlock;
file->name=name;
return file; }
UnLock(dirlock);
{
char *name = malloc(PATH_MAX);
FILE *file;
if (name == NULL)
goto error;
if (tmpnam(name) == NULL) {
goto error;
}
free(name);
}
return NULL;
file = fopen(name,"w+b");
if (file == NULL)
goto error;
file->name = name;
return file;
error:
if (name != NULL)
free(name);
return NULL;
}