summaryrefslogtreecommitdiff
path: root/libgfortran
diff options
context:
space:
mode:
authorfxcoudert <fxcoudert@138bc75d-0d04-0410-961f-82ee72b054a4>2011-03-19 12:09:27 +0000
committerfxcoudert <fxcoudert@138bc75d-0d04-0410-961f-82ee72b054a4>2011-03-19 12:09:27 +0000
commitd06c5aaaca3b5053811a3c93458b4a879efde101 (patch)
tree8daf711d7f6d8b47bf84824ba1beb10f9e16069d /libgfortran
parenta276a91d5754885618459f7958012b5e99b23e93 (diff)
downloadgcc-d06c5aaaca3b5053811a3c93458b4a879efde101.tar.gz
PR libfortran/47439
* io/unix.c (tempfile): Work around poor mktemp() implementations. * gfortran.dg/scratch_1.f90: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@171178 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgfortran')
-rw-r--r--libgfortran/ChangeLog5
-rw-r--r--libgfortran/io/unix.c38
2 files changed, 38 insertions, 5 deletions
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 9723efe21ee..70cf85b42a6 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,8 @@
+2011-03-19 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
+
+ PR libfortran/47439
+ * io/unix.c (tempfile): Work around poor mktemp() implementations.
+
2011-03-16 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
PR libfortran/47883
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 12536ca5cbe..edccdd6397a 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -1022,6 +1022,12 @@ tempfile (st_parameter_open *opp)
char *template;
const char *slash = "/";
int fd;
+ size_t tempdirlen;
+
+#ifndef HAVE_MKSTEMP
+ int count;
+ size_t slashlen;
+#endif
tempdir = getenv ("GFORTRAN_TMPDIR");
#ifdef __MINGW32__
@@ -1046,16 +1052,19 @@ tempfile (st_parameter_open *opp)
if (tempdir == NULL)
tempdir = DEFAULT_TEMPDIR;
#endif
+
/* Check for special case that tempdir contains slash
or backslash at end. */
- if (*tempdir == 0 || tempdir[strlen (tempdir) - 1] == '/'
+ tempdirlen = strlen (tempdir);
+ if (*tempdir == 0 || tempdir[tempdirlen - 1] == '/'
#ifdef __MINGW32__
- || tempdir[strlen (tempdir) - 1] == '\\'
+ || tempdir[tempdirlen - 1] == '\\'
#endif
)
slash = "";
- template = get_mem (strlen (tempdir) + 20);
+ // Take care that the template is longer in the mktemp() branch.
+ template = get_mem (tempdirlen + 23);
#ifdef HAVE_MKSTEMP
sprintf (template, "%s%sgfortrantmpXXXXXX", tempdir, slash);
@@ -1064,11 +1073,30 @@ tempfile (st_parameter_open *opp)
#else /* HAVE_MKSTEMP */
fd = -1;
+ count = 0;
+ slashlen = strlen (slash);
do
{
- sprintf (template, "%s%sgfortrantmpXXXXXX", tempdir, slash);
+ sprintf (template, "%s%sgfortrantmpaaaXXXXXX", tempdir, slash);
+ if (count > 0)
+ {
+ int c = count;
+ template[tempdirlen + slashlen + 13] = 'a' + (c% 26);
+ c /= 26;
+ template[tempdirlen + slashlen + 12] = 'a' + (c % 26);
+ c /= 26;
+ template[tempdirlen + slashlen + 11] = 'a' + (c % 26);
+ if (c >= 26)
+ break;
+ }
+
if (!mktemp (template))
- break;
+ {
+ errno = EEXIST;
+ count++;
+ continue;
+ }
+
#if defined(HAVE_CRLF) && defined(O_BINARY)
fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
S_IREAD | S_IWRITE);