Better temp file creation for non WIN32/unix systems
authorRobert Pengelly <robertapengelly@hotmail.com>
Wed, 3 Jun 2026 11:31:18 +0000 (12:31 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Wed, 3 Jun 2026 11:31:18 +0000 (12:31 +0100)
lib.c

diff --git a/lib.c b/lib.c
index 86805eb11742d3bd1ac49b4cf1fa724e2f01bcc6..7eb2bb0fabd96949e889cfa402bf26735d4c9563 100755 (executable)
--- a/lib.c
+++ b/lib.c
@@ -988,6 +988,7 @@ int scc_close (FILE *fp) {
             free (tmp->path);
             free (tmp->name);
             
+            free (tmp);
             return 0;
         
         }
@@ -1064,26 +1065,29 @@ int is_pragma_igored (const char *filename) {
 
 FILE *scc_tmpfile (void) {
 
-    static unsigned long temp_id = 1;
+    static char name[64] = { 0 };
     
-    char name[64], *temp;
-    unsigned long i;
+    char *temp;
+    unsigned long index, i, j;
     
     struct hashtab_name *key;
     struct temp_file *tmp;
     
     FILE *fp;
     
-    for (i = 0; i < 1000; i++) {
+    for (i = 0; i < 456976; i++) {
     
-        sprintf (name, "scc_tmp_%lu.tmp", temp_id++);
+        sprintf (name, "scc_AAAA.tmp");
         
-        if ((fp = fopen (name, "rb"))) {
+        index = 7;
+        j = i;
         
-            fclose (fp);
-            continue;
+        do {
         
-        }
+            name[index] = (j % 26) + 'A';
+            index--;
+        
+        } while ((j /= 26) >= 26);
         
         temp = xstrdup (name);
         
@@ -1094,6 +1098,15 @@ FILE *scc_tmpfile (void) {
         
         }
         
+        if ((fp = fopen (temp, "rb"))) {
+        
+            fclose (fp);
+            
+            free (temp);
+            continue;
+        
+        }
+        
         if (!((key = hashtab_alloc_name (temp)))) {
         
             report_at (program_name, 0, REPORT_ERROR, "failed to allocate memory for key");
@@ -1101,7 +1114,7 @@ FILE *scc_tmpfile (void) {
         
         }
         
-        if ((fp = fopen (name, "w+b"))) {
+        if ((fp = fopen (temp, "w+b"))) {
             
             tmp = xmalloc (sizeof (*tmp));
             
@@ -1151,6 +1164,8 @@ int scc_close (FILE *fp) {
             remove (tmp->name);
             
             free (tmp->name);
+            free (tmp);
+            
             return 0;
         
         }