From 85faa290c2777d8e89af05648c229b0da395d4f7 Mon Sep 17 00:00:00 2001
From: Carl Worth <cworth@cworth.org>
Date: Mon, 23 Sep 2013 08:16:27 -0700
Subject: [PATCH] Teach libglaze to look for wrapper library in current working
 directory

Previously, with libfoo.so in the current directory, the following
command would fail:

	glaze --wrapper=libfoo.so <program> <args>

And to fix things, the user would have to explicitly name the current
working directory such as:

	glaze --wrapper=./libfoo.so <program> <args>

That seems like a silly requirement, (and is an artefact if the logic
of the dynamic loader).

To allow the convenient usage (without "./") we teach libglaze to
explicitly look for the wrapper in the current working directory, and
if found, to resolve its absolute path.
---
 libglaze.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/libglaze.c b/libglaze.c
index 8377d84..17c4736 100644
--- a/libglaze.c
+++ b/libglaze.c
@@ -197,20 +197,39 @@ read_process_output_one_line (const char *program)
  * If "wrapper" is an absolute path, return it directly.
  *
  * Otherwise, ("wrapper" is relative), look for an existing file named
- * "wrapper" in the same directory as the currently executing binary,
- * (as determined by /proc/self/exe). If that file exists, return its
- * path.
+ * "wrapper" in the following locations in order:
+ *
+ *	1. The current working directory
+ *
+ *	2. The same directory as the currently executing binary, (as
+ *         determined by /proc/self/exe).
+ *
+ * If either of those files exist, return its path.
  *
  * Otherwise, return the original, relative "wrapper".
  */
 static const char *
 resolve_wrapper_path (void *ctx, const char *wrapper)
 {
-	char *bin_path, *lib_path;
+	char *cwd_path, *bin_path, *lib_path;
 
+	/* Return absolute wrapper path immediately. */
 	if (*wrapper == '/')
 		return wrapper;
 
+	/* Look for wrapper in current working directory. */
+	cwd_path = get_current_dir_name ();
+
+	lib_path = talloc_asprintf (ctx, "%s/%s", cwd_path, wrapper);
+
+	free (cwd_path);
+
+	if (exists (lib_path))
+		return lib_path;
+
+	talloc_free (lib_path);
+
+	/* Look for wrapper next to current executable. */
 	bin_path = get_bin_name (ctx);
 
 	chop_trailing_path_component (bin_path);
@@ -224,6 +243,7 @@ resolve_wrapper_path (void *ctx, const char *wrapper)
 
 	talloc_free (lib_path);
 
+	/* Allow relative wrapper path to remain as-is. */
 	return wrapper;
 }
 
-- 
2.45.2