From 39da6979add895cf58e9c6f883ef8df465975cd6 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 1 Sep 2013 22:53:02 +0200 Subject: use realpath in a posix compliant way --- lib/pure/os.nim | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 176439ec7..43bdb16ee 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -164,7 +164,7 @@ else: # UNIX-like operating system ScriptExt* = "" DynlibFormat* = when defined(macosx): "lib$1.dylib" else: "lib$1.so" -when defined(macosx) or defined(bsd): +when defined(posix): var pathMax {.importc: "PATH_MAX", header: "".}: cint @@ -674,17 +674,12 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", var L = GetFullPathNameA(filename, bufsize, result, unused) if L <= 0'i32 or L >= bufsize: OSError(OSLastError()) setLen(result, L) - elif defined(macosx) or defined(bsd): - # On Mac OS X 10.5, realpath does not allocate the buffer on its own - var pathBuffer: cstring = newString(pathMax) - var resultBuffer = realpath(filename, pathBuffer) - if resultBuffer == nil: OSError(OSLastError()) - result = $resultBuffer else: - var res = realpath(filename, nil) - if res == nil: OSError(OSLastError()) - result = $res - c_free(res) + # careful, realpath needs to take an allocated buffer according to Posix: + result = newString(pathMax) + var r = realpath(filename, result) + if r.isNil: OSError(OSLastError()) + setlen(result, c_strlen(result)) proc ChangeFileExt*(filename, ext: string): string {. noSideEffect, rtl, extern: "nos$1".} = -- cgit 1.4.1-2-gfad0