Cleanup temp files on exit
authorRobert Pengelly <robertapengelly@hotmail.com>
Sun, 31 May 2026 11:59:19 +0000 (12:59 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sun, 31 May 2026 11:59:19 +0000 (12:59 +0100)
cc.c
lib.c
lib.h

diff --git a/cc.c b/cc.c
index c8978fe8fe77ec6aba4fa933fd71ac30e7b6a3bb..74312ad72a53b47ae86f1911a60c5215c53e46eb 100755 (executable)
--- 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 bfa7a40f0a76c419d921d8a3f0825b8c98bb54ac..36ed9ca6fa95d14a4e50e91b4c5038d8ac7e42c1 100755 (executable)
--- 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       <windows.h>
 
@@ -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 afaf50c9371508a5995a41ad40562d504841b2bb..d57619407303c55b7e6957868a9861097c40afaf 100755 (executable)
--- 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 */