Skip to content

Commit f55b5b7

Browse files
Merge pull request migueldeicaza#380 from krzyzanowskim/marcin/current-directory
Add support for setting child process working directory in LocalProcess
2 parents 561d219 + f592210 commit f55b5b7

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

Sources/SwiftTerm/LocalProcess.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public class LocalProcess {
188188
* - Parameter environment: an array of environment variables to pass to the child process, if this is null, this picks a good set of defaults from `Terminal.getEnvironmentVariables`.
189189
* - Parameter execName: If provided, this is used as the Unix argv[0] parameter, otherwise, the executable is used as the args [0], this is used when the intent is to set a different process name than the file that backs it.
190190
*/
191-
public func startProcess(executable: String = "/bin/bash", args: [String] = [], environment: [String]? = nil, execName: String? = nil)
191+
public func startProcess(executable: String = "/bin/bash", args: [String] = [], environment: [String]? = nil, execName: String? = nil, currentDirectory: String? = nil)
192192
{
193193
if running {
194194
return
@@ -208,8 +208,8 @@ public class LocalProcess {
208208
} else {
209209
env = environment!
210210
}
211-
212-
if let (shellPid, childfd) = PseudoTerminalHelpers.fork(andExec: executable, args: shellArgs, env: env, desiredWindowSize: &size) {
211+
212+
if let (shellPid, childfd) = PseudoTerminalHelpers.fork(andExec: executable, args: shellArgs, env: env, currentDirectory: currentDirectory, desiredWindowSize: &size) {
213213
childMonitor = DispatchSource.makeProcessSource(identifier: shellPid, eventMask: .exit, queue: dispatchQueue)
214214
if let cm = childMonitor {
215215
if #available(macOS 10.12, *) {

Sources/SwiftTerm/Pty.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class PseudoTerminalHelpers {
6565
*
6666
* - Returns: nil on error, or a tuple containing the process ID, and the file descriptor to the primary side of the newly created pseudo-terminal.
6767
*/
68-
public static func fork (andExec: String, args: [String], env: [String], desiredWindowSize: inout winsize) -> (pid: pid_t, masterFd: Int32)?
68+
public static func fork (andExec: String, args: [String], env: [String], currentDirectory: String? = nil, desiredWindowSize: inout winsize) -> (pid: pid_t, masterFd: Int32)?
6969
{
7070
var master: Int32 = 0
7171

@@ -74,6 +74,12 @@ public class PseudoTerminalHelpers {
7474
return nil
7575
}
7676
if pid == 0 {
77+
if let currentDirectory {
78+
_ = currentDirectory.withCString { p in
79+
chdir(p)
80+
}
81+
}
82+
7783
withArrayOfCStrings(args, { pargs in
7884
withArrayOfCStrings(env, { penv in
7985
let _ = execve(andExec, pargs, penv)

0 commit comments

Comments
 (0)