Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix os.putEnv in cpp backend on Windows #19293

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/std/private/win_setenv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ check errno_t vs cint

when not defined(windows): discard
else:
type wchar_t {.importc: "wchar_t".} = int16

proc setEnvironmentVariableA*(lpName, lpValue: cstring): int32 {.
stdcall, dynlib: "kernel32", importc: "SetEnvironmentVariableA", sideEffect.}
# same as winlean.setEnvironmentVariableA

proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".}
proc c_putenv_s(envname: cstring, envval: cstring): cint {.importc: "_putenv_s", header: "<stdlib.h>".}
proc c_wgetenv(varname: WideCString): WideCString {.importc: "_wgetenv", header: "<stdlib.h>".}
proc c_wgetenv(varname: ptr wchar_t): ptr wchar_t {.importc: "_wgetenv", header: "<stdlib.h>".}

var errno {.importc, header: "<errno.h>".}: cint
type wchar_t {.importc: "wchar_t".} = int16
var gWenviron {.importc:"_wenviron".}: ptr ptr wchar_t
# xxx `ptr UncheckedArray[WideCString]` did not work

proc mbstowcs_s(pReturnValue: ptr csize_t, wcstr: WideCString, sizeInWords: csize_t, mbstr: cstring, count: csize_t): cint {.importc: "mbstowcs_s", header: "<stdlib.h>".}
proc mbstowcs_s(pReturnValue: ptr csize_t, wcstr: ptr wchar_t, sizeInWords: csize_t, mbstr: cstring, count: csize_t): cint {.importc: "mbstowcs_s", header: "<stdlib.h>".}
# xxx cint vs errno_t?

proc setEnvImpl*(name: cstring, value: cstring, overwrite: cint): cint =
Expand Down Expand Up @@ -77,13 +78,15 @@ else:
if gWenviron != nil:
# var buf: array[MAX_ENV + 1, WideCString]
var buf: array[MAX_ENV + 1, Utf16Char]
let buf2 = cast[WideCString](buf[0].addr)
let buf2 = cast[ptr wchar_t](buf[0].addr)
var len: csize_t
if mbstowcs_s(len.addr, buf2, buf.len.csize_t, name, MAX_ENV) != 0:
errno = EINVAL
return -1
c_wgetenv(buf2)[0] = '\0'.Utf16Char
c_wgetenv(buf2)[1] = '='.Utf16Char
let ptrToEnv = cast[WideCString](c_wgetenv(buf2))
ptrToEnv[0] = '\0'.Utf16Char
let ptrToEnv2 = cast[WideCString](c_wgetenv(buf2))
ptrToEnv2[1] = '='.Utf16Char

# And now, we have to update the outer environment to have a proper empty value.
if setEnvironmentVariableA(name, value) == 0:
Expand Down