14#include <util/pragma_push.def>
16#pragma warning(disable:4668)
18#pragma warning(disable : 5039)
30#include <util/pragma_pop.def>
40#if defined(__linux__) || \
41 defined(__FreeBSD_kernel__) || \
43 defined(__unix__) || \
44 defined(__CYGWIN__) || \
52#define mkstemps my_mkstemps
53int my_mkstemps(
char *template_str,
int suffix_len)
57 std::size_t template_length=strlen(template_str);
62 if(
static_cast<std::size_t
>(suffix_len+6)>template_length)
66 template_str+template_length-6-suffix_len;
68 if(strncmp(XXXXXX_pos,
"XXXXXX", 6)!=0)
71 static const char letters_and_numbers[]=
72 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
74 static long long unsigned int random_state;
75 random_state+=getpid()+123;
77 for(
unsigned attempt = 0; attempt < 1000; ++attempt)
79 unsigned long long number=random_state;
81 for(
unsigned i=0; i<6; i++)
83 XXXXXX_pos[i]=letters_and_numbers[number%62];
87 int fd=open(template_str, O_RDWR|O_CREAT|O_EXCL, 0600);
91 random_state+=4321+getpid();
100 const std::string &prefix,
101 const std::string &suffix)
104 char lpTempPathBuffer[MAX_PATH];
112 if(dwRetVal>MAX_PATH || (dwRetVal==0))
116 std::string t_template=
117 std::string(lpTempPathBuffer)+prefix+
118 std::to_string(getpid())+
".XXXXXX"+suffix;
120 std::string dir=
"/tmp/";
121 const char *TMPDIR_env=getenv(
"TMPDIR");
122 if(TMPDIR_env!=
nullptr)
124 if(*dir.rbegin()!=
'/')
127 std::string t_template=
128 dir+prefix+std::to_string(getpid())+
".XXXXXX"+suffix;
131 char *t_ptr=strdup(t_template.c_str());
133 int fd=mkstemps(t_ptr, suffix.size());
140 std::string result=std::string(t_ptr);
148 std::filesystem::remove(
name);
Thrown when some external system fails unexpectedly.
std::string get_temporary_file(const std::string &prefix, const std::string &suffix)
Substitute for mkstemps (OpenBSD standard) for Windows, where it is unavailable.