How do I find the 'temp' directory in Linu

2020-01-30 11:51发布

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
5条回答
家丑人穷心不美
2楼-- · 2020-01-30 12:30

Check following variables:

If all fails try to use the directory /tmp.

You can also use tempnam function to generate a unique temporary file name.

查看更多
看我几分像从前
3楼-- · 2020-01-30 12:38

Edit: Fair point from the commenter. tmpnam isn't a good choice these days; use mktemp/mkstemp instead.

Historical answer: Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).

查看更多
聊天终结者
4楼-- · 2020-01-30 12:40

Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp.

查看更多
男人必须洒脱
5楼-- · 2020-01-30 12:43

In standard c, you could try: P_tmpdir

查看更多
小情绪 Triste *
6楼-- · 2020-01-30 12:47

The accepted sequence, specifically from a GNU standpoint, is:

  1. Check the environmental variable TMPDIR (getenv("TMPDIR")) only if the program is not running as SUID/SGID (issetugid() == 0)
  2. Otherwise use P_tmpdir if it is defined and is valid
  3. and finally, should those fail, use _PATH_TMP available from paths.h

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.

查看更多
登录 后发表回答