#endif
for (const char* compiler : compilers) {
if (ccbin) {
- char* path = format("%s/%s", ccbin, compiler);
+ std::string path = fmt::format("{}/{}", ccbin, compiler);
auto st = Stat::stat(path);
if (st) {
- hash_compiler(ctx, hash, st, path, false);
+ hash_compiler(ctx, hash, st, path.c_str(), false);
}
- free(path);
} else {
std::string path = find_executable(ctx, compiler, MYNAME);
if (!path.empty()) {
unsigned max_files;
uint64_t max_size;
- char* stats_dir = format("%s/0", config.cache_dir().c_str());
+ std::string stats_dir = fmt::format("{}/0", config.cache_dir());
if (Stat::stat(stats_dir)) {
- stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
+ stats_get_obsolete_limits(stats_dir.c_str(), &max_files, &max_size);
// STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory.
max_files *= 16;
max_size *= 16;
max_files = 0;
max_size = config.max_size();
}
- free(stats_dir);
FILE* f = fopen(config.primary_config_path().c_str(), "w");
if (!f) {
#include "Config.hpp"
#include "Context.hpp"
+#include "Fd.hpp"
#include "SignalHandler.hpp"
#include "Stat.hpp"
#include "Util.hpp"
add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, path);
BOOL ret = FALSE;
if (length > 8192) {
- char* tmp_file = format("%s.tmp", path);
- FILE* fp = create_tmp_file(&tmp_file, "w");
- char atfile[MAX_PATH + 3];
- fwrite(args, 1, length, fp);
- if (ferror(fp)) {
+ auto fd_and_path = Util::create_temp_fd(path);
+ Fd fd(fd_and_path.first);
+ const char* tmp_file = fd_and_path.second.c_str();
+ if (!write_fd(*fd, args, length)) {
cc_log("Error writing @file; this command will probably fail: %s", args);
}
- fclose(fp);
- snprintf(atfile, sizeof(atfile), "\"@%s\"", tmp_file);
- ret = CreateProcess(NULL, atfile, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+ std::string atfile = fmt::format("\"@{}\"", tmp_file);
+ ret = CreateProcess(nullptr,
+ const_cast<char*>(atfile.c_str()),
+ nullptr,
+ nullptr,
+ 1,
+ 0,
+ nullptr,
+ nullptr,
+ &si,
+ &pi);
Util::unlink_tmp(tmp_file);
- free(tmp_file);
}
if (!ret) {
- ret = CreateProcess(
- full_path_win_ext, args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+ ret = CreateProcess(full_path_win_ext,
+ args,
+ nullptr,
+ nullptr,
+ 1,
+ 0,
+ nullptr,
+ nullptr,
+ &si,
+ &pi);
}
if (fd_stdout != -1) {
close(fd_stdout);
#ifdef _WIN32
char namebuf[MAX_PATH];
int ret =
- SearchPath(dir.c_str(), name, NULL, sizeof(namebuf), namebuf, NULL);
+ SearchPath(dir.c_str(), name, nullptr, sizeof(namebuf), namebuf, nullptr);
if (!ret) {
- char* exename = format("%s.exe", name);
- ret =
- SearchPath(dir.c_str(), exename, NULL, sizeof(namebuf), namebuf, NULL);
- free(exename);
+ std::string exename = fmt::format("{}.exe", name);
+ ret = SearchPath(dir.c_str(),
+ exename.c_str(),
+ nullptr,
+ sizeof(namebuf),
+ namebuf,
+ nullptr);
}
(void)exclude_name;
if (ret) {
}
// Add "echo" command.
- bool cmd;
+ bool using_cmd_exe;
+ std::string adjusted_command;
if (str_startswith(command, "echo")) {
- command = format("cmd.exe /c \"%s\"", command);
- cmd = true;
+ adjusted_command = fmt::format("cmd.exe /c \"{}\"", command);
+ using_cmd_exe = true;
} else if (str_startswith(command, "%compiler%")
&& str_eq(compiler, "echo")) {
- command = format("cmd.exe /c \"%s%s\"", compiler, command + 10);
- cmd = true;
+ adjusted_command =
+ fmt::format("cmd.exe /c \"{}{}\"", compiler, command + 10);
+ using_cmd_exe = true;
} else {
- command = x_strdup(command);
- cmd = false;
+ adjusted_command = command;
+ using_cmd_exe = false;
}
+ command = adjusted_command.c_str();
#endif
Args args = Args::from_string(command);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.dwFlags = STARTF_USESTDHANDLES;
- char* win32args;
- if (!cmd) {
+ std::string win32args;
+ if (using_cmd_exe) {
+ win32args = command; // quoted
+ } else {
int length;
const char* prefix = sh.empty() ? nullptr : sh.c_str();
- win32args = win32argvtos(prefix, argv.data(), &length);
- } else {
- win32args = (char*)command; // quoted
+ char* args = win32argvtos(prefix, argv.data(), &length);
+ win32args = args;
+ free(args);
}
- BOOL ret = CreateProcess(
- path.c_str(), win32args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+ BOOL ret = CreateProcess(path.c_str(),
+ const_cast<char*>(win32args.c_str()),
+ nullptr,
+ nullptr,
+ 1,
+ 0,
+ nullptr,
+ nullptr,
+ &si,
+ &pi);
CloseHandle(pipe_out[1]);
- free(win32args);
- if (!cmd) {
- free((char*)command); // Original argument was replaced above.
- }
if (ret == 0) {
return false;
}