choose-temp.c (choose_temp_base): Remove MPW bits.
authorJeffrey A Law <law@cygnus.com>
Sat, 27 Jun 1998 00:43:42 +0000 (00:43 +0000)
committerJeff Law <law@gcc.gnu.org>
Sat, 27 Jun 1998 00:43:42 +0000 (18:43 -0600)
        * choose-temp.c (choose_temp_base): Remove MPW bits.  Use mkstemp
        instead of mktemp.
        * gcc.c (MKTEMP_EACH_FILE): Define.
        (main): No need to call choose_temp_base if we are going to
        use choose_temp_base to create each file later.
        * mkstemp.c: New file.  Adapted from glibc.
        * Makefile.in (xgcc, colect2, protoize, unprotoize): Link in mkstemp.o
        (mkstemp.o): Add dependencies.

From-SVN: r20746

gcc/ChangeLog
gcc/Makefile.in
gcc/choose-temp.c
gcc/gcc.c
gcc/mkstemp.c [new file with mode: 0644]

index c08fa65be5536b812ed13fd53c5e051c2d354925..dc0dcb542e64eb6e17194a821137acf97957f950 100644 (file)
@@ -1,3 +1,18 @@
+Sat Jun 27 01:35:14 1998  Jeffrey A Law  (law@cygnus.com)
+
+       * choose-temp.c (choose_temp_base): Remove MPW bits.  Use mkstemp
+       instead of mktemp.
+       * gcc.c (MKTEMP_EACH_FILE): Define.
+       (main): No need to call choose_temp_base if we are going to
+       use choose_temp_base to create each file later.
+       * mkstemp.c: New file.  Adapted from glibc.
+       * Makefile.in (xgcc, colect2, protoize, unprotoize): Link in mkstemp.o
+       (mkstemp.o): Add dependencies.
+
+        * configure.in (gettimeofday): Check for its existance.
+        * config.in (HAVE_GETTIMEOFDAY): Define.
+        * configure: Rebuilt.
+
 1998-06-26  Michael Meissner  <meissner@cygnus.com>
 
        * rs6000.md (ne 0, non power case): Add missing & constraint.
index a767cce1f52534c518e09de62022a85db507a65d..c18ff8932efa3496de58fab07c0624ae53b6ea5c 100644 (file)
@@ -840,9 +840,9 @@ stamp-objlist: $(OBJS)
 # to avoid confusion if the current directory is in the path
 # and CC is `gcc'.  It is renamed to `gcc' when it is installed.
 xgcc$(exeext): gcc.o version.o choose-temp.o pexecute.o prefix.o version.o \
