-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Environment.GetEnvironmentVariables for Apple platforms usi…
…ng official API (#58161) (#58254) Move environment handling from Mono runtime to System.Native Fixes #58156 Co-authored-by: Filip Navara <[email protected]> Co-authored-by: Adeel Mujahid <[email protected]>
- Loading branch information
1 parent
e1b407b
commit 4cf2af2
Showing
14 changed files
with
238 additions
and
180 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnv.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal unsafe partial class Sys | ||
{ | ||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetEnv")] | ||
internal static extern unsafe IntPtr GetEnv(string name); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnviron.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal unsafe partial class Sys | ||
{ | ||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetEnviron")] | ||
internal static extern unsafe IntPtr GetEnviron(); | ||
|
||
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_FreeEnviron")] | ||
internal static extern unsafe void FreeEnviron(IntPtr environ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "pal_config.h" | ||
#include "pal_environment.h" | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#if HAVE_NSGETENVIRON | ||
#include <crt_externs.h> | ||
#endif | ||
|
||
char* SystemNative_GetEnv(const char* variable) | ||
{ | ||
return getenv(variable); | ||
} | ||
|
||
char** SystemNative_GetEnviron() | ||
{ | ||
#if HAVE_NSGETENVIRON | ||
return *(_NSGetEnviron()); | ||
#else | ||
extern char **environ; | ||
return environ; | ||
#endif | ||
} | ||
|
||
void SystemNative_FreeEnviron(char** environ) | ||
{ | ||
// no op | ||
(void)environ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "pal_compiler.h" | ||
#include "pal_types.h" | ||
|
||
PALEXPORT char* SystemNative_GetEnv(const char* variable); | ||
|
||
PALEXPORT char** SystemNative_GetEnviron(void); | ||
|
||
PALEXPORT void SystemNative_FreeEnviron(char** environ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "pal_config.h" | ||
#include "pal_environment.h" | ||
|
||
#include <Foundation/Foundation.h> | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <objc/runtime.h> | ||
#include <objc/message.h> | ||
|
||
char* SystemNative_GetEnv(const char* variable) | ||
{ | ||
return getenv(variable); | ||
} | ||
|
||
static char *empty_key_value_pair = "="; | ||
|
||
static void get_environ_helper(const void *key, const void *value, void *context) | ||
{ | ||
char ***temp_environ_ptr = (char***)context; | ||
const char *utf8_key = [(NSString *)key UTF8String]; | ||
const char *utf8_value = [(NSString *)value UTF8String]; | ||
int utf8_key_length = strlen(utf8_key); | ||
int utf8_value_length = strlen(utf8_value); | ||
char *key_value_pair; | ||
|
||
key_value_pair = malloc(utf8_key_length + utf8_value_length + 2); | ||
if (key_value_pair != NULL) | ||
{ | ||
strcpy(key_value_pair, utf8_key); | ||
key_value_pair[utf8_key_length] = '='; | ||
strcpy(key_value_pair + utf8_key_length + 1, utf8_value); | ||
} | ||
else | ||
{ | ||
// In case of failed allocation add pointer to preallocated entry. This is | ||
// ignored on the managed side and skipped over in SystemNative_FreeEnviron. | ||
key_value_pair = empty_key_value_pair; | ||
} | ||
|
||
**temp_environ_ptr = key_value_pair; | ||
(*temp_environ_ptr)++; | ||
} | ||
|
||
char** SystemNative_GetEnviron() | ||
{ | ||
char **temp_environ; | ||
char **temp_environ_ptr; | ||
|
||
CFDictionaryRef environment = (CFDictionaryRef)[[NSProcessInfo processInfo] environment]; | ||
int count = CFDictionaryGetCount(environment); | ||
temp_environ = (char **)malloc((count + 1) * sizeof(char *)); | ||
if (temp_environ != NULL) | ||
{ | ||
temp_environ_ptr = temp_environ; | ||
CFDictionaryApplyFunction(environment, get_environ_helper, &temp_environ_ptr); | ||
*temp_environ_ptr = NULL; | ||
} | ||
|
||
return temp_environ; | ||
} | ||
|
||
void SystemNative_FreeEnviron(char** environ) | ||
{ | ||
if (environ != NULL) | ||
{ | ||
for (char** environ_ptr = environ; *environ_ptr != NULL; environ_ptr++) | ||
{ | ||
if (*environ_ptr != empty_key_value_pair) | ||
{ | ||
free(*environ_ptr); | ||
} | ||
} | ||
|
||
free(environ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.