diff options
Diffstat (limited to 'lib/pure/os.nim')
-rw-r--r-- | lib/pure/os.nim | 79 |
1 files changed, 36 insertions, 43 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index d74cb1fb9..2e15587f4 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -165,8 +165,12 @@ else: # UNIX-like operating system DynlibFormat* = when defined(macosx): "lib$1.dylib" else: "lib$1.so" when defined(posix): - var - pathMax {.importc: "PATH_MAX", header: "<stdlib.h>".}: cint + when NoFakeVars: + const pathMax = 5000 # doesn't matter really. The concept of PATH_MAX + # doesn't work anymore on modern OSes. + else: + var + pathMax {.importc: "PATH_MAX", header: "<stdlib.h>".}: cint const ExtSep* = '.' @@ -341,16 +345,6 @@ when defined(windows): template FindNextFile(a, b: expr): expr = FindNextFileW(a, b) template getCommandLine(): expr = getCommandLineW() - proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = - let - nul = 0 - dot = ord('.') - result = (f.cFilename[0].int == dot) - if result: - result = (f.cFilename[1].int in {dot, nul}) - if result: - result = (f.cFilename[2].int == nul) - template getFilename(f: expr): expr = $cast[WideCString](addr(f.cFilename[0])) else: @@ -358,18 +352,13 @@ when defined(windows): template FindNextFile(a, b: expr): expr = FindNextFileA(a, b) template getCommandLine(): expr = getCommandLineA() - proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = - let - nul = '\0' - dot = '.' - result = (f.cFilename[0] == dot) - if result: - result = (f.cFilename[1] in {dot, nul}) - if result: - result = (f.cFilename[2] == nul) - template getFilename(f: expr): expr = $f.cFilename + proc skipFindData(f: TWIN32_FIND_DATA): bool {.inline.} = + const dot = ord('.') + result = f.cFilename[0].int == dot and(f.cFilename[1].int == 0 or + f.cFilename[1].int == dot and f.cFilename[2].int == 0) + proc existsFile*(filename: string): bool {.rtl, extern: "nos$1", tags: [FReadDir].} = ## Returns true if the file exists, false otherwise. @@ -468,20 +457,21 @@ proc setCurrentDir*(newDir: string) {.inline, tags: [].} = ## `newDir` cannot been set. when defined(Windows): when useWinUnicode: - if SetCurrentDirectoryW(newWideCString(newDir)) == 0'i32: OSError(OSLastError()) + if SetCurrentDirectoryW(newWideCString(newDir)) == 0'i32: + OSError(OSLastError()) else: if SetCurrentDirectoryA(newDir) == 0'i32: OSError(OSLastError()) else: if chdir(newDir) != 0'i32: OSError(OSLastError()) -proc JoinPath*(head, tail: string): string {. +proc joinPath*(head, tail: string): string {. noSideEffect, rtl, extern: "nos$1".} = ## Joins two directory names to one. ## ## For example on Unix: ## ## .. code-block:: nimrod - ## JoinPath("usr", "lib") + ## joinPath("usr", "lib") ## ## results in: ## @@ -495,10 +485,10 @@ proc JoinPath*(head, tail: string): string {. ## examples on Unix: ## ## .. code-block:: nimrod - ## assert JoinPath("usr", "") == "usr/" - ## assert JoinPath("", "lib") == "lib" - ## assert JoinPath("", "/lib") == "/lib" - ## assert JoinPath("usr/", "/lib") == "usr/lib" + ## assert joinPath("usr", "") == "usr/" + ## assert joinPath("", "lib") == "lib" + ## assert joinPath("", "/lib") == "/lib" + ## assert joinPath("usr/", "/lib") == "usr/lib" if len(head) == 0: result = tail elif head[len(head)-1] in {DirSep, AltSep}: @@ -512,14 +502,14 @@ proc JoinPath*(head, tail: string): string {. else: result = head & DirSep & tail -proc JoinPath*(parts: varargs[string]): string {.noSideEffect, +proc joinPath*(parts: varargs[string]): string {.noSideEffect, rtl, extern: "nos$1OpenArray".} = - ## The same as `JoinPath(head, tail)`, but works with any number of directory + ## The same as `joinPath(head, tail)`, but works with any number of directory ## parts. You need to pass at least one element or the proc will assert in ## debug builds and crash on release builds. result = parts[0] for i in 1..high(parts): - result = JoinPath(result, parts[i]) + result = joinPath(result, parts[i]) proc `/` * (head, tail: string): string {.noSideEffect.} = ## The same as ``JoinPath(head, tail)`` @@ -531,9 +521,9 @@ proc `/` * (head, tail: string): string {.noSideEffect.} = ## assert "" / "lib" == "lib" ## assert "" / "/lib" == "/lib" ## assert "usr/" / "/lib" == "usr/lib" - return JoinPath(head, tail) + return joinPath(head, tail) -proc SplitPath*(path: string): tuple[head, tail: string] {. +proc splitPath*(path: string): tuple[head, tail: string] {. noSideEffect, rtl, extern: "nos$1".} = ## Splits a directory into (head, tail), so that ## ``JoinPath(head, tail) == path``. @@ -541,11 +531,11 @@ proc SplitPath*(path: string): tuple[head, tail: string] {. ## Examples: ## ## .. code-block:: nimrod - ## SplitPath("usr/local/bin") -> ("usr/local", "bin") - ## SplitPath("usr/local/bin/") -> ("usr/local/bin", "") - ## SplitPath("bin") -> ("", "bin") - ## SplitPath("/bin") -> ("", "bin") - ## SplitPath("") -> ("", "") + ## splitPath("usr/local/bin") -> ("usr/local", "bin") + ## splitPath("usr/local/bin/") -> ("usr/local/bin", "") + ## splitPath("bin") -> ("", "bin") + ## splitPath("/bin") -> ("", "bin") + ## splitPath("") -> ("", "") var sepPos = -1 for i in countdown(len(path)-1, 0): if path[i] in {dirsep, altsep}: @@ -699,7 +689,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", if r.isNil: OSError(OSLastError()) setlen(result, c_strlen(result)) -proc ChangeFileExt*(filename, ext: string): string {. +proc changeFileExt*(filename, ext: string): string {. noSideEffect, rtl, extern: "nos$1".} = ## Changes the file extension to `ext`. ## @@ -971,7 +961,10 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", raise newException(EOS, $strerror(errno)) when not defined(ENOENT) and not defined(Windows): - var ENOENT {.importc, header: "<errno.h>".}: cint + when NoFakeVars: + const ENOENT = cint(2) # 2 on most systems including Solaris + else: + var ENOENT {.importc, header: "<errno.h>".}: cint when defined(Windows): when useWinUnicode: @@ -1028,7 +1021,7 @@ when defined(windows): proc strEnd(cstr: wideCString, c = 0'i32): wideCString {. importc: "wcschr", header: "<string.h>".} else: - proc strEnd(cstr: CString, c = 0'i32): CString {. + proc strEnd(cstr: cstring, c = 0'i32): cstring {. importc: "strchr", header: "<string.h>".} proc getEnvVarsC() = @@ -1333,7 +1326,7 @@ proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", copyFile(path, dest / noSource) of pcDir: copyDir(path, dest / noSource) - else: nil + else: discard proc parseCmdLine*(c: string): seq[string] {. noSideEffect, rtl, extern: "nos$1".} = |