@@ -21,18 +21,34 @@ public enum ProcessEnv {
21
21
22
22
/// Set the given key and value in the process's environment.
23
23
public static func setVar( _ key: String , value: String ) throws {
24
- // FIXME: Need to handle Windows.
24
+ #if os(Windows)
25
+ guard 0 != key. withCString ( encodedAs: UTF16 . self, { keyStr in
26
+ value. withCString ( encodedAs: UTF16 . self) { valStr in
27
+ SetEnvironmentVariableW ( keyStr, valStr)
28
+ }
29
+ } ) else {
30
+ throw SystemError . setenv ( Int32 ( GetLastError ( ) ) , key)
31
+ }
32
+ #else
25
33
guard SPMLibc . setenv ( key, value, 1 ) == 0 else {
26
34
throw SystemError . setenv ( errno, key)
27
35
}
36
+ #endif
28
37
}
29
38
30
39
/// Unset the give key in the process's environment.
31
40
public static func unsetVar( _ key: String ) throws {
32
- // FIXME: Need to handle Windows.
41
+ #if os(Windows)
42
+ guard 0 != key. withCString ( encodedAs: UTF16 . self, { keyStr in
43
+ SetEnvironmentVariableW ( keyStr, nil )
44
+ } ) else {
45
+ throw SystemError . unsetenv ( Int32 ( GetLastError ( ) ) , key)
46
+ }
47
+ #else
33
48
guard SPMLibc . unsetenv ( key) == 0 else {
34
49
throw SystemError . unsetenv ( errno, key)
35
50
}
51
+ #endif
36
52
}
37
53
38
54
/// The current working directory of the process.
@@ -42,10 +58,17 @@ public enum ProcessEnv {
42
58
43
59
/// Change the current working directory of the process.
44
60
public static func chdir( _ path: AbsolutePath ) throws {
45
- // FIXME: Need to handle Windows.
46
61
let path = path. pathString
62
+ #if os(Windows)
63
+ guard 0 != path. withCString ( encodedAs: UTF16 . self, {
64
+ SetCurrentDirectoryW ( $0)
65
+ } ) else {
66
+ throw SystemError . chdir ( Int32 ( GetLastError ( ) ) , path)
67
+ }
68
+ #else
47
69
guard SPMLibc . chdir ( path) == 0 else {
48
70
throw SystemError . chdir ( errno, path)
49
71
}
72
+ #endif
50
73
}
51
74
}
0 commit comments