Replaced tmpfile for UNIX and WIN32
authorRobert Pengelly <robertapengelly@hotmail.com>
Mon, 13 Oct 2025 02:49:12 +0000 (03:49 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Mon, 13 Oct 2025 02:49:12 +0000 (03:49 +0100)
delete.c
lib.c
lib.h
ranlib.c
replace.c

index fdf6cd20f83dd7688626b783327a0e60608c6052..3649ff2a2575c650468eae5673bbc523ab5ce3fa 100644 (file)
--- 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 cde14cbaaacf913eebba9f9dce1d7a58563e3d2b..fa03cc43971386df9c1eec56de95926b3861152e 100644 (file)
--- 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    <limits.h>
 #include    <stdio.h>
 #include    <stdlib.h>
@@ -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   <windows.h>
+
+# 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 b86c80005c6db1415315d0c5d3d6babfa751324f..52f1f4833a5762537fb5cb154810b3095a110b43 100644 (file)
--- 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    <stdio.h>
+FILE *get_temp_file (void);
+
 #endif      /* _LIB_H */
index ce3aaa15bbe2a4815fb3d5bddf4efcb463dae4dd..61eec684520e907df739a6c0f3ff58559ea95423 100644 (file)
--- 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;
index f22215492ead67c63ab77df57672524eb3b10ede..876df8a2f6387e82480d7a30dee2cc824b8b9f4e 100644 (file)
--- 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;