17 #define getpid _getpid 27 #if defined(__linux__) || \ 28 defined(__FreeBSD_kernel__) || \ 30 defined(__unix__) || \ 31 defined(__CYGWIN__) || \ 40 #define mkstemps my_mkstemps 41 int my_mkstemps(
char *template_str,
int suffix_len)
45 std::size_t template_length=strlen(template_str);
50 if(static_cast<std::size_t>(suffix_len+6)>template_length)
54 template_str+template_length-6-suffix_len;
56 if(strncmp(XXXXXX_pos,
"XXXXXX", 6)!=0)
59 static const char letters_and_numbers[]=
60 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
62 static long long unsigned int random_state;
63 random_state+=getpid()+123;
65 for(
unsigned attempt=0; ; ++attempt)
67 unsigned long long number=random_state;
69 for(
unsigned i=0; i<6; i++)
71 XXXXXX_pos[i]=letters_and_numbers[number%62];
75 int fd=open(template_str, O_RDWR|O_CREAT|O_EXCL, 0600);
79 random_state+=4321+getpid();
88 const std::string &prefix,
89 const std::string &suffix)
92 char lpTempPathBuffer[MAX_PATH];
100 if(dwRetVal>MAX_PATH || (dwRetVal==0))
101 throw "GetTempPath failed";
104 std::string t_template=
105 std::string(lpTempPathBuffer)+prefix+
108 std::string dir=
"/tmp/";
109 const char *TMPDIR_env=getenv(
"TMPDIR");
110 if(TMPDIR_env!=
nullptr)
112 if(*dir.rbegin()!=
'/')
115 std::string t_template=
119 char *t_ptr=strdup(t_template.c_str());
121 int fd=mkstemps(t_ptr, suffix.size());
124 throw "mkstemps failed";
128 std::string result=std::string(t_ptr);
136 unlink(
name.c_str());
std::string get_temporary_file(const std::string &prefix, const std::string &suffix)
Substitute for mkstemps (OpenBSD standard) for Windows, where it is unavailable.
std::string to_string(const string_constraintt &expr)
Used for debug printing.