12 #include <util/pragma_push.def>
14 #pragma warning(disable:4668)
22 #define getpid _getpid
25 #include <util/pragma_pop.def>
36 #if defined(__linux__) || \
37 defined(__FreeBSD_kernel__) || \
39 defined(__unix__) || \
40 defined(__CYGWIN__) || \
48 #define mkstemps my_mkstemps
49 int my_mkstemps(
char *template_str,
int suffix_len)
53 std::size_t template_length=strlen(template_str);
58 if(static_cast<std::size_t>(suffix_len+6)>template_length)
62 template_str+template_length-6-suffix_len;
64 if(strncmp(XXXXXX_pos,
"XXXXXX", 6)!=0)
67 static const char letters_and_numbers[]=
68 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70 static long long unsigned int random_state;
71 random_state+=getpid()+123;
73 for(
unsigned attempt = 0; attempt < 1000; ++attempt)
75 unsigned long long number=random_state;
77 for(
unsigned i=0; i<6; i++)
79 XXXXXX_pos[i]=letters_and_numbers[number%62];
83 int fd=open(template_str, O_RDWR|O_CREAT|O_EXCL, 0600);
87 random_state+=4321+getpid();
96 const std::string &prefix,
97 const std::string &suffix)
100 char lpTempPathBuffer[MAX_PATH];
108 if(dwRetVal>MAX_PATH || (dwRetVal==0))
112 std::string t_template=
113 std::string(lpTempPathBuffer)+prefix+
116 std::string dir=
"/tmp/";
117 const char *TMPDIR_env=getenv(
"TMPDIR");
118 if(TMPDIR_env!=
nullptr)
120 if(*dir.rbegin()!=
'/')
123 std::string t_template=
127 char *t_ptr=strdup(t_template.c_str());
129 int fd=mkstemps(t_ptr, suffix.size());
136 std::string result=std::string(t_ptr);
144 std::remove(
name.c_str());