Skip to content

Commit

Permalink
Stop using std::tmpnam in POSIX operating systems
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Aug 10, 2020
1 parent 80e4f9b commit 594dca6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
20 changes: 3 additions & 17 deletions include/Sand/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,9 @@

namespace Sand::Helpers
{
bool ends_with(const std::string &str, const std::string &ending)
{
if (str.length() >= ending.length())
{
return str.compare(str.length() - ending.length(), ending.length(), ending) == 0;
}

return false;
}
bool ends_with(const std::string &str, const std::string &ending);

bool starts_with(const std::string &str, const std::string &starting)
{
if (str.length() >= starting.length())
{
return str.rfind(starting, 0) == 0;
}
bool starts_with(const std::string &str, const std::string &starting);

return false;
}
std::string temporary_filename();
} // namespace Sand::Helpers
4 changes: 3 additions & 1 deletion src/Compiler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <Sand/Compiler.hpp>

#include <Sand/Helpers.hpp>

#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/PassManager.h>
#include <llvm/Passes/PassBuilder.h>
Expand Down Expand Up @@ -28,7 +30,7 @@ std::vector<std::string> Sand::Compiler::generate_objects(const std::string &os,
std::cout << "Target triple: " << this->module->getTargetTriple() << std::endl;
}

std::string output_path = std::tmpnam(nullptr);
auto output_path = Helpers::temporary_filename();
std::error_code error_code;
llvm::raw_fd_ostream dest(output_path, error_code, llvm::sys::fs::OF_None);

Expand Down
40 changes: 40 additions & 0 deletions src/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <Sand/Helpers.hpp>

#include <Sand/filesystem.hpp>

#include <cstdio>
#include <cstdlib>

using namespace Sand;

bool Helpers::ends_with(const std::string &str, const std::string &ending)
{
if (str.length() >= ending.length())
{
return str.compare(str.length() - ending.length(), ending.length(), ending) == 0;
}

return false;
}

bool Helpers::starts_with(const std::string &str, const std::string &starting)
{
if (str.length() >= starting.length())
{
return str.rfind(starting, 0) == 0;
}

return false;
}

std::string Helpers::temporary_filename()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
return std::tmpnam(nullptr);
#else
auto filename = fs::temp_directory_path().u8string() + "XXXXXXXXXXXXXXXX";
mkstemp(filename.data());

return filename;
#endif
}
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <Sand/Debugger.hpp>
#include <Sand/Linker.hpp>

#include <Sand/Helpers.hpp>

#include <Sand/Exceptions/CompilationException.hpp>
#include <Sand/Exceptions/TargetLookupFailedException.hpp>

Expand Down Expand Up @@ -297,7 +299,7 @@ int main(int argc, char **argv)
run->add_flag("--verbose", options.verbose, "Verbose mode");

run->callback([&]() {
options.output_file = std::tmpnam(nullptr);
options.output_file = Sand::Helpers::temporary_filename();

if (!compile(options, debug))
{
Expand Down

0 comments on commit 594dca6

Please sign in to comment.