commit f429ebe34d95a682d5ea8f2f3f5ac5812f0fd6c1
parent 083bbae0e4416e9c3891ecc87c755c50bc612b8d
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Fri, 20 Aug 2021 12:25:04 +0200
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.
Diffstat:
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git 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);
+ extension = strrchr(openfile->filename, '.');
+
+ 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 = mkstemp(tempfile_name);
+ descriptor = mkstemps(tempfile_name, strlen(extension));
*stream = (descriptor > 0) ? fdopen(descriptor, "r+b") : NULL;