Skip to content

Commit

Permalink
Merge pull request #1215 from hartbit/posix-exec
Browse files Browse the repository at this point in the history
Wrap POSIX’s exec for future swift run PR
  • Loading branch information
hartbit committed May 31, 2017
2 parents e9c2095 + 4aa0b4e commit 5fbef73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/Basic/misc.swift
Expand Up @@ -8,6 +8,21 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import libc
import POSIX

/// Replace the current process image with a new process image.
///
/// - Parameters:
/// - path: Absolute path to the executable.
/// - args: The executable arguments.
public func exec(path: String, args: [String]) throws {
let cArgs = CStringArray(args)
guard execv(path, cArgs.cArray) != -1 else {
throw POSIX.SystemError.exec(errno, path: path, args: args)
}
}

// MARK: Utility function for searching for executables

/// Create a list of AbsolutePath search paths from a string, such as the PATH environment variable.
Expand Down
4 changes: 4 additions & 0 deletions Sources/POSIX/Error.swift
Expand Up @@ -12,6 +12,7 @@ public enum SystemError: Swift.Error {
case chdir(Int32, String)
case close(Int32)
case dirfd(Int32, String)
case exec(Int32, path: String, args: [String])
case fgetc(Int32)
case fread(Int32)
case getcwd(Int32)
Expand Down Expand Up @@ -68,6 +69,9 @@ extension SystemError: CustomStringConvertible {
return "close error: \(strerror(errno))"
case .dirfd(let errno, _):
return "dirfd error: \(strerror(errno))"
case .exec(let errno, let path, let args):
let joinedArgs = args.joined(separator: " ")
return "exec error: \(strerror(errno)): \(path) \(joinedArgs)"
case .fgetc(let errno):
return "fgetc error: \(strerror(errno))"
case .fread(let errno):
Expand Down

0 comments on commit 5fbef73

Please sign in to comment.