From bbb638e508bb3b5392d91327fd4b8edc32226b8e Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 13 Oct 2025 03:49:12 +0100 Subject: [PATCH] Replaced tmpfile for UNIX and WIN32 --- delete.c | 2 +- lib.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib.h | 3 +++ ranlib.c | 2 +- replace.c | 2 +- 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/delete.c b/delete.c index fdf6cd2..3649ff2 100644 --- a/delete.c +++ b/delete.c @@ -11,7 +11,7 @@ void delete (FILE *arfp, const char *fname) { - FILE *tfp = tmpfile (); + FILE *tfp = get_temp_file (); long bytes, len, read; const char *name; diff --git a/lib.c b/lib.c index cde14cb..fa03cc4 100644 --- a/lib.c +++ b/lib.c @@ -1,6 +1,12 @@ /****************************************************************************** * @file lib.c *****************************************************************************/ +#if defined (unix) || defined (__unix) || defined (__unix__) || defined (__APPLE__) +# ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200809L +# endif +#endif + #include #include #include @@ -262,3 +268,49 @@ void *xrealloc (void *__ptr, unsigned long __size) { return ptr; } + + +#if defined (unix) || defined (__unix) || defined (__unix__) || defined (__APPLE__) +FILE *get_temp_file (void) { + + char *temp_path = "/tmp", *template; + int fd; + + template = xmalloc (strlen (temp_path) + 1 + 10 + 1); + sprintf (template, "%s/sarXXXXXX", temp_path); + + if (!(fd = mkstemp (template))) { + return 0; + } + + return fdopen (fd, "w+b"); + +} +#elif defined (_WIN32) +# include + +# ifndef MAX_PATH +# define MAX_PATH 206 +# endif + +FILE *get_temp_file (void) { + + 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, "sar", 0, full_path)) { + return 0; + } + + return fopen (full_path, "w+b"); + +} +#else +FILE *get_temp_file (void) { + return tmpfile (); +} +#endif diff --git a/lib.h b/lib.h index b86c800..52f1f48 100644 --- a/lib.h +++ b/lib.h @@ -15,4 +15,7 @@ void parse_args (int argc, char **argv, int optind); void *xmalloc (unsigned long __size); void *xrealloc (void *__ptr, unsigned long __size); +#include +FILE *get_temp_file (void); + #endif /* _LIB_H */ diff --git a/ranlib.c b/ranlib.c index ce3aaa1..61eec68 100644 --- a/ranlib.c +++ b/ranlib.c @@ -624,7 +624,7 @@ static void omf_get_symbols (void *object, char *filename, uint64_t bytes, int64 void ranlib (FILE *arfp) { - FILE *tfp = tmpfile (); + FILE *tfp = get_temp_file (); struct ar_header header; long i, j, len, read, val; diff --git a/replace.c b/replace.c index f222154..876df8a 100644 --- a/replace.c +++ b/replace.c @@ -11,7 +11,7 @@ void replace (FILE *arfp, const char *fname) { - FILE *tfp = tmpfile (); + FILE *tfp = get_temp_file (); long bytes, len, read; const char *name; -- 2.34.1