]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libiberty/make-temp-file.c
[Ada] Warning for out-of-order record representation clauses
[thirdparty/gcc.git] / libiberty / make-temp-file.c
index 13e192565340266e80d40ff2bc56a8d6179b6325..2c66e35cc1f515ba16f5c2a6a9d03b66ebb20fc1 100644 (file)
@@ -1,5 +1,5 @@
 /* Utility to pick a temporary filename prefix.
-   Copyright (C) 1996, 1997, 1998, 2001, 2009 Free Software Foundation, Inc.
+   Copyright (C) 1996-2019 Free Software Foundation, Inc.
 
 This file is part of the libiberty library.
 Libiberty is free software; you can redistribute it and/or
@@ -56,7 +56,7 @@ extern int mkstemps (char *, int);
 
 /* Name of temporary file.
    mktemp requires 6 trailing X's.  */
-#define TEMP_FILE "ccXXXXXX"
+#define TEMP_FILE "XXXXXX"
 #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
 
 #if !defined(_WIN32) || defined(__CYGWIN__)
@@ -92,7 +92,7 @@ static char *memoized_tmpdir;
 
 /*
 
-@deftypefn Replacement char* choose_tmpdir ()
+@deftypefn Replacement const char* choose_tmpdir ()
 
 Returns a pointer to a directory path suitable for creating temporary
 files in.
@@ -101,7 +101,7 @@ files in.
 
 */
 
-char *
+const char *
 choose_tmpdir (void)
 {
   if (!memoized_tmpdir)
@@ -121,7 +121,12 @@ choose_tmpdir (void)
 #endif
       
 #ifdef P_tmpdir
-      base = try_dir (P_tmpdir, base);
+      /* We really want a directory name here as if concatenated with say \dir
+        we do not end up with a double \\ which defines an UNC path.  */
+      if (strcmp (P_tmpdir, "\\") == 0)
+       base = try_dir ("\\.", base);
+      else
+       base = try_dir (P_tmpdir, base);
 #endif
 
       /* Try /var/tmp, /usr/tmp, then /tmp.  */
@@ -176,25 +181,31 @@ string is @code{malloc}ed, and the temporary file has been created.
 */
 
 char *
-make_temp_file (const char *suffix)
+make_temp_file_with_prefix (const char *prefix, const char *suffix)
 {
   const char *base = choose_tmpdir ();
   char *temp_filename;
-  int base_len, suffix_len;
+  int base_len, suffix_len, prefix_len;
   int fd;
 
+  if (prefix == 0)
+    prefix = "cc";
+
   if (suffix == 0)
     suffix = "";
 
   base_len = strlen (base);
+  prefix_len = strlen (prefix);
   suffix_len = strlen (suffix);
 
   temp_filename = XNEWVEC (char, base_len
                           + TEMP_FILE_LEN
-                          + suffix_len + 1);
+                          + suffix_len
+                          + prefix_len + 1);
   strcpy (temp_filename, base);
-  strcpy (temp_filename + base_len, TEMP_FILE);
-  strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
+  strcpy (temp_filename + base_len, prefix);
+  strcpy (temp_filename + base_len + prefix_len, TEMP_FILE);
+  strcpy (temp_filename + base_len + prefix_len + TEMP_FILE_LEN, suffix);
 
   fd = mkstemps (temp_filename, suffix_len);
   /* Mkstemps failed.  It may be EPERM, ENOSPC etc.  */
@@ -209,3 +220,9 @@ make_temp_file (const char *suffix)
     abort ();
   return temp_filename;
 }
+
+char *
+make_temp_file (const char *suffix)
+{
+  return make_temp_file_with_prefix (NULL, suffix);
+}