From f429ebe34d95a682d5ea8f2f3f5ac5812f0fd6c1 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 20 Aug 2021 12:25:04 +0200 Subject: [PATCH] files: add the original file's suffix to the name of a temporary file This allows 'aspell' to recognize a C file and thus spell check only the comments and strings. This fulfills https://savannah.gnu.org/bugs/?61056. --- src/files.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/files.c b/src/files.c index 3175d980..a974e65c 100644 --- a/src/files.c +++ b/src/files.c @@ -1451,6 +1451,7 @@ char *safe_tempfile(FILE **stream) { const char *env_dir = getenv("TMPDIR"); char *tempdir = NULL, *tempfile_name = NULL; + char *extension; int descriptor; /* Get the absolute path for the first directory among $TMPDIR @@ -1464,10 +1465,16 @@ char *safe_tempfile(FILE **stream) if (tempdir == NULL) tempdir = copy_of("/tmp/"); - tempfile_name = nrealloc(tempdir, strlen(tempdir) + 12); - strcat(tempfile_name, "nano.XXXXXX"); + extension = strrchr(openfile->filename, '.'); - descriptor = mkstemp(tempfile_name); + if (!extension) + extension = openfile->filename + strlen(openfile->filename); + + tempfile_name = nrealloc(tempdir, strlen(tempdir) + 12 + strlen(extension)); + strcat(tempfile_name, "nano.XXXXXX"); + strcat(tempfile_name, extension); + + descriptor = mkstemps(tempfile_name, strlen(extension)); *stream = (descriptor > 0) ? fdopen(descriptor, "r+b") : NULL;