* ldlang.c (lang_process): Use sizeof instead of magic constant.
authorDavid MacKenzie <djm@cygnus>
Mon, 12 Jul 1993 19:34:04 +0000 (19:34 +0000)
committerDavid MacKenzie <djm@cygnus>
Mon, 12 Jul 1993 19:34:04 +0000 (19:34 +0000)
* ldmain.c (get_emulation, check_for_scripts_dir,
set_scripts_dir):  New functions.
(main): Call them.

ld/ChangeLog
ld/ldlang.c
ld/ldmain.c

index 4178568b6614576fa6367371555ca2112a604b42..7445fe316c52b13f06cbc7907368eafdb5041d60 100644 (file)
@@ -1,3 +1,11 @@
+Mon Jul 12 11:45:48 1993  David J. Mackenzie  (djm@thepub.cygnus.com)
+
+       * ldlang.c (lang_process): Use sizeof instead of magic constant.
+
+       * ldmain.c (get_emulation, check_for_scripts_dir,
+       set_scripts_dir):  New functions.
+       (main): Call them.
+
 Mon Jul 12 10:57:03 1993  Ken Raeburn  (raeburn@deneb.cygnus.com)
 
        * scripttempl/elf.sc: Include .init, .fini, .rodata sections.
index b53a2fab3d92c2501c75effed8bc023aa7c3cec9..e2e8d5f7970345ce7fd5a3541d558007d0c3947e 100644 (file)
@@ -2449,7 +2449,8 @@ lang_process ()
     {
       /* Read the emulation's appropriate default script.  */
       char *scriptname = ldemul_get_script ();
-      size_t size = strlen (scriptname) + 13;
+      /* sizeof counts the terminating NUL.  */
+      size_t size = strlen (scriptname) + sizeof ("-Tldscripts/");
       char *buf = (char *) ldmalloc(size);
 
       sprintf (buf, "-Tldscripts/%s", scriptname);
index 0ee740cc29ad75d744baf05cc5e78254d9d8e306..111edea5a0f372ae9b388a5c7c583c253ce49625 100644 (file)
@@ -37,6 +37,16 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "ldctor.h"
 #include "lderror.h"
 
+#include <sys/stat.h>
+#if !defined(S_ISDIR) && defined(S_IFDIR)
+#define        S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
+#endif
+
+#include <string.h>
+
+static char *get_emulation ();
+static void set_scripts_dir ();
+
 /* IMPORTS */
 extern boolean lang_has_input_file;
 extern boolean trace_files;
@@ -125,56 +135,11 @@ main (argc, argv)
      int argc;
 {
   char *emulation;
-  int i;
 
   program_name = argv[0];
 
   bfd_init ();
 
-  /* We need to find any explicitly given emulation before we initialize the
-     state that's needed by the lex&yacc argument parser (parse_args).  */
-
-#ifdef GNU960
-  check_v960 (argc, argv);
-  emulation = "gld960";
-  for (i = 1; i < argc; i++)
-    {
-      if (!strcmp (argv[i], "-Fcoff"))
-       {
-         emulation = "lnk960";
-         output_flavor = BFD_COFF_FORMAT;
-         break;
-       }
-    }
-#else
-  emulation = (char *) getenv (EMULATION_ENVIRON);
-#endif
-
-  for (i = 1; i < argc; i++)
-    {
-      if (!strncmp (argv[i], "-m", 2))
-       {
-         if (argv[i][2] == '\0')
-           {
-             /* -m EMUL */
-             if (i < argc - 1)
-               {
-                 emulation = argv[i + 1];
-                 i++;
-               }
-             else
-               {
-                 einfo("%P%F missing argument to -m\n");
-               }
-           }
-         else
-           {
-             /* -mEMUL */
-             emulation = &argv[i][2];
-           }
-       }
-    }
-
   /* Initialize the data about options.  */
 
 
@@ -187,10 +152,7 @@ main (argc, argv)
   ldsym_init ();
   ldfile_add_arch ("");
 
-  /* Set the default directory for finding script files.
-     Libraries will be searched for here too, but we want
-     them to be, anyway.  */
-  ldfile_add_library_path (SCRIPTDIR);
+  set_scripts_dir ();
 
   config.make_executable = true;
   force_make_executable = false;
@@ -205,11 +167,8 @@ main (argc, argv)
   config.magic_demand_paged = true;
   config.text_read_only = true;
   config.make_executable = true;
-  if (emulation == (char *) NULL)
-    {
-      emulation = DEFAULT_EMULATION;
-    }
 
+  emulation = get_emulation (argc, argv);
   ldemul_choose_mode (emulation);
   default_target = ldemul_choose_target ();
   lang_init ();
@@ -301,7 +260,115 @@ main (argc, argv)
     }
 
   exit (0);
-}                              /* main() */
+}
+
+/* We need to find any explicitly given emulation in order to initialize the
+   state that's needed by the lex&yacc argument parser (parse_args).  */
+
+static char *
+get_emulation (argc, argv)
+     int argc;
+     char **argv;
+{
+  char *emulation;
+  int i;
+
+#ifdef GNU960
+  check_v960 (argc, argv);
+  emulation = "gld960";
+  for (i = 1; i < argc; i++)
+    {
+      if (!strcmp (argv[i], "-Fcoff"))
+       {
+         emulation = "lnk960";
+         output_flavor = BFD_COFF_FORMAT;
+         break;
+       }
+    }
+#else
+  emulation = (char *) getenv (EMULATION_ENVIRON);
+  if (emulation == NULL)
+    emulation = DEFAULT_EMULATION;
+#endif
+
+  for (i = 1; i < argc; i++)
+    {
+      if (!strncmp (argv[i], "-m", 2))
+       {
+         if (argv[i][2] == '\0')
+           {
+             /* -m EMUL */
+             if (i < argc - 1)
+               {
+                 emulation = argv[i + 1];
+                 i++;
+               }
+             else
+               {
+                 einfo("%P%F missing argument to -m\n");
+               }
+           }
+         else
+           {
+             /* -mEMUL */
+             emulation = &argv[i][2];
+           }
+       }
+    }
+
+  return emulation;
+}
+
+/* If directory DIR contains an "ldscripts" subdirectory,
+   add DIR to the library search path and return true,
+   else return false.  */
+
+static boolean
+check_for_scripts_dir (dir)
+     char *dir;
+{
+  size_t dirlen;
+  char *buf;
+  struct stat s;
+  boolean res;
+
+  dirlen = strlen (dir);
+  /* sizeof counts the terminating NUL.  */
+  buf = (char *) ldmalloc (dirlen + sizeof("/ldscripts"));
+  sprintf (buf, "%s/ldscripts", dir);
+
+  res = stat (buf, &s) == 0 && S_ISDIR (s.st_mode);
+  if (res)
+    {
+      buf[dirlen] = '\0';
+      ldfile_add_library_path (buf);
+    }
+  else
+    free (buf);
+
+  return res;
+}
+
+/* Set the default directory for finding script files.
+   Libraries will be searched for here too, but that's ok.  */
+
+static void
+set_scripts_dir ()
+{
+  char *end;
+
+  if (check_for_scripts_dir (SCRIPTDIR))
+    return;                    /* Good--we've been installed.  */
+
+  /* Look for "ldscripts" in the dir where our binary is.  */
+  end = strrchr (program_name, '/');
+  if (!end)
+    return;                    /* Hope for the best.  */
+
+  *end = '\0';
+  check_for_scripts_dir (program_name);
+  *end = '/';
+}
 
 void
 Q_read_entry_symbols (desc, entry)
This page took 0.03719 seconds and 4 git commands to generate.