From: Mike Hommey <glandium@debian.org>
Date: Thu, 1 May 2008 14:32:54 +0200
Subject: Use application.ini from where the xulrunner-stub lies

Whether it is a symlink or not, use the application.ini file from where
the xulrunner-stub is, if present. If not, fallback to upstream method
of following symlinks. This will avoid copying the stub in all xulrunner
applications like upstream does.

Squashed:
Refactored the previous change allowing xulrunner-stub to be symlinked

It will now work in more cases, including not calling the stub from
$PATH or an absolute location.
---
 xulrunner/stub/nsXULStub.cpp |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/xulrunner/stub/nsXULStub.cpp b/xulrunner/stub/nsXULStub.cpp
index 57645df..17b96e4 100644
--- a/xulrunner/stub/nsXULStub.cpp
+++ b/xulrunner/stub/nsXULStub.cpp
@@ -278,8 +278,12 @@ main(int argc, char **argv)
   // 3) give up
 
   struct stat fileStat;
-
-  if (!realpath(argv[0], iniPath) || stat(iniPath, &fileStat)) {
+  strncpy(tmpPath, argv[0], sizeof(tmpPath));
+  lastSlash = strrchr(tmpPath, '/');
+  if (lastSlash) {
+    *lastSlash = 0;
+    realpath(tmpPath, iniPath);
+  } else {
     const char *path = getenv("PATH");
     if (!path)
       return 1;
@@ -292,8 +296,11 @@ main(int argc, char **argv)
     char *token = strtok(pathdup, ":");
     while (token) {
       sprintf(tmpPath, "%s/%s", token, argv[0]);
-      if (realpath(tmpPath, iniPath) && stat(iniPath, &fileStat) == 0) {
+      if (stat(tmpPath, &fileStat) == 0) {
         found = PR_TRUE;
+        lastSlash = strrchr(tmpPath, '/');
+        *lastSlash = 0;
+        realpath(tmpPath, iniPath);
         break;
       }
       token = strtok(NULL, ":");
@@ -302,11 +309,15 @@ main(int argc, char **argv)
     if (!found)
       return 1;
   }
+  lastSlash = iniPath + strlen(iniPath);
+  *lastSlash = '/';
 #endif
 
+#ifndef XP_UNIX
   lastSlash = strrchr(iniPath, PATH_SEPARATOR_CHAR);
   if (!lastSlash)
     return 1;
+#endif
 
   *(++lastSlash) = '\0';
 
