X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fos.c;h=a4142ff04e051750eebb14778fa6621fd2638d5d;hp=8ba360a14e0729c2ea9181ebee47cc636418f740;hb=8589e0d1f5e47c05458033e750fd6182ca704fbe;hpb=c28a7d2e3e91db98028a3238daf5e15f1a2c6103 diff --git a/lib/os.c b/lib/os.c index 8ba360a..a4142ff 100755 --- a/lib/os.c +++ b/lib/os.c @@ -47,11 +47,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endif #if defined(CYGWIN) -static char seperator = '/'; +char path_seperator = '/'; #elif defined(WIN32) -static char seperator = '\\'; +char path_seperator = '\\'; #else -static char seperator = '/'; +char path_seperator = '/'; #endif #ifdef WIN32 @@ -146,14 +146,14 @@ char* concatPaths(const char*base, const char*add) int l2 = strlen(add); int pos = 0; char*n = 0; - while(l1 && base[l1-1] == seperator) + while(l1 && base[l1-1] == path_seperator) l1--; - while(pos < l2 && add[pos] == seperator) + while(pos < l2 && add[pos] == path_seperator) pos++; n = (char*)malloc(l1 + (l2-pos) + 2); memcpy(n,base,l1); - n[l1]=seperator; + n[l1]=path_seperator; strcpy(&n[l1+1],&add[pos]); return n; } @@ -192,8 +192,8 @@ static char* getTempDir() return dir; } -char* mktempname(char*ptr) { - static char tmpbuf[128]; +char* mktempname(char*ptr, const char*ext) { + static char tmpbuf[160]; char*dir = getTempDir(); int l = strlen(dir); char*sep = ""; @@ -207,20 +207,24 @@ char* mktempname(char*ptr) { #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()); + unsigned int r1 = (unsigned int)lrand48(); + unsigned int r2 = (unsigned int)lrand48(); +#elif HAVE_RAND + unsigned int r1 = rand(); + unsigned int r2 = rand(); #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 + static int count = 1; + unsigned int r1 = time(0); + unsigned int r2 = (unsigned int)tmpbuf<<8^count; + count ++; #endif - return ptr; + if(ext) { + sprintf(ptr, "%s%s%04x%04x.%s",dir,sep,r1,r2,ext); + } else { + sprintf(ptr, "%s%s%04x%04x",dir,sep,r1,r2); + } + return ptr; } memfile_t* memfile_open(const char*path) @@ -269,7 +273,38 @@ void memfile_close(memfile_t*file) #else free(file->data); #endif - file->data = file->len = 0; + file->data = 0; + file->len = 0; free(file); } +void move_file(const char*from, const char*to) +{ + int result = rename(from, to); + + if(result==0) return; //done! + + /* if we can't rename, for some reason, copy the file + manually */ + FILE*fi = fopen(from, "rb"); + if(!fi) { + perror(from); + return; + } + FILE*fo = fopen(to, "wb"); + if(!fo) { + perror(to); + return; + } + char buffer[16384]; + while(1) { + int bytes = fread(buffer, 16384, 1, fi); + if(bytes<=0) + return; + fwrite(buffer, bytes, 1, fo); + } + + fclose(fo); + fclose(fi); +} +