mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-28 13:53:10 +00:00
Create function to retrieve tmpdir
`/tmp` isn't present / writeable on every system. Instead of always using `/tmp`, try to use standard environment variables and configuration to find a temporary directory. Adapted from #3974, with updates based on those comments. Closes #3845.
This commit is contained in:
parent
7f1dfb6284
commit
6025c28efc
3 changed files with 31 additions and 1 deletions
|
@ -7,6 +7,7 @@
|
|||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <paths.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
@ -2442,3 +2443,25 @@ std::string get_executable_path(const char *argv0) {
|
|||
return std::string(argv0 ? argv0 : "");
|
||||
}
|
||||
|
||||
/// Return a path to a directory where we can store temporary files.
|
||||
std::string get_path_to_tmp_dir() {
|
||||
char *env_tmpdir = getenv("TMPDIR");
|
||||
if (env_tmpdir) {
|
||||
return env_tmpdir;
|
||||
}
|
||||
#if defined(_CS_DARWIN_USER_TEMP_DIR)
|
||||
char osx_tmpdir[PATH_MAX];
|
||||
size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, osx_tmpdir, PATH_MAX);
|
||||
if (0 < n && n <= PATH_MAX) {
|
||||
return osx_tmpdir;
|
||||
} else {
|
||||
return "/tmp";
|
||||
}
|
||||
#elif defined(P_tmpdir)
|
||||
return P_tmpdir;
|
||||
#elif defined(_PATH_TMP)
|
||||
return _PATH_TMP;
|
||||
#else
|
||||
return "/tmp";
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -19,6 +19,11 @@
|
|||
#include "fallback.h" // IWYU pragma: keep
|
||||
#include "maybe.h"
|
||||
|
||||
// PATH_MAX may not exist.
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 4096
|
||||
#endif
|
||||
|
||||
// Define a symbol we can use elsewhere in our code to determine if we're being built on MS Windows
|
||||
// under Cygwin.
|
||||
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(__CYGWIN__) || \
|
||||
|
@ -947,6 +952,8 @@ static const wchar_t *enum_to_str(T enum_val, const enum_map<T> map[]) {
|
|||
|
||||
void redirect_tty_output();
|
||||
|
||||
std::string get_path_to_tmp_dir();
|
||||
|
||||
// Minimum allowed terminal size and default size if the detected size is not reasonable.
|
||||
#define MIN_TERM_COL 20
|
||||
#define MIN_TERM_ROW 2
|
||||
|
|
|
@ -1741,7 +1741,7 @@ wcstring env_get_runtime_path() {
|
|||
auto pwuid = getpwuid(geteuid());
|
||||
const char *uname = pwuid ? pwuid->pw_name : NULL;
|
||||
// /tmp/fish.user
|
||||
std::string tmpdir = "/tmp/fish.";
|
||||
std::string tmpdir = get_path_to_tmp_dir() + "/fish.";
|
||||
if (uname) {
|
||||
tmpdir.append(uname);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue