Skip to content

Commit 47da6f1

Browse files
authored
Merge pull request #21 from lorentey/standard-fds
Add symbolic names for standard file descriptors
2 parents 5bea53a + 2904f9e commit 47da6f1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Sources/System/FileDescriptor.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift System project authors
4+
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -25,6 +25,21 @@ public struct FileDescriptor: RawRepresentable, Hashable, Codable {
2525
public init(rawValue: CInt) { self.rawValue = rawValue }
2626
}
2727

28+
// Standard file descriptors
29+
extension FileDescriptor {
30+
/// The standard input file descriptor, with a numeric value of 0.
31+
@_alwaysEmitIntoClient
32+
public static var standardInput: FileDescriptor { .init(rawValue: 0) }
33+
34+
/// The standard output file descriptor, with a numeric value of 1.
35+
@_alwaysEmitIntoClient
36+
public static var standardOutput: FileDescriptor { .init(rawValue: 1) }
37+
38+
/// The standard error file descriptor, with a numeric value of 2.
39+
@_alwaysEmitIntoClient
40+
public static var standardError: FileDescriptor { .init(rawValue: 2) }
41+
}
42+
2843
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
2944
extension FileDescriptor {
3045
/// The desired read and write access for a newly opened file.

Tests/SystemTests/FileTypesTest.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift System open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift System project authors
4+
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -12,6 +12,12 @@ import SystemPackage
1212

1313
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
1414
final class FileDescriptorTest: XCTestCase {
15+
func testStandardDescriptors() {
16+
XCTAssertEqual(FileDescriptor.standardInput.rawValue, 0)
17+
XCTAssertEqual(FileDescriptor.standardOutput.rawValue, 1)
18+
XCTAssertEqual(FileDescriptor.standardError.rawValue, 2)
19+
}
20+
1521
// Test the constants match the C header values. For various reasons,
1622
func testConstants() {
1723
XCTAssertEqual(O_RDONLY, FileDescriptor.AccessMode.readOnly.rawValue)

0 commit comments

Comments
 (0)