gcc.c-torture/execute: Fix tmpnam issue on Windows
authorJonathan Yong <10walls@gmail.com>
Sun, 22 Aug 2021 03:05:07 +0000 (03:05 +0000)
committerJonathan Yong <10walls@gmail.com>
Mon, 23 Aug 2021 02:17:48 +0000 (02:17 +0000)
2021-08-22  Jonathan Yong  <10walls@gmail.com>

gcc/testsuite/ChangeLog:

* gcc.c-torture/execute/gcc_tmpnam.h: Fix tmpnam case on Windows
where it can return a filename with "\" to indicate current
directory.
* gcc.c-torture/execute/fprintf-2.c: Use wrapper.
* gcc.c-torture/execute/printf-2.c: Use wrapper.
* gcc.c-torture/execute/user-printf.c: Use wrapper.

Signed-off-by: Jonathan Yong <10walls@gmail.com>
gcc/testsuite/gcc.c-torture/execute/fprintf-2.c
gcc/testsuite/gcc.c-torture/execute/gcc_tmpnam.h [new file with mode: 0644]
gcc/testsuite/gcc.c-torture/execute/printf-2.c
gcc/testsuite/gcc.c-torture/execute/user-printf.c

index d8e19e7..00517d1 100644 (file)
@@ -9,10 +9,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "gcc_tmpnam.h"
 
 int main (void)
 {
-  char *tmpfname = tmpnam (0);
+  char *tmpfname = gcc_tmpnam (0);
   FILE *f = fopen (tmpfname, "w");
   if (!f)
     {
diff --git a/gcc/testsuite/gcc.c-torture/execute/gcc_tmpnam.h b/gcc/testsuite/gcc.c-torture/execute/gcc_tmpnam.h
new file mode 100644 (file)
index 0000000..2136e6e
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#ifndef GCC_TMPNAM
+#define GCC_TMPNAM
+static inline char *gcc_tmpnam(char *s)
+{
+  char *ret = tmpnam (s);
+  // Windows sometimes prepends a backslash to denote the current directory,
+  // so swallow that if it occurs
+  if (ret[0] == '\\')
+    ++ret;
+  return ret;
+}
+#endif
index 4e7d8f7..2087bba 100644 (file)
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "gcc_tmpnam.h"
 
 __attribute__ ((noipa)) void
 write_file (void)
@@ -26,7 +27,7 @@ write_file (void)
 
 int main (void)
 {
-  char *tmpfname = tmpnam (0);
+  char *tmpfname = gcc_tmpnam (0);
   FILE *f = freopen (tmpfname, "w", stdout);
   if (!f)
     {
index 42a3b17..bfee076 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "gcc_tmpnam.h"
 
 void __attribute__ ((format (printf, 1, 2), noipa))
 user_print (const char *fmt, ...)
@@ -23,7 +24,7 @@ user_print (const char *fmt, ...)
 
 int main (void)
 {
-  char *tmpfname = tmpnam (0);
+  char *tmpfname = gcc_tmpnam (0);
   FILE *f = freopen (tmpfname, "w", stdout);
   if (!f)
     {