unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dario Gjorgjevski <dario.gjorgjevski@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: psainty@orcon.net.nz, 47800@debbugs.gnu.org
Subject: bug#47800: [native-comp] could not resolve realpath of "emacs"
Date: Fri, 16 Apr 2021 10:57:22 +0200	[thread overview]
Message-ID: <fv2zojmttysj2l.fsf@gmail.com> (raw)
In-Reply-To: <83im4ny4a6.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 15 Apr 2021 18:08:01 +0300")

[-- Attachment #1: Type: text/plain, Size: 325 bytes --]

> Does the untested patch below fix the problem?

Hi Eli,

Thanks for the patch.  Unfortunately it does not help as the raw_name
variable in set_invocation_vars is still not resolved according to PATH.

The patch I am attaching to this message _does_ resolve the issue, but I
am not sure about Windows.

Best regards,
Dario


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Resolve raw_name according to PATH --]
[-- Type: text/x-diff, Size: 6243 bytes --]

diff --git a/src/emacs.c b/src/emacs.c
index a256564..16fdc3c 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -460,8 +460,81 @@ real_filename (char *filename)
   return real_name;
 }
 
-/* Set `invocation-name' `invocation-directory'.  */
+/* Find a name (absolute or relative) of the Emacs executable whose
+   name (as passed into this program) is ARGV0.  Called early in
+   initialization by portable dumper loading code, so avoid Lisp and
+   associated machinery.  Return a heap-allocated string giving a name
+   of the Emacs executable, or an empty heap-allocated string or NULL
+   if not found.  Store into *CANDIDATE_SIZE a lower bound on the size
+   of any heap allocation.  */
+static char *
+find_executable (char const *argv0, ptrdiff_t *candidate_size)
+{
+  *candidate_size = 0;
+
+  /* Use xstrdup etc. to allocate storage, so as to call our private
+     implementation of malloc, since the caller calls our free.  */
+#ifdef WINDOWSNT
+  char *prog_fname = w32_my_exename ();
+  return prog_fname ? xstrdup (prog_fname) : NULL;
+#else  /* !WINDOWSNT */
+  char *candidate = NULL;
+
+  /* If the executable name contains a slash, we have some kind of
+     path already, so just copy it.  */
+  eassert (argv0);
+  if (strchr (argv0, DIRECTORY_SEP))
+    return xstrdup (argv0);
+  ptrdiff_t argv0_length = strlen (argv0);
+
+  const char *path = getenv ("PATH");
+  if (!path)
+    {
+      /* Default PATH is implementation-defined, so we don't know how
+         to conduct the search.  */
+      return NULL;
+    }
+
+  /* Actually try each concatenation of a path element and the
+     executable basename.  */
+  do
+    {
+      static char const path_sep[] = { SEPCHAR, '\0' };
+      ptrdiff_t path_part_length = strcspn (path, path_sep);
+      const char *path_part = path;
+      path += path_part_length;
+      if (path_part_length == 0)
+        {
+          path_part = ".";
+          path_part_length = 1;
+        }
+      ptrdiff_t needed = path_part_length + 1 + argv0_length + 1;
+      if (*candidate_size <= needed)
+	{
+	  xfree (candidate);
+	  candidate = xpalloc (NULL, candidate_size,
+			       needed - *candidate_size + 1, -1, 1);
+	}
+      memcpy (candidate + 0, path_part, path_part_length);
+      candidate[path_part_length] = DIRECTORY_SEP;
+      memcpy (candidate + path_part_length + 1, argv0, argv0_length + 1);
+      struct stat st;
+      if (file_access_p (candidate, X_OK)
+	  && stat (candidate, &st) == 0 && S_ISREG (st.st_mode))
+	{
+	  if (lstat (candidate, &st) == 0 && S_ISLNK (st.st_mode))
+	    return realpath (candidate, NULL);
+	  return candidate;
+	}
+      *candidate = '\0';
+    }
+  while (*path++ != '\0');
+
+  return candidate;
+#endif	/* !WINDOWSNT */
+}
 
+/* Set `invocation-name' `invocation-directory'.  */
 static void
 set_invocation_vars (char *argv0, char const *original_pwd)
 {
@@ -486,7 +559,9 @@ set_invocation_vars (char *argv0, char const *original_pwd)
       raw_name = build_unibyte_string (argv0);
   }
 #else
