(putenv): New function, copied from gcc.c.
authorRichard Kenner <kenner@gcc.gnu.org>
Thu, 11 Feb 1993 10:55:22 +0000 (05:55 -0500)
committerRichard Kenner <kenner@gcc.gnu.org>
Thu, 11 Feb 1993 10:55:22 +0000 (05:55 -0500)
(last_file_name): New variable.
(find_a_file): Ignore name in last_file_name too.
(main): Look at name in COLLECT_NAME and set it to the name we were called
with.
Pass filename used for ld to ld as argv[0].

From-SVN: r3461

gcc/collect2.c

index f695d377901f6eb360959c49e9b702fb789ba4cf..a8ba20f71b7575337327855dbb03998ffa69091e 100644 (file)
@@ -450,6 +450,52 @@ choose_temp_base ()
   temp_filename_length = strlen (temp_filename);
 }
 \f
+/* Routine to add variables to the environment.  */
+
+#ifndef HAVE_PUTENV
+
+putenv (str)
+     char *str;
+{
+#ifndef VMS                    /* nor about VMS */
+
+  extern char **environ;
+  char **old_environ = environ;
+  char **envp;
+  int num_envs = 0;
+  int name_len = 1;
+  int str_len = strlen (str);
+  char *p = str;
+  int ch;
+
+  while ((ch = *p++) != '\0' && ch != '=')
+    name_len++;
+
+  if (!ch)
+    abort ();
+
+  /* Search for replacing an existing environment variable, and
+     count the number of total environment variables.  */
+  for (envp = old_environ; *envp; envp++)
+    {
+      num_envs++;
+      if (!strncmp (str, *envp, name_len))
+       {
+         *envp = str;
+         return;
+       }
+    }
+
+  /* Add a new environment variable */
+  environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
+  *environ = str;
+  bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
+
+#endif /* VMS */
+}
+
+#endif /* HAVE_PUTENV */
+\f
 /* By default, colon separates directories in a path.  */
 #ifndef PATH_SEPARATOR
 #define PATH_SEPARATOR ':'
@@ -483,10 +529,10 @@ static struct path_prefix cpath, path;
 static char *target_machine = TARGET_MACHINE;
 #endif
 
-/* Name under which we were executed.  Never return that file in our
+/* Names under which we were executed.  Never return one of those files in our
    searches.  */
 
-static char *our_file_name;
+static char *our_file_name, *last_file_name;
 \f
 /* Search for NAME using prefix list PPREFIX.  We only look for executable
    files. 
@@ -523,14 +569,18 @@ find_a_file (pprefix, name)
       {
        strcpy (temp, pl->prefix);
        strcat (temp, name);
-       if (strcmp (temp, our_file_name) != 0 && access (temp, X_OK) == 0)
+       if (strcmp (temp, our_file_name) != 0
+           && ! (last_file_name != 0 && strcmp (temp, last_file_name) == 0)
+           && access (temp, X_OK) == 0)
          return temp;
 
 #ifdef EXECUTABLE_SUFFIX
        /* Some systems have a suffix for executable files.
           So try appending that.  */
        strcat (temp, EXECUTABLE_SUFFIX);
-       if (strcmp (temp, our_file_name) != 0 && access (temp, X_OK) == 0)
+       if (strcmp (temp, our_file_name) != 0
+           && ! (last_file_name != 0 && strcmp (temp, last_file_name) == 0)
+           && access (temp, X_OK) == 0)
          return temp;
 #endif
       }
@@ -663,6 +713,20 @@ main (argc, argv)
 
   our_file_name = argv[0];
 
+  /* We must check that we do not call ourselves in an infinite
+     recursion loop. We save the name used for us in the COLLECT_NAME
+     environment variable, first getting the previous value.
+
+     To be fully safe, we need to maintain a list of names that name
+     been used, but, in practice, two names are enough.  */
+
+  last_file_name = getenv ("COLLECT_NAME");
+
+  p = (char *) xcalloc (sizeof (char *),
+                       sizeof (our_file_name) + strlen ("COLLECT_NAME=") + 1);
+  sprintf (p, "COLLECT_NAME=%s", our_file_name);
+  putenv (p);
+
   p = (char *) getenv ("COLLECT_GCC_OPTIONS");
   if (p)
     while (*p)
@@ -820,7 +884,7 @@ main (argc, argv)
   if (p)
     c_file_name = p;
 
-  *ld1++ = *ld2++ = "ld";
+  *ld1++ = *ld2++ = ld_file_name;
 
   /* Make temp file names. */
   choose_temp_base ();