如何在 Linux 中找到“temp”目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the 'temp' directory in Linux?
提问by Charles K
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 中找到“temp”目录?我正在编写一个平台中立的 C++ 函数,它返回临时目录。在 Mac 和 Windows 中,有一个 API 可以返回这些结果。在 Linux 中,我被难住了。---------------------------
回答by jm.
In standard c, you could try: P_tmpdir
在标准 c 中,您可以尝试: P_tmpdir
回答by Greg Hewgill
Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp
.
使用 $TMPDIR 环境变量的值,如果不存在,请使用/tmp
.
回答by Adam Wright
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 (它将在临时位置为您提供完整的文件名)。
回答by Greg Dan
Check following variables:
检查以下变量:
- The environment variable
TMPDIR
- The value of the
P_tmpdir
macro
If all fails try to use the directory /tmp
.
如果一切都失败,请尝试使用该目录/tmp
。
You can also use tempnam
function to generate a unique temporary file name.
您还可以使用tempnam
函数生成唯一的临时文件名。
回答by Borgboy
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)
- Otherwise use P_tmpdirif it is defined and is valid
- and finally, should those fail, use _PATH_TMPavailable from paths.h
- 仅当程序未作为SUID/SGID运行时才检查环境变量TMPDIR( getenv("TMPDIR")) ( issetugid() == 0)
- 否则,如果P_tmpdir已定义且有效,则使用它
- 最后,如果这些失败,请使用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()。