Skip to content

Commit

Permalink
Remove uses of GNUism get_current_dir_name
Browse files Browse the repository at this point in the history
This function is part of glibc but is not a POSIX standard function.
Plain old `getcwd` is portable. Fortunately, most of the uses are
copypasta that feed directly into a C++ string construction so this was
easy to replace.
  • Loading branch information
sampsyo committed Apr 12, 2023
1 parent 8b46f48 commit ef57bea
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/algorithms/temp_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ std::string get_dir() {

// Get the default temp dir from environment variables.
if (temp_dir.empty()) {
char* cwd = get_current_dir_name();
char cwd[512];
getcwd(cwd, sizeof(cwd));
temp_dir = std::string(cwd);
free(cwd);
/*const char* system_temp_dir = nullptr;
for(const char* var_name : {"TMPDIR", "TMP", "TEMP", "TEMPDIR", "USERPROFILE"}) {
if (system_temp_dir == nullptr) {
Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/xp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,19 +776,19 @@ namespace xp {

// Get the default temp dir from environment variables.
if (temp_dir.empty()) {
char* cwd = get_current_dir_name();
char cwd[512];
getcwd(cwd, sizeof(cwd));
temp_dir = std::string(cwd);
free(cwd);
/*const char *system_temp_dir = nullptr;
for (const char *var_name : {"TMPDIR", "TMP", "TEMP", "TEMPDIR", "USERPROFILE"}) {
if (system_temp_dir == nullptr) {
system_temp_dir = getenv(var_name);
}
}
if (system_temp_dir == nullptr) {
char* cwd = get_current_dir_name();
char cwd[512];
getcwd(cwd, sizeof(cwd));
temp_dir = std::string(cwd);
free(cwd);
} else {
temp_dir = system_temp_dir;
}*/
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/layout_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ int main_layout(int argc, char **argv) {
if (tmp_base) {
xp::temp_file::set_dir(args::get(tmp_base));
} else {
char* cwd = get_current_dir_name();
char cwd[512];
getcwd(cwd, sizeof(cwd));
xp::temp_file::set_dir(std::string(cwd));
free(cwd);
}

if (!graph.is_optimized()) {
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/sort_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ int main_sort(int argc, char** argv) {
if (tmp_base) {
xp::temp_file::set_dir(args::get(tmp_base));
} else {
char* cwd = get_current_dir_name();
char cwd[512];
getcwd(cwd, sizeof(cwd));
xp::temp_file::set_dir(std::string(cwd));
free(cwd);
}

// If required, first of all, optimize the graph so that it is optimized for subsequent algorithms (if required)
Expand Down

0 comments on commit ef57bea

Please sign in to comment.