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>
38 #if defined(__linux__) || \
39 defined(__FreeBSD_kernel__) || \
41 defined(__unix__) || \
42 defined(__CYGWIN__) || \
50 #define mkstemps my_mkstemps
51 int my_mkstemps(
char *template_str,
int suffix_len)
55 std::size_t template_length=strlen(template_str);
60 if(
static_cast<std::size_t
>(suffix_len+6)>template_length)
64 template_str+template_length-6-suffix_len;
66 if(strncmp(XXXXXX_pos,
"XXXXXX", 6)!=0)
69 static const char letters_and_numbers[]=
70 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
72 static long long unsigned int random_state;
73 random_state+=getpid()+123;
75 for(
unsigned attempt = 0; attempt < 1000; ++attempt)
77 unsigned long long number=random_state;
79 for(
unsigned i=0; i<6; i++)
81 XXXXXX_pos[i]=letters_and_numbers[number%62];
85 int fd=open(template_str, O_RDWR|O_CREAT|O_EXCL, 0600);
89 random_state+=4321+getpid();
98 const std::string &prefix,
99 const std::string &suffix)
102 char lpTempPathBuffer[MAX_PATH];
110 if(dwRetVal>MAX_PATH || (dwRetVal==0))
114 std::string t_template=
115 std::string(lpTempPathBuffer)+prefix+
118 std::string dir=
"/tmp/";
119 const char *TMPDIR_env=getenv(
"TMPDIR");
120 if(TMPDIR_env!=
nullptr)
122 if(*dir.rbegin()!=
'/')
125 std::string t_template=
129 char *t_ptr=strdup(t_template.c_str());
131 int fd=mkstemps(t_ptr, suffix.size());
138 std::string result=std::string(t_ptr);
146 std::remove(
name.c_str());