Which gtk method I have to use to get temporary path in Ubuntu?
Asked Answered
L

5

6

How can I get the temporary directory path in Ubuntu?

Loom answered 25/1, 2011 at 5:59 Comment(0)
L
3

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

Loom answered 25/1, 2011 at 11:2 Comment(0)
L
5

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.

Lilienthal answered 25/1, 2011 at 6:6 Comment(2)
ya that's what i am expecting. I want to get that path programmatically using gtk or glib which ever feasible.Loom
Err, you don't have to get it programatically. It's /tmp. It's (sadistic administrators notwithstanding) always /tmp. If you're coding in C: char *tmpdir = "/tmp"; will do it just fine :-) You can affect certain applications (like the TMPDIR variable in bash) but a lot of things will break if /tmp doesn't exist.Lilienthal
A
3

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.

Anther answered 25/1, 2011 at 6:31 Comment(0)
L
3

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

Loom answered 25/1, 2011 at 11:2 Comment(0)
W
0

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)

Winegrower answered 25/1, 2011 at 6:27 Comment(0)
C
0

From the command line:

$ tempfile | xargs dirname
/tmp
$ TMPDIR="/mnt/tmp" tempfile | xargs dirname
/mnt/tmp
Chrysalid answered 17/8, 2012 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.