From 8d6ed9429a14e2306db9721701d256da8b4eba3b Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 22 Apr 2014 23:15:29 +0200 Subject: [PATCH] make getAppFilename work when there is no procfs --- lib/pure/os.nim | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 00a33db754..3cb1482b72 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1,7 +1,7 @@ # # # Nimrod's Runtime Library -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2014 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -1612,6 +1612,16 @@ when defined(linux) or defined(solaris) or defined(bsd) or defined(aix): len = readlink(procPath, result, len) setLen(result, len) + proc getApplHeuristic(): string = + result = string(paramStr(0)) + # POSIX guaranties that this contains the executable + # as it has been executed by the calling process + if len(result) > 0 and result[0] != DirSep: # not an absolute path? + # iterate over any path in the $PATH environment variable + for p in split(string(getEnv("PATH")), {PathSep}): + var x = joinPath(p, result) + if existsFile(x): return x + when defined(macosx): type cuint32* {.importc: "unsigned int", nodecl.} = int @@ -1648,10 +1658,13 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} = 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) @@ -1663,15 +1676,7 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} = else: # little heuristic that may work on other POSIX-like systems: result = string(getEnv("_")) - if len(result) == 0: - result = string(paramStr(0)) - # POSIX guaranties that this contains the executable - # as it has been executed by the calling process - if len(result) > 0 and result[0] != DirSep: # not an absolute path? - # iterate over any path in the $PATH environment variable - for p in split(string(getEnv("PATH")), {PathSep}): - var x = joinPath(p, result) - if existsFile(x): return x + if result.len == 0: result = getApplHeuristic() proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} = ## Returns the filename of the application's executable.