Fix getCurrentDir() gcc compilation warning. (#61)

* Fix GCC warning on getcwd usage.
* Check returned value of getCurrentDir() in tests.
This commit is contained in:
Eugene Kabanov 2020-11-04 15:22:38 +02:00 committed by GitHub
parent bc3b4cf2de
commit ff524ed832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -208,10 +208,10 @@ elif defined(posix):
importc, header: "<unistd.h>".}
proc read(a1: cint, a2: pointer, a3: csize_t): int {.
importc, header: "<unistd.h>".}
proc c_strlen(a: cstring): cint {.
importc: "strlen", header: "<string.h>", noSideEffect.}
proc c_strerror(errnum: cint): cstring {.
importc: "strerror", header: "<string.h>".}
proc c_free(p: pointer) {.
importc: "free", header: "<stdlib.h>".}
proc getcwd(a1: cstring, a2: int): cstring {.
importc, header: "<unistd.h>", sideEffect.}
proc `==`*(a: IoErrorCode, b: cint): bool {.inline.} =
@ -406,21 +406,18 @@ proc getCurrentDir*(): IoResult[string] =
## Returns string containing an absolute pathname that is the current working
## directory of the calling process.
when defined(posix):
var bufsize = 1024
var buffer = newString(bufsize)
while true:
if getcwd(buffer, bufsize) != nil:
buffer.setLen(c_strlen(buffer))
return ok(buffer)
else:
let res = getcwd(nil, 0)
if isNil(res):
let errCode = ioLastError()
if errCode == EINTR:
continue
elif errCode == ERANGE:
bufsize = bufsize shl 1
buffer = newString(bufsize)
else:
return err(errCode)
else:
var buffer = $res
c_free(res)
return ok(buffer)
elif defined(windows):
var bufsize = uint32(MAX_PATH)
var buffer = newWideCString("", int(bufsize))

View File

@ -2,6 +2,11 @@ import unittest
import ../stew/io2
suite "OS Input/Output procedures test suite":
test "getCurrentDir() test":
let res = getCurrentDir()
check:
res.isOk() == true
len(res.get()) > 0
test "splitDrive() test":
when defined(windows):
check: