Skip to content

Commit

Permalink
Created a namespace System.Private.PlatformSpecific.
Browse files Browse the repository at this point in the history
Added implementations in Unix.cs and Windows.cs for retrieving
environment variables in a platform specific manner.
Implementations are called from Environment.cs

svn path=/trunk/mcs/; revision=554
  • Loading branch information
Jim Richardson committed Aug 22, 2001
1 parent 40055a3 commit 3a88605
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
80 changes: 80 additions & 0 deletions mcs/class/corlib/System/Unix.cs
@@ -0,0 +1,80 @@
//------------------------------------------------------------------------------
//
// System.Private.Unix.cs
//
// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
//
// Author: Jim Richardson, develop@wtfo-guru.com
// Created: Tuesday, August 21, 2001
//
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System
{
public sealed class PlatformSpecific
{
/// <summary>
/// Gets the standard new line value
/// </summary>
public static string NewLine
{
get
{
return "\n";
}
}

[ DllImport("libc", EntryPoint="getpid") ]
private extern static int getPid();

private static char[] getPidFileContents(string fileName, ref int length)
{
Int32 pid = getPid();
// TODO: Use a special folder define probably for the proc folder
string path = Path.Combine("/proc", pid.ToString());
path = Path.Combine(path, fileName);
StreamReader stream = File.OpenText(path);
FileInfo finfo = new FileInfo(path);
length = (int)finfo.Length;
char[] buffer = new char[length];
stream.Read(buffer, 0, length);
return buffer;
}

public static string getEnvironment()
{
// couldn't DllImport environ from libc because it was a
// global variable not a method couldn't find another
// way to access unsafe globals
// definitely a candidate for "libmono"

string strEnv = null;

try
{
int length = 0;
char[] arEnv = getPidFileContents("environ", ref length);

for(int i = 0; i < length - 1; i++)
{
if(arEnv[i] == '\0')
{
arEnv[i] = '\t'; // safer delimeter
}
}
strEnv = new string(arEnv);
//Console.WriteLine(str);
}
catch(Exception e)
{
Debug.WriteLine(e.ToString());
strEnv = null;
}
return strEnv;
}
}
}
101 changes: 101 additions & 0 deletions mcs/class/corlib/System/Windoze.cs
@@ -0,0 +1,101 @@
//------------------------------------------------------------------------------
//
// System.Private.Windows.cs
//
// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
//
// Author: Jim Richardson, develop@wtfo-guru.com
// Created: Tuesday, August 21, 2001
//
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System
{
public sealed class PlatformSpecific
{
/// <summary>
/// Gets the standard new line value
/// </summary>
public static string NewLine
{
get
{
return "\r\n";
}
}

// TODO: verify the "W" versions are available on 95/98/ME
// or whatever other windoze platforms we'll target
[ DllImport("kernel32", EntryPoint="GetEnvironmentStringsW") ]
private extern static IntPtr getEnvironStrings();
[ DllImport("kernel32", EntryPoint="FreeEnvironmentStringsW") ]
private extern static int freeEnvironStrings(IntPtr p);

public static string getEnvironment()
{
IntPtr pEnv = getEnvironStrings();
int start = 0;
int offset = 0;
int length = 0;
int consecutive_nulls = 0;
int null_count = 0;
char ch;
string strEnv = null;

try
{
// the first string is just the process identifer, etc.
// the array of strings ends with double (perhaps) triple null
// if we read too far before we reach the end an exception will
// surely be thrown
while(length == 0)
{
ch = (char)System.Runtime.InteropServices.Marshal.ReadInt16(pEnv, offset);

offset += 2;

if(ch == '\0')
{
null_count++;
consecutive_nulls++;
if(start == 0 && null_count > 1)
{ // we skip the first two windows strings
// because they aren't environment variables
start = offset;
}
if(consecutive_nulls > 1)
{ // TODO: verify this length is exactly correct
// this was a quickie calculation that worked
length = offset - (consecutive_nulls + start);
}
}
else
{
consecutive_nulls = 0;
}
}

char[] arEnv = new char[length];
int index;
for(offset = start, index = 0; index < length; offset += 2, index++)
{
ch = (char)System.Runtime.InteropServices.Marshal.ReadInt16(pEnv, offset);
arEnv[index] = ch == '\0' ? '\n' : ch;
}
strEnv = new string(arEnv);
//Console.WriteLine(str);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
strEnv = null;
}
freeEnvironStrings(pEnv);
return strEnv;
}
}
}

0 comments on commit 3a88605

Please sign in to comment.