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.
master
Benno Schulenberg 2021-08-20 12:25:04 +02:00
parent 083bbae0e4
commit f429ebe34d
1 changed files with 10 additions and 3 deletions

View File

@ -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;