我必须使用哪种 gtk 方法来获取 Ubuntu 中的临时路径?

发布于 2024-10-13 12:13:37 字数 26 浏览 5 评论 0原文

如何获取Ubuntu中的临时目录路径?

How can I get the temporary directory path in Ubuntu?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾听心声的旋律 2024-10-20 12:13:37

在大多数类 Unix 系统上,您需要查找 /tmp。如果这不是您想要的答案,您应该指定您正在谈论的 Ubuntu 的部分。

某些应用程序允许您指定临时文件的放置位置(例如使用 TMPTEMPTMPDIR 环境变量),但很多如果 /tmp 不存在,很多东西都会在 UNIX 下崩溃,所以使用它是安全的。如果您想使其可配置,在您的代码中,您可以在以下完整程序中使用类似函数 getTmpDir() 的内容:

#include <stdio.h>
#include <stdlib.h>

const char *getTmpDir (void) {
    char *tmpdir;

    if ((tmpdir = getenv ("TEMP")) != NULL)   return tmpdir;
    if ((tmpdir = getenv ("TMP")) != NULL)    return tmpdir;
    if ((tmpdir = getenv ("TMPDIR")) != NULL) return tmpdir;

    return "/tmp";
}

int main(void) {
    const char *xyzzy = getTmpDir();
    printf ("Temporary directory =  %s\n", xyzzy);
    return 0;
}

在我的 CygWin 环境中输出(我有 TEMPTMP 设置为此值):

Temporary directory =  /cygdrive/c/Users/Pax/AppData/Local/Temp

这几乎就是 GLib g_get_tmp_dir() 调用确实如此,尽管可能以不同的顺序。

当然,如果您想使用特定于应用程序的环境变量,您可以将其放在其他变量之前:

const char *getTmpDir (void) {
    char *tmpdir;

    if ((tmpdir = getenv ("XYZZY_TMP")) != NULL)   return tmpdir;
    if ((tmpdir = getenv ("TEMP")) != NULL)        return tmpdir;
    if ((tmpdir = getenv ("TMP")) != NULL)         return tmpdir;
    if ((tmpdir = getenv ("TMPDIR")) != NULL)      return tmpdir;

    return "/tmp";
}

或者甚至删除部分或全部“标准”变量。但如果用户没有配置任何内容,您几乎应该总是依靠/tmp

On most Unix-like systems, you'd be looking for /tmp. If that's not quite the answer you were after, you should specify which bit of Ubuntu you're talking about.

Certain applications will allow you to specify where their temporary files are put (such as with the TMP, TEMP or TMPDIR environment variables) but a lot of stuff would break under UNIX if /tmp didn't exist, so it's safe just to use that. If you want to make it configurable, in your code, you'd use something like the function getTmpDir() in the following complete program:

#include <stdio.h>
#include <stdlib.h>

const char *getTmpDir (void) {
    char *tmpdir;

    if ((tmpdir = getenv ("TEMP")) != NULL)   return tmpdir;
    if ((tmpdir = getenv ("TMP")) != NULL)    return tmpdir;
    if ((tmpdir = getenv ("TMPDIR")) != NULL) return tmpdir;

    return "/tmp";
}

int main(void) {
    const char *xyzzy = getTmpDir();
    printf ("Temporary directory =  %s\n", xyzzy);
    return 0;
}

which outputs, in my CygWin environment (I have both TEMP and TMP set to this value):

Temporary directory =  /cygdrive/c/Users/Pax/AppData/Local/Temp

That's pretty much what the GLib g_get_tmp_dir() call does, though possibly in a different order.

Of course, if you wanted to use an application-specific environment variable, you could put that before the others thus:

const char *getTmpDir (void) {
    char *tmpdir;

    if ((tmpdir = getenv ("XYZZY_TMP")) != NULL)   return tmpdir;
    if ((tmpdir = getenv ("TEMP")) != NULL)        return tmpdir;
    if ((tmpdir = getenv ("TMP")) != NULL)         return tmpdir;
    if ((tmpdir = getenv ("TMPDIR")) != NULL)      return tmpdir;

    return "/tmp";
}

Or even take out some or all of the "standard" ones. But you should pretty much always fall back on /tmp if the user hasn't configured anything.

暖心男生 2024-10-20 12:13:37

我查看了 Python 是如何做到这一点的 ,并且似乎没有任何特定的 UNIX 接口可以执行此操作。

他们只是按顺序尝试:

  1. 环境变量 TMPDIRTEMPTMP
  2. /tmp
  3. / var/tmp
  4. /usr/tmp

鉴于 Python 是由比我聪明得多的人编写的,我愿意打赌这可能是您能做的最好的事情。

I looked at how Python does this, and there doesn't seem to be any specific UNIX interface to do so.

They just try, in sequence:

  1. The environment variables TMPDIR, TEMP, and TMP
  2. /tmp
  3. /var/tmp
  4. /usr/tmp

Given that Python was written by people a lot smarter than I am, I'd be willing to bet this is probably the best you can do.

萌面超妹 2024-10-20 12:13:37

Flocks,

感谢您抽出时间,但我期待的是来自 gnome 链接。

http://library.gnome.org/devel/glib /unstable/glib-Miscellaneous-Utility-Functions.html

使用 API g_get_tmp_dir() 我们可以获得临时目录的位置

Flocks,

Thanks for taking your time but what i am expecting is from the gnome link.

http://library.gnome.org/devel/glib/unstable/glib-Miscellaneous-Utility-Functions.html

using the API g_get_tmp_dir() we can have the location of temp directory

甜宝宝 2024-10-20 12:13:37

有一个环境变量 TMPDIR 可以设置临时目录的位置,大多数程序都尊重这一点,如果未设置它将默认为 /tmp (或 /var/tmp)

There is an environment variable TMPDIR which can set the location of the temporary dir, most programs respect this, if it's not set it will default to /tmp (or /var/tmp)

阳光下的泡沫是彩色的 2024-10-20 12:13:37

从命令行:

$ tempfile | xargs dirname
/tmp
$ TMPDIR="/mnt/tmp" tempfile | xargs dirname
/mnt/tmp

From the command line:

$ tempfile | xargs dirname
/tmp
$ TMPDIR="/mnt/tmp" tempfile | xargs dirname
/mnt/tmp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文