From 1ae4a04e42b9db848baacfc30e0aa4bc8aa7c974 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Tue, 13 Mar 2018 19:23:28 -0700 Subject: [PATCH] Replace mkstemp with std::tmpfile TMPDIR is not always defined in the environment but the code doesn't check for that. Let's used std::tmpfile instead. Maybe an easier fix is to perform the tmpdir lookup better, but that is also noisy since the search for a decent tmpdir is based on lore. std::tmpfile() is rather clean. However it does not leave a nice filename name around for debugging, so I understand why it might not be preferred. Fixes #495 --- src/readline_callbacks.cc | 41 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/readline_callbacks.cc b/src/readline_callbacks.cc index 12d54ccd..bb5015d6 100644 --- a/src/readline_callbacks.cc +++ b/src/readline_callbacks.cc @@ -439,39 +439,28 @@ void rl_callback(void *dummy, readline_curses *rc) } case LNM_EXEC: { - char fn_template[PATH_MAX]; - auto_mem abspath; - auto_fd fd; - - snprintf(fn_template, sizeof(fn_template), - "/%s/lnav-script-out.XXXXXX", - getenv("TMPDIR")); - if ((fd = mkstemp(fn_template)) == -1) { + auto_mem tmpout(fclose); + + tmpout = std::tmpfile(); + + if (!tmpout) { rc->set_value("Unable to open temporary output file: " + string(strerror(errno))); } - else if ((abspath = realpath(fn_template, NULL)) == NULL) { - rc->set_value("Unable to get real path to temporary file"); - } else { + auto_fd fd(fileno(tmpout)); auto_fd fd_copy((const auto_fd &) fd); char desc[256], timestamp[32]; time_t current_time = time(NULL); string path_and_args = rc->get_value(); - { - auto_mem tmpout(fclose); - - if ((tmpout = fdopen(fd, "w+")) != NULL) { - lnav_data.ld_output_stack.push(tmpout); - string result = execute_file(ec, path_and_args); - string::size_type lf_index = result.find('\n'); - if (lf_index != string::npos) { - result = result.substr(0, lf_index); - } - rc->set_value(result); - lnav_data.ld_output_stack.pop(); - } - } + lnav_data.ld_output_stack.push(tmpout); + string result = execute_file(ec, path_and_args); + string::size_type lf_index = result.find('\n'); + if (lf_index != string::npos) { + result = result.substr(0, lf_index); + } + rc->set_value(result); + lnav_data.ld_output_stack.pop(); struct stat st; @@ -493,8 +482,6 @@ void rl_callback(void *dummy, readline_curses *rc) X, "to close the file")); } } - - remove(abspath.in()); } break; }