summary refs log tree commit diff stats
diff options
context:
space:
mode:
authordef <dennis@felsin9.de>2016-03-07 01:44:11 +0100
committerdef <dennis@felsin9.de>2016-03-07 02:12:44 +0100
commit4b1e3f26a73a0f9bf773f5a5c70025ed70a038cc (patch)
treea7a1c33919037d9dda541c720a7b6495b17607e0
parentc398bdc534a80cbacc74d21329346ed7fedc1107 (diff)
downloadNim-4b1e3f26a73a0f9bf773f5a5c70025ed70a038cc.tar.gz
Better getAppFilename() heuristic for OpenBSD and NetBSD
Using the environment variable _ is completely broken and makes it
impossible to build even nimble. After calling `sh` (ksh) on OpenBSD,
`_` is wrongly set to `/bin/sh` and all subprocess calls to Nim fail.
-rw-r--r--lib/pure/os.nim25
1 files changed, 9 insertions, 16 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 017385825..64b270f0c 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -1350,15 +1350,12 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect].} =
   ## Returns the filename of the application's executable.
   ##
   ## This procedure will resolve symlinks.
-  ##
-  ## **Note**: This does not work reliably on BSD.
 
   # Linux: /proc/<pid>/exe
   # Solaris:
   # /proc/<pid>/object/a.out (filename only)
   # /proc/<pid>/path/a.out (complete pathname)
-  # *BSD (and maybe Darwin too):
-  # /proc/<pid>/file
+  # FreeBSD: /proc/<pid>/file
   when defined(windows):
     when useWinUnicode:
       var buf = newWideCString("", 256)
@@ -1368,15 +1365,6 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect].} =
       result = newString(256)
       var len = getModuleFileNameA(0, result, 256)
       setlen(result, int(len))
-  elif defined(linux) or defined(aix):
-    result = getApplAux("/proc/self/exe")
-    if result.len == 0: result = getApplHeuristic()
-  elif defined(solaris):
-    result = getApplAux("/proc/" & $getpid() & "/path/a.out")
-    if result.len == 0: result = getApplHeuristic()
-  elif defined(freebsd):
-    result = getApplAux("/proc/" & $getpid() & "/file")
-    if result.len == 0: result = getApplHeuristic()
   elif defined(macosx):
     var size: cuint32
     getExecPath1(nil, size)
@@ -1386,9 +1374,15 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect].} =
     if result.len > 0:
       result = result.expandFilename
   else:
+    when defined(linux) or defined(aix):
+      result = getApplAux("/proc/self/exe")
+    elif defined(solaris):
+      result = getApplAux("/proc/" & $getpid() & "/path/a.out")
+    elif defined(freebsd):
+      result = getApplAux("/proc/" & $getpid() & "/file")
     # little heuristic that may work on other POSIX-like systems:
-    result = string(getEnv("_"))
-    if result.len == 0: result = getApplHeuristic()
+    if result.len == 0:
+      result = getApplHeuristic()
 
 proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
   ## Returns the filename of the application's executable.
@@ -1404,7 +1398,6 @@ proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} =
 
 proc getAppDir*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect].} =
   ## Returns the directory of the application's executable.
-  ## **Note**: This does not work reliably on BSD.
   result = splitFile(getAppFilename()).dir
 
 proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [TimeEffect].} =