tweaks: rename three variables, and reshuffle some lines

master
Benno Schulenberg 2019-10-09 14:20:29 +02:00
parent 56d98052d1
commit 600f81cf98
1 changed files with 18 additions and 19 deletions

View File

@ -1346,29 +1346,28 @@ char *check_writable_directory(const char *path)
return full_path; return full_path;
} }
/* This function calls mkstemp(($TMPDIR|P_tmpdir|/tmp/)"nano.XXXXXX"). /* Create, safely, a temporary file in the standard temp directory.
* On success, it returns the malloc()ed filename and corresponding * On success, return the malloc()ed filename, plus the corresponding
* FILE stream, opened in "r+b" mode. On error, it returns NULL for * file stream opened in read-write mode. On error, return NULL. */
* the filename and leaves the FILE stream unchanged. */ char *safe_tempfile(FILE **stream)
char *safe_tempfile(FILE **f)
{ {
const char *tmpdir_env = getenv("TMPDIR"); const char *env_dir = getenv("TMPDIR");
char *full_tempdir = NULL, *tempfile_name = NULL; char *tempdir = NULL, *tempfile_name = NULL;
mode_t original_umask = 0; mode_t original_umask = 0;
int fd; int fd;
/* Get the absolute path for the first directory among $TMPDIR /* Get the absolute path for the first directory among $TMPDIR
* and P_tmpdir that is writable, otherwise use /tmp/. */ * and P_tmpdir that is writable, otherwise use /tmp/. */
if (tmpdir_env != NULL) if (env_dir != NULL)
full_tempdir = check_writable_directory(tmpdir_env); tempdir = check_writable_directory(env_dir);
if (full_tempdir == NULL) if (tempdir == NULL)
full_tempdir = check_writable_directory(P_tmpdir); tempdir = check_writable_directory(P_tmpdir);
if (full_tempdir == NULL) if (tempdir == NULL)
full_tempdir = mallocstrcpy(NULL, "/tmp/"); tempdir = mallocstrcpy(NULL, "/tmp/");
tempfile_name = charealloc(full_tempdir, strlen(full_tempdir) + 12); tempfile_name = charealloc(tempdir, strlen(tempdir) + 12);
strcat(tempfile_name, "nano.XXXXXX"); strcat(tempfile_name, "nano.XXXXXX");
original_umask = umask(0); original_umask = umask(0);
@ -1376,14 +1375,14 @@ char *safe_tempfile(FILE **f)
fd = mkstemp(tempfile_name); fd = mkstemp(tempfile_name);
if (fd != -1) umask(original_umask);
*f = fdopen(fd, "r+b");
else { if (fd == -1) {
free(tempfile_name); free(tempfile_name);
tempfile_name = NULL; return NULL;
} }
umask(original_umask); *stream = fdopen(fd, "r+b");
return tempfile_name; return tempfile_name;
} }