+ Lisp_Object name, dir;
+
+ initial_argv = argv;
+ initial_argc = argc;
+
+ Vinvocation_name = Ffile_name_nondirectory (build_string (argv[0]));
+ Vinvocation_directory = Ffile_name_directory (build_string (argv[0]));
+ /* If we got no directory in argv[0], search PATH to find where
+ Emacs actually came from. */
+ if (NILP (Vinvocation_directory))
+ {
+ Lisp_Object found;
+ int yes = openp (Vexec_path, Vinvocation_name,
+ EXEC_SUFFIXES, &found, 1);
+ if (yes == 1)
+ Vinvocation_directory = Ffile_name_directory (found);
+ }
+
+ Vinstallation_directory = Qnil;
+
+ if (!NILP (Vinvocation_directory))
+ {
+ dir = Vinvocation_directory;
+ name = Fexpand_file_name (Vinvocation_name, dir);
+ while (1)
+ {
+ Lisp_Object tem, lib_src_exists;
+ Lisp_Object etc_exists, info_exists;
+
+ /* See if dir contains subdirs for use by Emacs.
+ Check for the ones that would exist in a build directory,
+ not including lisp and info. */
+ tem = Fexpand_file_name (build_string ("lib-src"), dir);
+ lib_src_exists = Ffile_exists_p (tem);
+ if (!NILP (lib_src_exists))
+ {
+ tem = Fexpand_file_name (build_string ("etc"), dir);
+ etc_exists = Ffile_exists_p (tem);
+ if (!NILP (etc_exists))
+ {
+ Vinstallation_directory
+ = Ffile_name_as_directory (dir);
+ break;
+ }
+ }
+
+ /* See if dir's parent contains those subdirs. */
+ tem = Fexpand_file_name (build_string ("../lib-src"), dir);
+ lib_src_exists = Ffile_exists_p (tem);
+ if (!NILP (lib_src_exists))
+ {
+ tem = Fexpand_file_name (build_string ("../etc"), dir);
+ etc_exists = Ffile_exists_p (tem);
+ if (!NILP (etc_exists))
+ {
+ tem = Fexpand_file_name (build_string (".."), dir);
+ Vinstallation_directory
+ = Ffile_name_as_directory (tem);
+ break;
+ }
+ }
+
+ /* If the Emacs executable is actually a link,
+ next try the dir that the link points into. */
+ tem = Ffile_symlink_p (name);
+ if (!NILP (tem))
+ {
+ name = Fexpand_file_name (tem, dir);
+ dir = Ffile_name_directory (name);
+ }
+ else
+ break;
+ }
+ }