[Mono-osx] OSX vs Linux

Gavin Landon Gavin.Landon at ignitetech.com
Mon Dec 17 19:20:57 EST 2007


Thanks, but your method IsUnix will come back true on OSX, which is what
I was trying to prevent.  However someone on this list responded with
the following which seems to work.  He's basically doing an output of
all system vars and checking for "Darwin".   I know in Windows this is a
real bad idea, but from what I've seen, there's no real way to tell
IfOSX.
__________________________

        static public string GetOSType(bool bWithVersion)
        {
            string ret = "unknown";
            switch (Environment.OSVersion.Platform) {
            case PlatformID.Unix:
                ret = "Unix";
                // need to execute uname -s to find out if it is a MAC
or not
                StringBuilder output = new StringBuilder();
                int nRet = execute("uname -s", output);
                if ((nRet == 0) &&
(output.ToString().StartsWith("Darwin"))) {
                    ret = "Mac";
                } // end if
                break;
            case PlatformID.Win32NT:
                ret = "WinNT";
                break;
            case PlatformID.Win32S:
                ret = "Win32s";
                break;
            case PlatformID.Win32Windows:
                ret = "Win95+";
                break;
            case PlatformID.WinCE:
                ret = "WinCE";
                break;
            } // end switch

            if(bWithVersion)
            	ret = ret + " v" + Environment.OSVersion.Version;
            
            return ret;
        } // end GetOS
        
        static int execute(string _command, StringBuilder _output)
        {
            int ret = -1;

            Process proc = new Process();
            int firstSpaceIndex = _command.IndexOf(' ');
            string command = _command.Substring(0, firstSpaceIndex);
            string commandLine = _command.Substring(firstSpaceIndex);
            proc.StartInfo.FileName = command;
            proc.StartInfo.Arguments = commandLine;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;

            // redirect output if we want to
            ms_output = _output;
            
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.OutputDataReceived += new
DataReceivedEventHandler(outputDataReceived_static);
            proc.ErrorDataReceived += new
DataReceivedEventHandler(errorDataReceived_static);

            try {
                proc.Start();
                proc.BeginErrorReadLine();
                proc.BeginOutputReadLine();
                proc.WaitForExit();
                ret = proc.ExitCode;
                proc.Close();
            } // end try
            catch (Exception _e) {
                if (_output != null) {
                    _output.AppendFormat("Exception \"{0}\"\nfor {2}
{3}\nStack Trace{1}", _e.Message, _e.StackTrace, command, commandLine);
                } // end if
                ret = -1;
            } // end catch
            ms_output = null;

            return ret;
        } // end execute
     
        static void errorDataReceived_static(object sender,
DataReceivedEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Data)) {
                if (ms_output != null) {
                    ms_output.AppendFormat("{0}{1}", e.Data,
Environment.NewLine);
                } // end if
            } // end if
        } // end errorDataReceived_static
   
        static void outputDataReceived_static(object sender,
DataReceivedEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Data)) {
                if (ms_output != null) {
                    ms_output.AppendFormat("{0}{1}", e.Data,
Environment.NewLine);
                } // end if
            } // end if
        } // end outputDataReceived_static   

__________________________

-----Original Message-----
From: Miguel de Icaza [mailto:miguel at novell.com] 
Sent: Monday, December 17, 2007 6:04 PM
To: Gavin Landon
Cc: mono-osx at lists.ximian.com
Subject: Re: [Mono-osx] OSX vs Linux

Hello,

> I'm having some trouble distinguishing the difference between Linux 
> and OSX.

Oh.

>From the subject line I felt a new flame war was brewing.

The uname approach works, the following is a piece of uname that uses
P/Invoke instead of depending on uname, its part of mkbundle.cs:

using System.Runtime.InteropServices;

        [DllImport ("libc")]
        static extern int uname (IntPtr buf);

        static void DetectOS ()
        {
                if (!IsUnix) {
                        Console.WriteLine ("OS is: Windows");
                        style = "windows";
                        return;
                }

                IntPtr buf = UnixMarshal.AllocHeap(8192);
                if (uname (buf) != 0){
                        Console.WriteLine ("Warning: Unable to detect
OS");
                        return;
                }
                string os = Marshal.PtrToStringAnsi (buf);
                Console.WriteLine ("OS is: " + os);
                if (os == "Darwin")
                        style = "osx";

                UnixMarshal.FreeHeap(buf);
        }

        static bool IsUnix {
                get {
                        int p = (int) Environment.OSVersion.Platform;
                        return ((p == 4) || (p == 128));
                }
        }



More information about the Mono-osx mailing list