From 485b40ac2a6fcc06900f247bfb32b9818537affb Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Wed, 11 Sep 2002 04:58:24 +0000 Subject: [PATCH] * utilunix.c (mc_tmpdir): New function that returns temporary directory for mc. * util.c (mc_mkstemps): Use mc_tmpdir(). * main.c (main): Call mc_tmpdir(). --- src/ChangeLog | 7 +++++++ src/main.c | 5 ++++- src/util.c | 9 +-------- src/util.h | 1 + src/utilunix.c | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f9581bad1..a90b8c6bc 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2002-09-11 Pavel Roskin + + * utilunix.c (mc_tmpdir): New function that returns temporary + directory for mc. + * util.c (mc_mkstemps): Use mc_tmpdir(). + * main.c (main): Call mc_tmpdir(). + 2002-09-10 Pavel Roskin * util.c (trim): Handle short strings even better - don't use diff --git a/src/main.c b/src/main.c index f27b38e90..a43e154d5 100644 --- a/src/main.c +++ b/src/main.c @@ -2449,7 +2449,10 @@ main (int argc, char *argv []) /* Initialize list of all user group for timur_clr_mode */ init_groups (); - + + /* Set up temporary directory */ + mc_tmpdir (); + OS_Setup (); /* This variable is used by the subshell */ diff --git a/src/util.c b/src/util.c index b9b8d5868..c45a41de4 100644 --- a/src/util.c +++ b/src/util.c @@ -1205,15 +1205,8 @@ int mc_mkstemps(char **pname, const char *prefix, const char *suffix) int count; if (strchr(prefix, PATH_SEP) == NULL) { - char *tmpdir; - - tmpdir = getenv("TMPDIR"); - if (!tmpdir) { - tmpdir = TMPDIR_DEFAULT; - } - /* Add prefix first to find the position of XXXXXX */ - tmpbase = concat_dir_and_file (tmpdir, prefix); + tmpbase = concat_dir_and_file (mc_tmpdir (), prefix); } else { tmpbase = g_strdup (prefix); } diff --git a/src/util.h b/src/util.h index cced4380e..5594e5270 100644 --- a/src/util.h +++ b/src/util.h @@ -88,6 +88,7 @@ char *canonicalize_pathname (char *); /* Misc Unix functions */ char *get_current_wd (char *buffer, int size); +const char *mc_tmpdir (void); int my_mkdir (char *s, mode_t mode); int my_rmdir (char *s); diff --git a/src/utilunix.c b/src/utilunix.c index 6602de79d..14fc9ab7a 100644 --- a/src/utilunix.c +++ b/src/utilunix.c @@ -298,6 +298,44 @@ char *tilde_expand (const char *directory) } +/* + * Return the directory where mc should keep its temporary files. + * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}-$USER" + * When called the first time, the directory is created if needed. + * The first call should be done early, since we are using fprintf() + * and not message() to report possible problems. + */ +const char * +mc_tmpdir (void) +{ + static char tmpdir[64]; + const char *sys_tmp; + struct passwd *pwd; + + if (*tmpdir) + return tmpdir; + + sys_tmp = getenv ("TMPDIR"); + if (!sys_tmp) { + sys_tmp = TMPDIR_DEFAULT; + } + + pwd = getpwuid (getuid ()); + g_snprintf (tmpdir, sizeof (tmpdir), "%s/mc-%s", sys_tmp, + pwd->pw_name); + canonicalize_pathname (tmpdir); + + /* No recursion or VFS here. If $TMPDIR is missing, exit. */ + if (mkdir (tmpdir, 0700) != 0 && errno != EEXIST) { + fprintf (stderr, _("Cannot create temporary directory %s: %s\n"), + tmpdir, unix_error_string (errno)); + exit (1); + } + + return tmpdir; +} + + /* Pipes are guaranteed to be able to hold at least 4096 bytes */ /* More than that would be unportable */ #define MAX_PIPE_SIZE 4096