Skip to content

Commit 766bc4e

Browse files
committed
Introduce Abc_GetTmpDir() to set the tmp directory via an environment variable.
1 parent c798fea commit 766bc4e

6 files changed

Lines changed: 48 additions & 15 deletions

File tree

src/aig/gia/giaStoch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Vec_Int_t * Gia_StochProcessArray( Vec_Ptr_t * vGias, char * pScript, int TimeSe
115115
Gia_Man_t * Gia_StochProcessOne( Gia_Man_t * p, char * pScript, int Rand, int TimeSecs )
116116
{
117117
Gia_Man_t * pNew;
118-
char FileName[100], Command[1000];
119-
sprintf( FileName, "%06x.aig", Rand );
118+
char FileName[1000], Command[2000];
119+
sprintf( FileName, "%s/%06x.aig", Abc_GetTmpDir(), Rand );
120120
Gia_AigerWrite( p, FileName, 0, 0, 0 );
121121
sprintf( Command, "./abc -q \"&read %s; %s; &write %s\"", FileName, pScript, FileName );
122122
#if defined(__wasm)

src/base/abci/abcPart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,10 @@ Abc_Ntk_t * Abc_NtkStochProcessOne( Abc_Ntk_t * p, char * pScript0, int Rand, in
12961296
extern int Abc_NtkWriteToFile( char * pFileName, Abc_Ntk_t * pNtk );
12971297
extern Abc_Ntk_t * Abc_NtkReadFromFile( char * pFileName );
12981298
Abc_Ntk_t * pNew, * pTemp;
1299-
char FileName[100], Command[1000], PreCommand[500] = {0};
1299+
char FileName[1000], Command[2000], PreCommand[500] = {0};
13001300
char * pLibFileName = Abc_NtkIsMappedLogic(p) ? Mio_LibraryReadFileName((Mio_Library_t *)p->pManFunc) : NULL;
13011301
if ( pLibFileName ) sprintf( PreCommand, "read_genlib %s; ", pLibFileName );
1302-
sprintf( FileName, "%06x.mm", Rand );
1302+
sprintf( FileName, "%s/%06x.mm", Abc_GetTmpDir(), Rand );
13031303
Abc_NtkWriteToFile( FileName, p );
13041304
char * pScript = Abc_UtilStrsav( pScript0 );
13051305
sprintf( Command, "./abc -q \"%sread_mm %s; %s; write_mm %s\"", PreCommand[0] ? PreCommand : "", FileName, pScript, FileName );

src/base/sn/snCom.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ static int Sn_TempPrefix( char * pBuffer, size_t nBuffer, const char * pStem )
9696
{
9797
int Written;
9898
#if defined(_MSC_VER) || defined(__MINGW32__)
99-
const char * pDirectory = getenv( "TEMP" );
100-
if ( pDirectory == NULL )
101-
pDirectory = ".";
102-
Written = snprintf( pBuffer, nBuffer, "%s\\%s", pDirectory, pStem );
99+
Written = snprintf( pBuffer, nBuffer, "%s\\%s", Abc_GetTmpDir(), pStem );
103100
#else
104-
Written = snprintf( pBuffer, nBuffer, "/tmp/%s", pStem );
101+
Written = snprintf( pBuffer, nBuffer, "%s/%s", Abc_GetTmpDir(), pStem );
105102
#endif
106103
return Written >= 0 && (size_t)Written < nBuffer;
107104
}

src/misc/util/abc_global.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,25 @@ static inline void Abc_ReverseOrder( int * pA, int nA )
590590
ABC_SWAP( int, pA[i], pA[nA-1-i] );
591591
}
592592

593+
static inline const char * Abc_GetTmpDir()
594+
{
595+
const char * s;
596+
#if defined(_MSC_VER) || defined(__MINGW32__)
597+
s = getenv( "TMP" );
598+
if ( s && *s )
599+
return s;
600+
s = getenv( "TEMP" );
601+
if ( s && *s )
602+
return s;
603+
return ".";
604+
#else
605+
s = getenv( "TMPDIR" );
606+
if ( s && *s )
607+
return s;
608+
return "/tmp";
609+
#endif
610+
}
611+
593612

594613
// sorting
595614
extern void Abc_MergeSort( int * pInput, int nSize );

src/misc/util/utilAigSim.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <assert.h>
2626
#include <ctype.h>
2727
#include <time.h>
28+
#include "abc_global.h"
2829
#ifdef _WIN32
2930
#include <io.h>
3031
#define mkstemp(p) _mktemp_s(p, strlen(p)+1)
@@ -345,8 +346,11 @@ static int make_tmp_file(char *path, size_t cap, const char *prefix) {
345346
static int seq = 0; // no risk of collision since we're in a sandbox
346347
snprintf(path, cap, "%s%08d", prefix, seq++);
347348
int fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
349+
#elif defined(_WIN32)
350+
snprintf(path, cap, "%s\\%sXXXXXX", Abc_GetTmpDir(), prefix);
351+
int fd = mkstemp(path);
348352
#else
349-
snprintf(path, cap, "/tmp/%sXXXXXX", prefix);
353+
snprintf(path, cap, "%s/%sXXXXXX", Abc_GetTmpDir(), prefix);
350354
int fd = mkstemp(path);
351355
#endif
352356
if (fd < 0) return 0;
@@ -360,8 +364,11 @@ static int make_tmp_path_noexist(char *path, size_t cap, const char *prefix) {
360364
static int seq = 0; // no risk of collision since we're in a sandbox
361365
snprintf(path, cap, "%s%08d", prefix, seq++);
362366
int fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
367+
#elif defined(_WIN32)
368+
snprintf(path, cap, "%s\\%sXXXXXX", Abc_GetTmpDir(), prefix);
369+
int fd = mkstemp(path);
363370
#else
364-
snprintf(path, cap, "/tmp/%sXXXXXX", prefix);
371+
snprintf(path, cap, "%s/%sXXXXXX", Abc_GetTmpDir(), prefix);
365372
int fd = mkstemp(path);
366373
#endif
367374
if (fd < 0) return 0;

src/misc/util/utilFile.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ int tmpFile(const char* prefix, const char* suffix, char** out_name)
107107
{
108108
#if defined(_MSC_VER) || defined(__MINGW32__)
109109
int i, fd;
110-
*out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 27);
110+
const char* dir = (strchr(prefix, '/') == NULL && strchr(prefix, '\\') == NULL) ? Abc_GetTmpDir() : "";
111+
int need_slash = (*dir != '\0' && dir[strlen(dir)-1] != '\\' && dir[strlen(dir)-1] != '/');
112+
*out_name = (char*)malloc(strlen(dir) + need_slash + strlen(prefix) + strlen(suffix) + 27);
111113
for (i = 0; i < 10; i++){
112-
sprintf(*out_name, "%s%I64X%d%s", prefix, realTimeAbs(), _getpid(), suffix);
114+
if (*dir != '\0')
115+
sprintf(*out_name, "%s%s%s%I64X%d%s", dir, need_slash ? "\\" : "", prefix, realTimeAbs(), _getpid(), suffix);
116+
else
117+
sprintf(*out_name, "%s%I64X%d%s", prefix, realTimeAbs(), _getpid(), suffix);
113118
fd = _open(*out_name, O_CREAT | O_EXCL | O_BINARY | O_RDWR, _S_IREAD | _S_IWRITE);
114119
if (fd == -1){
115120
free(*out_name);
@@ -132,9 +137,14 @@ int tmpFile(const char* prefix, const char* suffix, char** out_name)
132137
return fd;
133138
#else
134139
int fd;
135-
*out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 7);
140+
const char* dir = (strchr(prefix, '/') == NULL) ? Abc_GetTmpDir() : "";
141+
int need_slash = (*dir != '\0' && dir[strlen(dir)-1] != '/');
142+
*out_name = (char*)malloc(strlen(dir) + need_slash + strlen(prefix) + strlen(suffix) + 7);
136143
assert(*out_name != NULL);
137-
sprintf(*out_name, "%sXXXXXX", prefix);
144+
if (*dir != '\0')
145+
sprintf(*out_name, "%s%s%sXXXXXX", dir, need_slash ? "/" : "", prefix);
146+
else
147+
sprintf(*out_name, "%sXXXXXX", prefix);
138148
fd = mkstemp(*out_name);
139149
if (fd == -1){
140150
free(*out_name);

0 commit comments

Comments
 (0)