This is a patch to enable mkbundle executable files to work when they are
run from a path that is on the PATH environment variable.

Mono mini has a function probe_embedded() which looks for the program's
file (using argv[0]). If the program is run from a path on the PATH
environment variable then the open() fails and only a single parameter is
sent to the mono_main() which in turn causes an if condition to print the
help and exit.

This patch searches through the directories specified in the current
environment's PATH variable and tries to open() then first file found.

Apologies if this is not the right way to submit patches but I had a quick
look at the contribution page online and my understanding is that I must
send a patch this to the community with the most likely or relevant
owner(s) on the CC.

Please let me know if I need to do anything else to push this (change
log!?).


Thanks



Ramin Zaghi

*Mosaic3DX™ | User Interface Technology*
St John's Innovation Centre,
Cowley Road,
Cambridge,
CB4 0WS, UK
*E*: rza...@mosaic3dx.com
*T*: +44 1223 421 311
http://linkedin.com/in/raminzaghi
From e56b276f949864ef4a30149db425b77afed4f511 Mon Sep 17 00:00:00 2001
From: Ramin Zaghi <rza...@mosaic3dx.com>
Date: Fri, 1 Dec 2017 18:59:46 +0000
Subject: [PATCH] Make sure mkbundle executables work if they are run from a
 path on the PATH environment variable.

---
 mono/mini/main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

diff --git a/mono/mini/main.c b/mono/mini/main.c
index 874548e3b13d3cd13d62e56ce3a4aa195d1da1f8..c67ba5eeeed82e94e567923f9edae2ac1dba4ac3 100644
--- a/mono/mini/main.c
+++ b/mono/mini/main.c
@@ -16,6 +16,7 @@
  */
 #include <config.h>
 #include <fcntl.h>
+#include <dirent.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/mono-config.h>
 #include <mono/utils/mono-mmap.h>
@@ -186,6 +187,46 @@ save_library (int fd, uint64_t offset, uint64_t size, const char *destfname)
 }
 
 static gboolean
+search_directories(const char *envPath, const char *program, char **new_program)
+{
+	char *path = NULL;
+
+#ifdef HOST_WIN32
+	const char *path_separators = ";";
+	const char *directory_separator = "\\";
+#else
+	const char *path_separators = ":";
+	const char *directory_separator = "/";
+#endif
+
+	path = strtok (envPath, path_separators);
+	while (path != NULL)
+	{
+		DIR *dir = NULL;
+		struct dirent *ent = NULL;
+		if ((dir = opendir (path)) != NULL)
+		{
+			while ((ent = readdir (dir)) != NULL)
+			{
+				if (!strcmp(ent->d_name, program))
+				{
+					*new_program = (char *) malloc(strlen(path) + strlen(directory_separator) + strlen(program) + 1);
+					strcpy(*new_program, path);
+					strcat(*new_program, directory_separator);
+					strcat(*new_program, program);
+					return TRUE;
+				}
+			}
+			closedir (dir);
+		}
+		path = strtok (NULL, path_separators);
+	}
+
+	new_program = NULL;
+	return FALSE;
+}
+
+static gboolean
 probe_embedded (const char *program, int *ref_argc, char **ref_argv [])
 {
 	MonoBundledAssembly last = { NULL, 0, 0 };
@@ -202,10 +243,27 @@ probe_embedded (const char *program, int *ref_argc, char **ref_argv [])
 	char *entry_point = NULL;
 	char **new_argv;
 	int j;
+	char *new_program = NULL;
 
 	int fd = open (program, O_RDONLY);
 	if (fd == -1)
-		return FALSE;
+        {
+		// Also search through the PATH in case the program ws run from a different directory
+		char* envPath = getenv ("PATH"); // Notice on Windows the variable names are not case-sensitive so this will work on Unix as well as Windows
+		if (envPath != NULL)
+		{
+			if(search_directories(envPath, program, &new_program))
+			{
+				fprintf (stderr, "Found a program: %s\n", new_program);
+				fd = open (new_program, O_RDONLY);
+			}
+		}
+
+		if (fd == -1)
+		{
+			return FALSE;
+		}
+        }
 	if ((sigstart = lseek (fd, -24, SEEK_END)) == -1)
 		goto doclose;
 	if (read (fd, sigbuffer, sizeof (sigbuffer)) == -1)
-- 
2.7.4

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list@lists.dot.net
http://lists.dot.net/mailman/listinfo/mono-devel-list

Reply via email to