From 3a7e625b3a8b72b9cda8091bc0d8cdc98c8ce561 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sun, 31 May 2026 12:59:19 +0100 Subject: [PATCH] Cleanup temp files on exit --- cc.c | 2 ++ lib.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib.h | 2 ++ 3 files changed, 65 insertions(+) diff --git a/cc.c b/cc.c index c8978fe..74312ad 100755 --- a/cc.c +++ b/cc.c @@ -30,6 +30,8 @@ static void cleanup (void) { } } + + cleanup_tempfiles (); } diff --git a/lib.c b/lib.c index bfa7a40..36ed9ca 100755 --- a/lib.c +++ b/lib.c @@ -667,6 +667,7 @@ int scc_close (FILE *fp) { return fclose (fp); } +void cleanup_tempfiles (void) {}; #elif defined (_WIN32) # include @@ -855,6 +856,36 @@ int scc_close (FILE *fp) { return 0; +} + +void cleanup_tempfiles (void) { + + struct hashtab_entry *entry; + struct temp_file *tmp; + + int ret, i; + + for (i = 0; i < hashtab_tmpfiles.capacity; i++) { + + if (!(entry = &hashtab_tmpfiles.entries[i])) { + continue; + } + + if (!entry->key || !entry->value) { + continue; + } + + tmp = (struct temp_file *) entry->value; + + if ((ret = fclose (tmp->fp))) { + continue; + } + + remove (tmp->path); + hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name)); + + } + } #else # include "hashtab.h" @@ -982,6 +1013,36 @@ int scc_close (FILE *fp) { return 0; +} + +void cleanup_tempfiles (void) { + + struct hashtab_entry *entry; + struct temp_file *tmp; + + int ret, i; + + for (i = 0; i < hashtab_tmpfiles.capacity; i++) { + + if (!(entry = &hashtab_tmpfiles.entries[i])) { + continue; + } + + if (!entry->key || !entry->value) { + continue; + } + + tmp = (struct temp_file *) entry->value; + + if ((ret = fclose (tmp->fp))) { + continue; + } + + remove (tmp->name); + hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name)); + + } + } #endif diff --git a/lib.h b/lib.h index afaf50c..d576194 100755 --- a/lib.h +++ b/lib.h @@ -55,4 +55,6 @@ int is_pragma_igored (const char *filename); FILE *scc_tmpfile (void); int scc_close (FILE *fp); +void cleanup_tempfiles (void); + #endif /* _LIB_H */ -- 2.34.1