chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
 
using System.Diagnostics;
using System.IO;
 
namespace OpenTap
{
    /// <summary> Detects which operating system is used. </summary>
    class OperatingSystem
    {
        public static readonly OperatingSystem Windows = new OperatingSystem(nameof(Windows));
        public static readonly OperatingSystem Linux = new OperatingSystem(nameof(Linux));
        public static readonly OperatingSystem MacOS = new OperatingSystem(nameof(MacOS));
        public static readonly OperatingSystem Unsupported = new OperatingSystem(nameof(Unsupported));
        public override string ToString() => Name;
        public string Name { get; }
        OperatingSystem(string name)
        {
            Name = name;
        }
 
        private static TraceSource log = Log.CreateSource(nameof(OperatingSystem));
        static OperatingSystem getCurrent()
        {
 
            if (Path.DirectorySeparatorChar == '\\')
            {
                log.Debug($"OS is windows: Path.DirectorySeparatorChar == '{Path.DirectorySeparatorChar}'");
                return OperatingSystem.Windows;
            }
            else
            {
                if (isMacOs())
                {
                    log.Debug("OS is mac (uname)");
                    return OperatingSystem.MacOS;
                }
                else if (Directory.Exists("/proc/"))
                {
                    log.Debug("Os is Linux: /proc/ exists.");
                    return OperatingSystem.Linux;
                }
            }
            log.Debug("OS not detected.");
            return OperatingSystem.Unsupported;
        }
        
        static bool isMacOs()
        {
            try
            {
                var startInfo = new ProcessStartInfo("uname");
                startInfo.RedirectStandardOutput = true;
                var process = Process.Start(startInfo);
                process.WaitForExit(1000);
                var uname = process.StandardOutput.ReadToEnd();
                return uname.ToUpperInvariant().Contains("DARWIN");
            }
            catch
            {
                // ignored
            }
 
            return false;
        }
 
        static OperatingSystem current;
        public static OperatingSystem Current => current ??= getCurrent();
    }
    
    class MacOsArchitecture
    {
        public string Architecture { get; }
        public static readonly MacOsArchitecture Intel = new MacOsArchitecture("x64");
        public static readonly MacOsArchitecture Apple = new MacOsArchitecture("arm64");
        public static MacOsArchitecture Current { get; }
        static MacOsArchitecture()
        {
            try
            {
                var startInfo = new ProcessStartInfo("uname", "-m");
                startInfo.RedirectStandardOutput = true;
                var process = Process.Start(startInfo);
                process.WaitForExit(1000);
                var uname = process?.StandardOutput.ReadToEnd();
                Current = uname.Contains("arm64") ? Apple : Intel;
            }
            catch
            {
                // ignored
            }
        }
        public MacOsArchitecture(string architecture) => Architecture = architecture;
    }
    class LinuxArchitecture
    {
        public string Architecture { get; }
        public static readonly LinuxArchitecture x64 = new LinuxArchitecture("x64");
        public static readonly LinuxArchitecture arm = new LinuxArchitecture("arm");
        public static readonly LinuxArchitecture arm64 = new LinuxArchitecture("arm64");
        public static LinuxArchitecture Current { get; }
        static LinuxArchitecture()
        {
            try
            {
                var startInfo = new ProcessStartInfo("uname", "-m");
                startInfo.RedirectStandardOutput = true;
                var process = Process.Start(startInfo);
                process.WaitForExit(1000);
                var uname = process?.StandardOutput.ReadToEnd();
                if (uname.Contains("armv7"))
                    Current = arm;
                else if (uname.Contains("arm64"))
                    Current = arm64;
                else
                    Current = x64;
            }
            catch
            {
                // ignored
            }
        }
        public LinuxArchitecture(string architecture) => Architecture = architecture;
    }
}