poppler: embed mktmpname() from the xpdf changes patch
[swftools.git] / lib / pdf / popplercompat.cc
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "popplercompat.h"
5
6 #ifdef HAVE_POPPLER
7 static char* getTempDir()
8 {
9 #ifdef WIN32
10     char*dir = getenv("TMP");
11     if(!dir) dir = getenv("TEMP");
12     if(!dir) dir = getenv("tmp");
13     if(!dir) dir = getenv("temp");
14     if(!dir) dir = "C:\\";
15 #else
16     char* dir = "/tmp/";
17 #endif
18     return dir;
19 }
20
21 char* mktmpname(char*ptr) {
22     static char tmpbuf[128];
23     char*dir = getTempDir();
24     int l = strlen(dir);
25     char*sep = "";
26     if(!ptr)
27         ptr = tmpbuf;
28     if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
29 #ifdef WIN32
30         sep = "\\";
31 #else
32         sep = "/";
33 #endif
34     }
35
36  //   used to be mktemp. This does remove the warnings, but
37  //   It's not exactly an improvement.
38 #ifdef HAVE_LRAND48
39     sprintf(ptr, "%s%s%08x%08x",dir,sep,(unsigned int)lrand48(),(unsigned int)lrand48());
40 #else
41 #   ifdef HAVE_RAND
42         sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
43 #   else
44         static int count = 1;
45         sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
46         count ++;
47 #   endif
48 #endif
49      return ptr;
50 }
51
52 #endif