Repository: dotnet/runtime
File: src/libraries/System.Console/src/System/ConsolePal.Unix.cs (C#)

Code before:
            IntPtr fd = 0;
            if (!Interop.Sys.Fcntl.CheckAccess(fd, (int)FileAccess.Read))
            {
                throw new IOException(SR.IO_NoConsole);
            }
            return new SafeFileHandle(fd, ownsHandle: false);
        }

        public static SafeFileHandle OpenStandardOutputHandle()
        {
            IntPtr fd = 1;
            if (!Interop.Sys.Fcntl.CheckAccess(fd, (int)FileAccess.Write))
            {
                throw new IOException(SR.IO_NoConsole);
            }
            return new SafeFileHandle(fd, ownsHandle: false);
        }

        public static SafeFileHandle OpenStandardErrorHandle()
        {
            IntPtr fd = 2;
            if (!Interop.Sys.Fcntl.CheckAccess(fd, (int)FileAccess.Write))
            {
                throw new IOException(SR.IO_NoConsole);
            }
            return new SafeFileHandle(fd, ownsHandle: false);
        }

        public static Encoding InputEncoding
        {
            get { return GetConsoleEncoding(); }
        }

        public static Encoding OutputEncoding
        {
            get { return GetConsoleEncoding(); }
        }

        private static SyncTextReader? s_stdInReader;

        internal static SyncTextReader StdInReader
        {
            get
            {
                return Volatile.Read(ref s_stdInReader) ?? EnsureInitialized();

                static SyncTextReader EnsureInitialized()
                {
                    EnsureConsoleInitialized();

                    SyncTextReader reader = SyncTextReader.GetSynchronizedTextReader(

Review comment (suggestion):
Let's reduce code duplication

```suggestion
        public static SafeFileHandle OpenStandardInputHandle(bool verifyFd = true) => OpenStandardHandle(0, FileAccess.Read, verifyFd);

        public static SafeFileHandle OpenStandardOutputHandle(bool verifyFd = true) => OpenStandardHandle(1, FileAccess.Write, verifyFd);

        public static SafeFileHandle OpenStandardErrorHandle(bool verifyFd = true) => OpenStandardHandle(2, FileAccess.Write, verifyFd);

        private static SafeFileHandle OpenStandardHandle(IntPtr fd, FileAccess access, bool verifyFd)
        {
            if (verifyFd)
            {
                Interop.CheckIo(Interop.Sys.Fcntl.CheckAccess(fd, (int)access));
            }

            return new SafeFileHandle(fd, ownsHandle: false);
```

Code after:
        public static SafeFileHandle OpenStandardOutputHandle() => OpenStandardHandle(1);

        public static SafeFileHandle OpenStandardErrorHandle() => OpenStandardHandle(2);

        private static SafeFileHandle OpenStandardHandle(IntPtr fd) => new SafeFileHandle(fd, ownsHandle: false);

        public static Encoding InputEncoding
        {
            get { return GetConsoleEncoding(); }
        }

        public static Encoding OutputEncoding
        {
            get { return GetConsoleEncoding(); }
        }

        private static SyncTextReader? s_stdInReader;

        internal static SyncTextReader StdInReader
        {
            get
            {
                return Volatile.Read(ref s_stdInReader) ?? EnsureInitialized();

                static SyncTextReader EnsureInitialized()
                {
                    EnsureConsoleInitialized();

                    SyncTextReader reader = SyncTextReader.GetSynchronizedTextReader(
                                                new StdInReader(
                                                    encoding: Console.InputEncoding
                                                ));

                    // Don't overwrite a set reader.
                    // The reader doesn't own resources, so we don't need to dispose
                    // when it was already set.
                    Interlocked.CompareExchange(ref s_stdInReader, reader, null);

                    return s_stdInReader;
                }
            }
        }

        internal static TextReader GetOrCreateReader()
        {
            if (Console.IsInputRedirected)
            {
                Stream inputStream = OpenStandardInput();
                return SyncTextReader.GetSynchronizedTextReader(
                    inputStream == Stream.Null ?
                    StreamReader.Null :