-  raw_name = build_unibyte_string (argv0);
+  ptrdiff_t bufsize;
+  char *executable = find_executable (argv0, &bufsize);
+  raw_name = build_unibyte_string (executable);
 #endif
 
   /* Add /: to the front of the name
@@ -785,76 +860,6 @@ dump_error_to_string (int result)
     }
 }
 
-/* Find a name (absolute or relative) of the Emacs executable whose
-   name (as passed into this program) is ARGV0.  Called early in
-   initialization by portable dumper loading code, so avoid Lisp and
-   associated machinery.  Return a heap-allocated string giving a name
-   of the Emacs executable, or an empty heap-allocated string or NULL
-   if not found.  Store into *CANDIDATE_SIZE a lower bound on the size
-   of any heap allocation.  */
-static char *
-load_pdump_find_executable (char const *argv0, ptrdiff_t *candidate_size)
-{
-  *candidate_size = 0;
-
-  /* Use xstrdup etc. to allocate storage, so as to call our private
-     implementation of malloc, since the caller calls our free.  */
-#ifdef WINDOWSNT
-  char *prog_fname = w32_my_exename ();
-  return prog_fname ? xstrdup (prog_fname) : NULL;
-#else  /* !WINDOWSNT */
-  char *candidate = NULL;
-
-  /* If the executable name contains a slash, we have some kind of
-     path already, so just copy it.  */
-  eassert (argv0);
-  if (strchr (argv0, DIRECTORY_SEP))
-    return xstrdup (argv0);
-  ptrdiff_t argv0_length = strlen (argv0);
-
-  const char *path = getenv ("PATH");
-  if (!path)
-    {
-      /* Default PATH is implementation-defined, so we don't know how
-         to conduct the search.  */
-      return NULL;
-    }
-
-  /* Actually try each concatenation of a path element and the
-     executable basename.  */
-  do
-    {
-      static char const path_sep[] = { SEPCHAR, '\0' };
-      ptrdiff_t path_part_length = strcspn (path, path_sep);
-      const char *path_part = path;
-      path += path_part_length;
-      if (path_part_length == 0)
-        {
-          path_part = ".";
-          path_part_length = 1;
-        }
-      ptrdiff_t needed = path_part_length + 1 + argv0_length + 1;
-      if (*candidate_size <= needed)
-	{
-	  xfree (candidate);
-	  candidate = xpalloc (NULL, candidate_size,
-			       needed - *candidate_size + 1, -1, 1);
-	}
-      memcpy (candidate + 0, path_part, path_part_length);
-      candidate[path_part_length] = DIRECTORY_SEP;
-      memcpy (candidate + path_part_length + 1, argv0, argv0_length + 1);
-      struct stat st;
-      if (file_access_p (candidate, X_OK)
-	  && stat (candidate, &st) == 0 && S_ISREG (st.st_mode))
-	return candidate;
-      *candidate = '\0';
-    }
-  while (*path++ != '\0');
-
-  return candidate;
-#endif	/* !WINDOWSNT */
-}
-
 static void
 load_pdump (int argc, char **argv, char const *original_pwd)
 {
@@ -906,7 +911,7 @@ load_pdump (int argc, char **argv, char const *original_pwd)
      encoding the system natively uses for filesystem access, so
      there's no need for character set conversion.  */
   ptrdiff_t bufsize;
-  dump_file = load_pdump_find_executable (argv[0], &bufsize);
+  dump_file = find_executable (argv[0], &bufsize);
 
   /* If we couldn't find our executable, go straight to looking for
      the dump in the hardcoded location.  */

[-- Attachment #3: Type: text/plain, Size: 127 bytes --]

-- 
$ keyserver=hkps://hkps.pool.sks-keyservers.net
$ keyid=744A4F0B4F1C9371
$ gpg --keyserver $keyserver --search-keys $keyid

  parent reply	other threads:[~2021-04-16  8:57 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-15 14:09 bug#47800: [native-comp] could not resolve realpath of "emacs" Dario Gjorgjevski
2021-04-15 14:17 ` Phil Sainty
2021-04-15 14:26   ` Dario Gjorgjevski
2021-04-15 15:08     ` Eli Zaretskii
2021-04-16  5:41       ` bug#44128: " Phil Sainty
2021-04-16  6:41         ` bug#44128: #44128: [feature/native-comp] When invoking a symlink to the 'emacs' binary Emacs fails to start Eli Zaretskii
2021-04-16 11:18           ` Phil Sainty
2021-04-16  8:57       ` Dario Gjorgjevski [this message]
2021-04-16  9:08         ` bug#47800: [native-comp] could not resolve realpath of "emacs" Dario Gjorgjevski
2021-04-16  9:23           ` Dario Gjorgjevski
2021-04-16  9:27             ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-04-16  9:37               ` Dario Gjorgjevski
2021-04-16 10:56               ` Phil Sainty
2021-04-16 11:32                 ` Phil Sainty
2021-04-16 12:32               ` Eli Zaretskii
2021-04-16 19:23                 ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-04-16 19:38                   ` Eli Zaretskii
2021-04-16 20:07                     ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-04-17  7:13                       ` Eli Zaretskii
2021-04-17  7:39                         ` Andrea Corallo via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-04-17 14:16                   ` Eli Zaretskii
2021-04-15 14:44 ` Eli Zaretskii
2021-04-16  9:53 ` wilde
2021-04-17 14:00   ` Eli Zaretskii
2021-04-17 19:12     ` wilde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fv2zojmttysj2l.fsf@gmail.com \
    --to=dario.gjorgjevski@gmail.com \
    --cc=47800@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=psainty@orcon.net.nz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).