From: kramm Date: Sun, 19 Nov 2006 21:35:17 +0000 (+0000) Subject: added mktempname function X-Git-Tag: release-0-8-0~126 X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=commitdiff_plain;h=267fbed207d24342f1117f83c944553c2360652b added mktempname function --- diff --git a/lib/os.c b/lib/os.c index c8135f6..207c3c1 100755 --- a/lib/os.c +++ b/lib/os.c @@ -147,3 +147,48 @@ char* stripFilename(char*filename, char*newext) return name; } +static char* getTempDir() +{ +#ifdef WIN32 + char*dir = getenv("TMP"); + if(!dir) dir = getenv("TEMP"); + if(!dir) dir = getenv("tmp"); + if(!dir) dir = getenv("temp"); + if(!dir) dir = "C:\\"; +#else + char* dir = "/tmp/"; +#endif + return dir; +} + +char* mktempname(char*ptr) { + static char tmpbuf[128]; + char*dir = getTempDir(); + int l = strlen(dir); + char*sep = ""; + if(!ptr) + ptr = tmpbuf; + if(l && dir[l-1]!='/' && dir[l-1]!='\\') { +#ifdef WIN32 + sep = "\\"; +#else + sep = "/"; +#endif + } + + // used to be mktemp. This does remove the warnings, but + // It's not exactly an improvement. +#ifdef HAVE_LRAND48 + sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48()); +#else +# ifdef HAVE_RAND + sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand()); +# else + static int count = 1; + sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count); + count ++; +# endif +#endif + return ptr; +} + diff --git a/lib/os.h b/lib/os.h index 8f33eb8..3dae4e6 100755 --- a/lib/os.h +++ b/lib/os.h @@ -38,6 +38,8 @@ char* getInstallationPath(); char* concatPaths(char*base, char*add); char* stripFilename(char*filename, char*newext); +char* mktempname(char*); + #ifdef __cplusplus } #endif