From: Robert Pengelly Date: Tue, 26 May 2026 07:12:02 +0000 (+0100) Subject: Use Windows/Linux temp file creation and use scc_ version as a fallback X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=4b9959feef1496c60d6d68f3fdcc58f693918595;p=scc.git Use Windows/Linux temp file creation and use scc_ version as a fallback --- diff --git a/lib.c b/lib.c index 6f22578..2b7b125 100755 --- 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 @@ -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 2dec618..dab1343 100755 --- 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 +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 4825c64..1cb9103 100755 --- 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 91e1072..9317f5f 100644 --- 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 ca9b15f..c0d1393 100755 --- 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;