changeset 445:294353fe2514

Added functions for finding the executables path.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 17 Oct 2007 17:38:54 +0200
parents 0bda71dc9c4f
children 18a8c01e3d7a
files trunk/src/dil/Settings.d trunk/src/main.d
diffstat 2 files changed, 63 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Settings.d	Wed Oct 17 03:12:46 2007 +0300
+++ b/trunk/src/dil/Settings.d	Wed Oct 17 17:38:54 2007 +0200
@@ -50,9 +50,9 @@
   string language; /// Language of loaded messages catalogue.
   string[] messages; /// Table of localized compiler messages.
   string[] importPaths; /// Array of import paths to look for modules.
-  void load(char[] execPath_)
+  void load()
   {
-    scope execPath = new FilePath(execPath_);
+    scope execPath = new FilePath(GetExecutableFilePath());
     auto fileName = execPath.file("config.d").toUtf8();
     auto sourceText = loadFile(fileName);
     auto parser = new Parser(sourceText, fileName);
@@ -142,3 +142,63 @@
     dil.Messages.SetMessages(messages);
   }
 }
+
+version(Windows)
+{
+private extern(Windows) uint GetModuleFileNameA(void*, char*, uint);
+/++
+  Get the fully qualified path to this executable.
++/
+char[] GetExecutableFilePath()
+{
+  alias GetModuleFileNameA GetModuleFileName;
+  char[] buffer = new char[256];
+  uint count;
+
+  while (1)
+  {
+    if (buffer is null)
+      return null;
+
+    count = GetModuleFileName(null, buffer.ptr, buffer.length);
+    if (count == 0)
+      return null;
+    if (buffer.length != count && buffer[count] == 0)
+      break;
+    // Increase size of buffer
+    buffer.length = buffer.length * 2;
+  }
+  assert(buffer[count] == 0);
+  // Reduce buffer to the actual length of the string (excluding '\0'.)
+  if (count < buffer.length)
+    buffer.length = count;
+  return buffer;
+}
+}
+else version(linux)
+{
+private extern(C) size_t readlink(char* path, char* buf, size_t bufsize);
+/++
+  Get the fully qualified path to this executable.
++/
+char[] GetExecutableFilePath()
+{
+  char[] buffer = new char[256];
+  size_t count;
+
+  while (1)
+  {
+    // This won't work on very old Linux systems.
+    count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length);
+    if (count == -1)
+      return null;
+    if (count < buffer.length)
+      break;
+    buffer.length = buffer.length * 2;
+  }
+  buffer.length = count;
+  return buffer;
+}
+}
+else
+  static assert(0, "GetExecutableFilePath() is not implemented on your platform.");
--- a/trunk/src/main.d	Wed Oct 17 03:12:46 2007 +0300
+++ b/trunk/src/main.d	Wed Oct 17 17:38:54 2007 +0200
@@ -19,7 +19,7 @@
 
 void main(char[][] args)
 {
-  GlobalSettings.load(args[0]);
+  GlobalSettings.load();
 
   if (args.length <= 1)
     return Stdout(helpMain()).newline;