-   $(LIBDEPS) $(EXTRA_GCC_OBJS)
+   mkstemp.o $(LIBDEPS) $(EXTRA_GCC_OBJS)
        $(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ gcc.o prefix.o version.o \
-         choose-temp.o pexecute.o $(EXTRA_GCC_OBJS) $(LIBS)
+         choose-temp.o pexecute.o mkstemp.o $(EXTRA_GCC_OBJS) $(LIBS)
 
 # Dump a specs file to make -B./ read these specs over installed ones.
 specs: xgcc$(exeext)
@@ -1282,11 +1282,11 @@ c-iterate.o: c-iterate.c $(CONFIG_H) system.h $(TREE_H) $(RTL_H) c-tree.h \
     flags.h toplev.h expr.h insn-codes.h
 
 collect2$(exeext): collect2.o tlink.o hash.o cplus-dem.o underscore.o \
-       version.o choose-temp.o $(LIBDEPS)
+       version.o choose-temp.o mkstemp.o $(LIBDEPS)
 # Don't try modifying collect2 (aka ld) in place--it might be linking this.
        -rm -f collect2$(exeext)
        $(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ collect2.o tlink.o hash.o \
-         cplus-dem.o underscore.o version.o choose-temp.o $(LIBS)
+         cplus-dem.o underscore.o version.o choose-temp.o mkstemp.o $(LIBS)
 
 collect2.o : collect2.c $(CONFIG_H) system.h gansidecl.h gstab.h obstack.h \
        $(DEMANGLE_H)
@@ -1350,6 +1350,7 @@ dumpvers: dumpvers.c
 version.o: version.c
 obstack.o: obstack.c $(CONFIG_H)
 choose-temp.o: choose-temp.c $(CONFIG_H) gansidecl.h system.h
+mkstemp.o: mkstemp.c $(CONFIG_H) gansidecl.h system.h
 pexecute.o: pexecute.c $(CONFIG_H) system.h gansidecl.h
 prefix.o: prefix.c $(CONFIG_H) system.h gansidecl.h Makefile
        $(CC) $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
@@ -1862,16 +1863,16 @@ cppalloc.o: cppalloc.c $(CONFIG_H) cpplib.h system.h gansidecl.h
 proto: config.status protoize$(exeext) unprotoize$(exeext) SYSCALLS.c.X
 
 protoize$(exeext): protoize.o getopt.o getopt1.o getpwd.o version.o \
-    pexecute.o choose-temp.o $(LIBDEPS)
+    pexecute.o choose-temp.o mkstemp.o $(LIBDEPS)
        $(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ \
          protoize.o getopt.o getopt1.o getpwd.o version.o \
-         pexecute.o choose-temp.o $(LIBS)
+         pexecute.o choose-temp.o mkstemp.o $(LIBS)
 
 unprotoize$(exeext): unprotoize.o getopt.o getopt1.o getpwd.o version.o \
-    pexecute.o choose-temp.o $(LIBDEPS)
+    pexecute.o choose-temp.o mkstemp.o $(LIBDEPS)
        $(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ \
          unprotoize.o getopt.o getopt1.o getpwd.o version.o \
-         pexecute.o choose-temp.o $(LIBS)
+         pexecute.o choose-temp.o mkstemp.o $(LIBS)
 
 protoize.o: protoize.c getopt.h $(CONFIG_H) system.h
        $(CC) -c $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
index ea4f9ed5f329784c3712cdc72d2d84dd9e86fa7f..1cc933c2574fb8347fdafc64466e936767317573 100644 (file)
@@ -110,10 +110,10 @@ choose_temp_base ()
   char *base = 0;
   char *temp_filename;
   int len;
+  int fd;
   static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
   static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
 
-#ifndef MPW
   base = try (getenv ("TMPDIR"), base);
   base = try (getenv ("TMP"), base);
   base = try (getenv ("TEMP"), base);
@@ -130,28 +130,24 @@ choose_temp_base ()
   if (base == 0)
     base = ".";
 
-#else /* MPW */
-  base = ":";
-#endif
-
   len = strlen (base);
   temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
                           + strlen (TEMP_FILE) + 1);
   strcpy (temp_filename, base);
 
-#ifndef MPW
   if (len != 0
       && temp_filename[len-1] != '/'
       && temp_filename[len-1] != DIR_SEPARATOR)
     temp_filename[len++] = DIR_SEPARATOR;
-#else /* MPW */
-  if (temp_filename[len-1] != ':')
-    temp_filename[len++] = ':';
-#endif /* MPW */
   strcpy (temp_filename + len, TEMP_FILE);
 
-  mktemp (temp_filename);
-  if (strlen (temp_filename) == 0)
+  fd = mkstemp (temp_filename);
+  /* If mkstemp failed, then something bad is happening.  Maybe we should
+     issue a message about a possible security attack in progress?  */
+  if (fd == -1)
+    abort ();
+  /* Similarly if we can not close the file.  */
+  if (close (fd))
     abort ();
   return temp_filename;
 }
index f9cd0efb9453502ecc89cb7ae2c4b04a6d7ffe6a..5a752a374762d6fcd9075fcc37a389107b35ff7c 100644 (file)
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1271,6 +1271,9 @@ static int argbuf_length;
 
 static int argbuf_index;
 
+/* We want this on by default all the time now.  */
+#define MKTEMP_EACH_FILE
+
 #ifdef MKTEMP_EACH_FILE
 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
    temp file.  */
@@ -4547,8 +4550,10 @@ main (argc, argv)
 
   /* Choose directory for temp files.  */
 
+#ifndef MKTEMP_EACH_FILE
   temp_filename = choose_temp_base ();
   temp_filename_length = strlen (temp_filename);
+#endif
 
   /* Make a table of what switches there are (switches, n_switches).
      Make a table of specified input files (infiles, n_infiles).
diff --git a/gcc/mkstemp.c b/gcc/mkstemp.c
new file mode 100644 (file)
index 0000000..1ef90a5
--- /dev/null
@@ -0,0 +1,111 @@
+/* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#ifndef IN_GCC
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/time.h>
+#else
+#include "config.h"
+#include "system.h"
+
+/* We need to provide a type for uint64_t.  */
+#ifdef __GNUC__
+typedef unsigned long long uint64_t;
+#else
+typedef unsigned long uint64_t;
+#endif
+
+#ifndef TMP_MAX
+#define TMP_MAX 16384
+#endif
+#endif
+
+/* Generate a unique temporary file name from TEMPLATE.
+   The last six characters of TEMPLATE must be "XXXXXX";
+   they are replaced with a string that makes the filename unique.
+   Returns a file descriptor open on the file for reading and writing.  */
+int
+mkstemp (template)
+     char *template;
+{
+  static const char letters[62]
+    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+  static uint64_t value;
+#ifdef HAVE_GETTIMEOFDAY
+  struct timeval tv;
+#endif
+  char *XXXXXX;
+  size_t len;
+  int count;
+
+  len = strlen (template);
+  if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
+    {
+      return -1;
+    }
+
+  /* This is where the Xs start.  */
+  XXXXXX = &template[len - 6];
+
+#ifdef HAVE_GETTIMEOFDAY
+  /* Get some more or less random data.  */
+  __gettimeofday (&tv, NULL);
+  value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
+#else
+  value += getpid ();
+#endif
+
+  for (count = 0; count < TMP_MAX; ++count)
+    {
+      uint64_t v = value;
+      int fd;
+
+      /* Fill in the random bits.  */
+      XXXXXX[0] = letters[v % 62];
+      v /= 62;
+      XXXXXX[1] = letters[v % 62];
+      v /= 62;
+      XXXXXX[2] = letters[v % 62];
+      v /= 62;
+      XXXXXX[3] = letters[v % 62];
+      v /= 62;
+      XXXXXX[4] = letters[v % 62];
+      v /= 62;
+      XXXXXX[5] = letters[v % 62];
+
+      fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
+      if (fd >= 0)
+       /* The file does not exist.  */
+       return fd;
+
+      /* This is a random value.  It is only necessary that the next
+        TMP_MAX values generated by adding 7777 to VALUE are different
+        with (module 2^32).  */
+      value += 7777;
+    }
+
+  /* We return the null string if we can't find a unique file name.  */
+  template[0] = '\0';
+  return -1;
+}