12 #include <util/pragma_push.def>
14 #pragma warning(disable:4668)
16 #pragma warning(disable : 5039)
24 #define getpid _getpid
27 #include <util/pragma_pop.def>
39 #if defined(__linux__) || \
40 defined(__FreeBSD_kernel__) || \
42 defined(__unix__) || \
43 defined(__CYGWIN__) || \
51 #define mkstemps my_mkstemps
52 int my_mkstemps(
char *template_str,
int suffix_len)
56 std::size_t template_length=strlen(template_str);
61 if(
static_cast<std::size_t
>(suffix_len+6)>template_length)
65 template_str+template_length-6-suffix_len;
67 if(strncmp(XXXXXX_pos,
"XXXXXX", 6)!=0)
70 static const char letters_and_numbers[]=
71 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
73 static long long unsigned int random_state;
74 random_state+=getpid()+123;
76 for(
unsigned attempt = 0; attempt < 1000; ++attempt)
78 unsigned long long number=random_state;
80 for(
unsigned i=0; i<6; i++)
82 XXXXXX_pos[i]=letters_and_numbers[number%62];
86 int fd=open(template_str, O_RDWR|O_CREAT|O_EXCL, 0600);
90 random_state+=4321+getpid();
99 const std::string &prefix,
100 const std::string &suffix)
103 char lpTempPathBuffer[MAX_PATH];
111 if(dwRetVal>MAX_PATH || (dwRetVal==0))
115 std::string t_template=
116 std::string(lpTempPathBuffer)+prefix+
119 std::string dir=
"/tmp/";
120 const char *TMPDIR_env=getenv(
"TMPDIR");
121 if(TMPDIR_env!=
nullptr)
123 if(*dir.rbegin()!=
'/')
126 std::string t_template=
130 char *t_ptr=strdup(t_template.c_str());
132 int fd=mkstemps(t_ptr, suffix.size());
139 std::string result=std::string(t_ptr);