Use Windows/Linux temp file creation and use scc_ version as a fallback
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 26 May 2026 07:12:02 +0000 (08:12 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 26 May 2026 07:12:02 +0000 (08:12 +0100)
lib.c
lib.h
list.h
parse.c
token.c

diff --git a/lib.c b/lib.c
index 6f22578cadb6bba416285fed599fb973085b8c7b..2b7b1258329f1656937bc6bcc578b4780ee5a181 100755 (executable)
--- a/lib.c
+++ b/lib.c
@@ -625,6 +625,36 @@ int is_pragma_igored (const char *filename) {
     
     return 0;
 
+}
+
+FILE *open_scc_temp_file (char *name, size_t name_size) {
+
+    char *temp_path, *template;
+    int fd;
+    
+    if (!(temp_path = getenv ("TMPDIR"))) {
+        temp_path = "/tmp";
+    }
+    
+    template = xmalloc (strlen (temp_path) + 1 + 10 + 1);
+    sprintf (template, "%s/sccXXXXXX", temp_path);
+    
+    if (!(fd = mkstemp (template))) {
+        return 0;
+    }
+    
+    (void) name;
+    (void) name_size;
+    
+    return fdopen (fd, "w+b");
+
+}
+
+FILE *scc_tmpfile (void) {
+
+    char name[64];
+    return open_scc_temp_file (name, sizeof (name));
+
 }
 #elif       defined (_WIN32)
 # include       <windows.h>
@@ -706,6 +736,33 @@ int is_pragma_igored (const char *filename) {
     CloseHandle (handle);
     return 0;
 
+}
+
+FILE *open_scc_temp_file (char *name, size_t name_size) {
+
+    char temp_path[MAX_PATH + 1] = { 0 };
+    char full_path[MAX_PATH + 1] = { 0 };
+    
+    if (!GetTempPath (MAX_PATH + 1, temp_path)) {
+        return 0;
+    }
+    
+    if (!GetTempFileName (temp_path, "scc", 0, full_path)) {
+        return 0;
+    }
+    
+    (void) name;
+    (void) name_size;
+    
+    return fopen (full_path, "w+b");
+
+}
+
+FILE *scc_tmpfile (void) {
+
+    char name[64];
+    return open_scc_temp_file (name, sizeof (name));
+
 }
 #else
 char *get_current_directory (void) {
@@ -730,6 +787,56 @@ int is_pragma_igored (const char *filename) {
     vec_push (&vec_pragmas, xstrdup (filename));
     return 0;
 
+}
+
+FILE *open_scc_temp_file (char *name, size_t name_size) {
+
+    static unsigned long temp_id = 1;
+    unsigned long i;
+    
+    FILE *fp;
+    
+    if (!name || name_size == 0) {
+        return 0;
+    }
+    
+    for (i = 0; i < 1000; i++) {
+    
+        sprintf (name, "scc_tmp_%lu.tmp", temp_id++);
+        
+        if ((fp = fopen (name, "rb"))) {
+        
+            fclose (fp);
+            continue;
+        
+        }
+        
+        fp = fopen (name, "w+b");
+        
+        if (fp) {
+            return fp;
+        }
+    
+    }
+    
+    name[0] = 0;
+    return 0;
+
+}
+
+FILE *scc_tmpfile (void) {
+
+    char name[64];
+    FILE *fp;
+
+    fp = open_scc_temp_file (name, sizeof (name));
+
+    if (fp && name[0]) {
+        remove (name);
+    }
+
+    return fp;
+
 }
 #endif
 
diff --git a/lib.h b/lib.h
index 2dec618dc187a95adea9e168a76c882f8ff1e79a..dab1343ea05790e22524ed01e2ef5b64732f3608 100755 (executable)
--- a/lib.h
+++ b/lib.h
@@ -31,6 +31,7 @@ unsigned long get_line_number (void);
 void set_filename (const char *__filename);
 void set_line_number (unsigned long __line_number);
 
+void get_filename_and_line_number (const char **__filename_p, unsigned long *__line_number_p);
 void set_filename_and_line_number (const char *__filename, unsigned long __line_number);
 
 
@@ -48,4 +49,8 @@ void print_include_stack (void);
 char *get_current_directory (void);
 int is_pragma_igored (const char *filename);
 
+#include    <stdio.h>
+FILE *open_scc_temp_file (char *name, size_t name_size);
+FILE *scc_tmpfile (void);
+
 #endif      /* _LIB_H */
diff --git a/list.h b/list.h
index 4825c64a8c1114db82b5efa9239856667c670d10..1cb9103d498924811012e49a8598a7e661e88d47 100755 (executable)
--- a/list.h
+++ b/list.h
@@ -12,5 +12,6 @@ struct list {
 };
 
 void list_append (struct list **list, void *data);
+unsigned int nlist (struct list *list);
 
 #endif      /* _LIST_H */
diff --git a/parse.c b/parse.c
index 91e1072bbbc1b444585199d057bc1ef23ce605a5..9317f5f6b31a20f221f54c0c025f7b3ef28156b2 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -1854,41 +1854,6 @@ static char *read_tmp_file_text (FILE *fp) {
 
 }
 
-static FILE *open_scc_temp_file (char *name, size_t name_size) {
-
-    static unsigned long temp_id = 1;
-    unsigned long i;
-    
-    FILE *fp;
-    
-    if (!name || name_size == 0) {
-        return 0;
-    }
-    
-    for (i = 0; i < 1000; i++) {
-    
-        sprintf (name, "scc_tmp_%lu.tmp", temp_id++);
-        
-        if ((fp = fopen (name, "rb"))) {
-        
-            fclose (fp);
-            continue;
-        
-        }
-        
-        fp = fopen (name, "w+b");
-        
-        if (fp) {
-            return fp;
-        }
-    
-    }
-    
-    name[0] = 0;
-    return 0;
-
-}
-
 static void append_inline_text (char **dst, const char *start, size_t len);
 
 static void emit_function_frame_adjust_text (char **dst, long frame_size) {
@@ -1950,21 +1915,6 @@ static char *replace_function_frame_placeholder (const char *text, long frame_si
 
 }
 
-static FILE *scc_tmpfile (void) {
-
-    char name[64];
-    FILE *fp;
-
-    fp = open_scc_temp_file (name, sizeof (name));
-
-    if (fp && name[0]) {
-        remove (name);
-    }
-
-    return fp;
-
-}
-
 static void append_inline_text (char **dst, const char *start, size_t len) {
 
     size_t old_len = 0;
@@ -37757,15 +37707,24 @@ void compile_translation_unit (void) {
     
     if (vec_dllexports.length) {
     
-        char *p = strrchr (state->ofile, '.'), *tmp;
+        char *p, *tmp;
         
         const char *name;
         long i;
         
         FILE *fp;
         
-        tmp = xmalloc (p - state->ofile + 4);
-        sprintf (tmp, "%.*s.def", (int) (p - state->ofile), state->ofile);
+        if ((p = strrchr (state->ofile, '.'))) {
+        
+            tmp = xmalloc (p - state->ofile + 5);
+            sprintf (tmp, "%.*s.def", (int) (p - state->ofile), state->ofile);
+        
+        } else {
+        
+            tmp = xmalloc (strlen (state->ofile) + 5);
+            sprintf (tmp, "%s.def", state->ofile);
+        
+        }
         
         if ((fp = fopen (tmp, "w"))) {
         
diff --git a/token.c b/token.c
index ca9b15f859f5be6115cb903153f9066be14398ef..c0d13936ff7a092daf2226ee13327d03b3ee8f7a 100755 (executable)
--- a/token.c
+++ b/token.c
@@ -459,7 +459,7 @@ static int get_line (void) {
                         
                             report_line_at (get_filename (), get_line_number (), REPORT_WARNING, start, caret, "missing terminating %c character", ch);
                             
-                            temp = xmalloc (line - caret);
+                            temp = xmalloc ((line - caret + 1));
                             sprintf (temp, "%.*s", (int) (line - caret), caret + 1);
                             
                             new_line_number = line_number;
@@ -624,7 +624,7 @@ static int get_line (void) {
                 
                     report_line_at (get_filename (), get_line_number (), REPORT_WARNING, start, caret, "missing terminating %c character", ch);
                     
-                    temp = xmalloc (line - caret);
+                    temp = xmalloc ((line - caret) + 1);
                     sprintf (temp, "%.*s", (int) (line - caret), caret + 1);
                     
                     new_line_number = line_number;
@@ -1180,7 +1180,7 @@ static int bn_lshift (unsigned int *bn, int shift, int or_val) {
 
 }
 
-float scc_strtof (const char *nptr, char **endptr) {
+static float scc_strtof (const char *nptr, char **endptr) {
 
     const char *s = nptr;
     
@@ -1314,7 +1314,7 @@ float scc_strtof (const char *nptr, char **endptr) {
 
 }
 
-double scc_strtod (const char *nptr, char **endptr) {
+static double scc_strtod (const char *nptr, char **endptr) {
 
     const char *s = nptr;