How do I find the 'temp' directory in Linux? I am writing a platform neutral C++ function that returns the temp directory. In Mac and Windows, there is an API that returns these results. In Linux, I'm stumped.
如何在Linux中找到“临时”目录?我正在编写一个平台中立的c++函数,它返回临时目录。在Mac和Windows中,有一个API返回这些结果。在Linux中,我难住了。
5 个解决方案
#1
28
Check following variables:
检查以下变量:
- The environment variable
TMPDIR
- 环境变量TMPDIR
- The value of the
P_tmpdir
macro - P_tmpdir宏的值
If all fails try to use the directory /tmp
.
如果全部失败,请尝试使用目录/tmp。
You can also use tempnam
function to generate a unique temporary file name.
还可以使用tempnam函数生成惟一的临时文件名。
#2
19
Edit: Fair point from the commenter. tmpnam
isn't a good choice these days; use mktemp
/mkstemp
instead.
编辑:来自评论者的公正观点。tmpnam现在不是一个好选择;使用mktemp / mkstemp代替。
Historical answer: Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).
历史答案:与POSIX兼容,并使用tmpnam(它将在临时位置为您提供完整的文件名)。
#3
11
Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp
.
使用$TMPDIR环境变量的值,如果不存在,则使用/tmp。
#4
9
The accepted sequence, specifically from a GNU standpoint, is:
被接受的序列,特别是从GNU的立场来看,是:
- Check the environmental variable TMPDIR (getenv("TMPDIR")) only if the program is not running as SUID/SGID (issetugid() == 0)
- 仅当程序不是作为SUID/SGID (issetugid() = 0)运行时,检查环境变量TMPDIR (getenv(“TMPDIR”))
- Otherwise use P_tmpdir if it is defined and is valid
- 否则,如果定义了P_tmpdir并有效,则使用P_tmpdir。
- and finally, should those fail, use _PATH_TMP available from paths.h
- 最后,如果失败,使用paths.h中可用的_PATH_TMP
If you are adding an extension or module, check to see if the core provides a function for this purpose. For example, PHP exports php_get_temporary_directory() from main/php_open_temporary_file.h.
如果正在添加扩展或模块,请检查内核是否为此目的提供了一个函数。例如,PHP从main/php_open_temporary_file.h导出php_get_temporary_directory()。
#5
1
In standard c, you could try: P_tmpdir
在标准c中,可以尝试:P_tmpdir。
#1
28
Check following variables:
检查以下变量:
- The environment variable
TMPDIR
- 环境变量TMPDIR
- The value of the
P_tmpdir
macro - P_tmpdir宏的值
If all fails try to use the directory /tmp
.
如果全部失败,请尝试使用目录/tmp。
You can also use tempnam
function to generate a unique temporary file name.
还可以使用tempnam函数生成惟一的临时文件名。
#2
19
Edit: Fair point from the commenter. tmpnam
isn't a good choice these days; use mktemp
/mkstemp
instead.
编辑:来自评论者的公正观点。tmpnam现在不是一个好选择;使用mktemp / mkstemp代替。
Historical answer: Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).
历史答案:与POSIX兼容,并使用tmpnam(它将在临时位置为您提供完整的文件名)。
#3
11
Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp
.
使用$TMPDIR环境变量的值,如果不存在,则使用/tmp。
#4
9
The accepted sequence, specifically from a GNU standpoint, is:
被接受的序列,特别是从GNU的立场来看,是:
- Check the environmental variable TMPDIR (getenv("TMPDIR")) only if the program is not running as SUID/SGID (issetugid() == 0)
- 仅当程序不是作为SUID/SGID (issetugid() = 0)运行时,检查环境变量TMPDIR (getenv(“TMPDIR”))
- Otherwise use P_tmpdir if it is defined and is valid
- 否则,如果定义了P_tmpdir并有效,则使用P_tmpdir。
- and finally, should those fail, use _PATH_TMP available from paths.h
- 最后,如果失败,使用paths.h中可用的_PATH_TMP
If you are adding an extension or module, check to see if the core provides a function for this purpose. For example, PHP exports php_get_temporary_directory() from main/php_open_temporary_file.h.
如果正在添加扩展或模块,请检查内核是否为此目的提供了一个函数。例如,PHP从main/php_open_temporary_file.h导出php_get_temporary_directory()。
#5
1
In standard c, you could try: P_tmpdir
在标准c中,可以尝试:P_tmpdir。