Commit 2053de6d59e35b2f74cccea0d0c9419886820c79

Con Kolivas 2011-09-06T10:11:34

Add the directory name from the arguments cgminer was called from as well to allow it running from a relative pathname.

diff --git a/main.c b/main.c
index 355ab1b..b79e28d 100644
--- a/main.c
+++ b/main.c
@@ -31,6 +31,8 @@
 #include <ccan/opt/opt.h>
 #include <jansson.h>
 #include <curl/curl.h>
+#include <libgen.h>
+
 #include "compat.h"
 #include "miner.h"
 #include "findnonce.h"
@@ -225,6 +227,7 @@ bool opt_autofan;
 bool opt_autoengine;
 
 char *opt_kernel_path;
+char *cgminer_path;
 
 #define QUIET	(opt_quiet || opt_realquiet)
 
@@ -4950,8 +4953,11 @@ int main (int argc, char *argv[])
 	sigaction(SIGTERM, &handler, &termhandler);
 	sigaction(SIGINT, &handler, &inthandler);
 
-	opt_kernel_path = malloc(PATH_MAX);
+	opt_kernel_path = alloca(PATH_MAX);
 	strcpy(opt_kernel_path, CGMINER_PREFIX);
+	cgminer_path = alloca(PATH_MAX);
+	strcpy(cgminer_path, dirname(argv[0]));
+	strcat(cgminer_path, "/");
 
 	// Hack to make cgminer silent when called recursively on WIN32
 	int skip_to_bench = 0;
@@ -5307,8 +5313,6 @@ int main (int argc, char *argv[])
 				char *buf;
 
 				applog(LOG_ERR, "The most common reason for this failure is cgminer being unable to read the kernel .cl files");
-				applog(LOG_ERR, "You must either CD into the directory you are running cgminer from,");
-				applog(LOG_ERR, "or run it from a 'make install'ed location. ");
 				applog(LOG_ERR, "Alternatively if it has failed on different GPUs, restarting might help.");
 				failmessage = true;
 				buf = curses_input("Press enter to continue");
@@ -5415,8 +5419,6 @@ int main (int argc, char *argv[])
 		free(block);
 	}
 
-	free(opt_kernel_path);
-
 	curl_global_cleanup();
 
 	return 0;
diff --git a/miner.h b/miner.h
index 5618e12..113c36f 100644
--- a/miner.h
+++ b/miner.h
@@ -304,6 +304,7 @@ extern bool opt_debug;
 extern bool opt_protocol;
 extern bool opt_log_output;
 extern char *opt_kernel_path;
+extern char *cgminer_path;
 extern bool opt_autofan;
 extern bool opt_autoengine;
 
diff --git a/ocl.c b/ocl.c
index 8dde384..ab6fdfb 100644
--- a/ocl.c
+++ b/ocl.c
@@ -39,9 +39,17 @@ char *file_contents(const char *filename, int *length)
 	strcpy(fullpath, opt_kernel_path);
 	strcat(fullpath, filename);
 
-	f = fopen(filename, "rb");
-	if (!f)
+	/* Try in the optional kernel path or installed prefix first */
+	f = fopen(fullpath, "rb");
+	if (!f) {
+		/* Then try from the path cgminer was called */
+		strcpy(fullpath, cgminer_path);
+		strcat(fullpath, filename);
 		f = fopen(fullpath, "rb");
+	}
+	/* Finally try opening it directly */
+	if (!f)
+		f = fopen(filename, "rb");
 
 	if (!f) {
 		applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);