Browse Source

Do not hardcode temporary directory

master
Damien Goutte-Gattat 9 years ago
parent
commit
e93236c627
1 changed files with 38 additions and 11 deletions
  1. +38
    -11
      src/fmail.c

+ 38
- 11
src/fmail.c View File

@ -27,6 +27,8 @@
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <gpgme.h>
#include <magic.h>
@ -424,30 +426,55 @@ get_editor(void)
return editor;
}
static int
is_usable_as_tmp_dir(const char *dirname)
{
struct stat st_buf;
if ( stat(dirname, &st_buf) != -1 )
if ( S_ISDIR(st_buf.st_mode) )
if ( access(dirname, R_OK | W_OK) != -1 )
return 0;
return -1;
}
static const char *
get_tmp_dir(void)
{
const char *tmp;
if ( ! ((tmp = getenv("TMPDIR")) && is_usable_as_tmp_dir(tmp)) )
if ( ! ((tmp = getenv("TMP")) && is_usable_as_tmp_dir(tmp)) )
tmp = is_usable_as_tmp_dir("/tmp") ? "/tmp" : ".";
return tmp;
}
static FILE *
read_input_from_editor(void)
{
char tmp_filename[] = "/tmp/fmailXXXXXX";
const char *editor;
char *command;
int tmp_fd;
char *command, *filename;
int fd;
FILE *f;
editor = get_editor();
if ( asprintf(&filename, "%s/fmailXXXXXX", get_tmp_dir()) == -1 )
err(EXIT_FAILURE, "cannot create temporary file");
if ( (tmp_fd = mkstemp(tmp_filename)) == -1 )
if ( (fd = mkstemp(filename)) == -1 )
err(EXIT_FAILURE, "cannot create temporary file");
command = xmalloc(strlen(editor) + sizeof(tmp_filename) + 1);
sprintf(command, "%s %s", editor, tmp_filename);
if ( asprintf(&command, "%s %s", get_editor(), filename) == -1 )
err(EXIT_FAILURE, "cannot execute editor");
if ( system(command) == -1 )
err(EXIT_FAILURE, "cannot execute editor '%s'", editor);
err(EXIT_FAILURE, "cannot execute editor");
if ( (f = fdopen(tmp_fd, "r")) == NULL )
if ( (f = fdopen(fd, "r")) == NULL )
err(EXIT_FAILURE, "cannot open temporary file");
unlink(tmp_filename);
unlink(filename);
free(filename);
free(command);
return f;


Loading…
Cancel
Save