// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

// TODO: This implementation is temporary until Environment.OSVersion is officially exposed,
//       at which point that should be used instead.

internal static partial class Interop
{
    internal enum OperatingSystem
    {
        Windows,
        Linux,
        OSX
    }

    internal static class PlatformDetection
    {
        internal static OperatingSystem OperatingSystem { get { return s_os.Value; } }

        private static readonly Lazy<OperatingSystem> s_os = new Lazy<OperatingSystem>(() =>
        {
            if (Environment.NewLine != "\r\n")
            {
                unsafe
                {
                    byte* buffer = stackalloc byte[8192]; // the size use with uname is platform specific; this should be large enough for any OS
                    if (uname(buffer) == 0)
                    {
                        return Marshal.PtrToStringAnsi((IntPtr)buffer) == "Darwin" ?
                            OperatingSystem.OSX :
                            OperatingSystem.Linux;
                    }
                }
            }
            return OperatingSystem.Windows;
        });

        // not in src\Interop\Unix to avoiding pulling platform-dependent files into all projects
        [DllImport("libc")]
        private static extern unsafe uint uname(byte* buf); 
    }
}

Add a platform detection mechanism to use in tests
Add a platform detection mechanism to use in tests

This should eventually be replaced with (or reimplemented based on) Environment.OSVersion, once it's available.
