|
1 | 1 | using System; |
2 | 2 | using System.Diagnostics; |
| 3 | +using System.IO; |
3 | 4 | using System.Runtime.InteropServices; |
4 | 5 | using GitCredentialManager.Interop.Posix.Native; |
5 | 6 |
|
@@ -175,6 +176,63 @@ public static bool IsElevatedUser() |
175 | 176 | return false; |
176 | 177 | } |
177 | 178 |
|
| 179 | + #region Platform argv[0] Utils |
| 180 | + |
| 181 | + public static string GetNativeArgv0() |
| 182 | + { |
| 183 | + try |
| 184 | + { |
| 185 | + if (IsWindows()) |
| 186 | + { |
| 187 | + return GetWindowsArgv0(); |
| 188 | + } |
| 189 | + |
| 190 | + if (IsMacOS()) |
| 191 | + { |
| 192 | + return GetMacOSArgv0(); |
| 193 | + } |
| 194 | + |
| 195 | + if (IsLinux()) |
| 196 | + { |
| 197 | + return GetLinuxArgv0(); |
| 198 | + } |
| 199 | + } |
| 200 | + catch |
| 201 | + { |
| 202 | + // If there are any issues getting the native argv[0] |
| 203 | + // we should not throw, and certainly not crash! |
| 204 | + // Just return null instead. |
| 205 | + } |
| 206 | + |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + private static string GetLinuxArgv0() |
| 211 | + { |
| 212 | + string cmdline = File.ReadAllText("/proc/self/cmdline"); |
| 213 | + return cmdline.Split('\0')[0]; |
| 214 | + } |
| 215 | + |
| 216 | + private static string GetMacOSArgv0() |
| 217 | + { |
| 218 | + IntPtr ptr = Interop.MacOS.Native.LibC._NSGetArgv(); |
| 219 | + IntPtr argvPtr = Marshal.ReadIntPtr(ptr); |
| 220 | + IntPtr argv0Ptr = Marshal.ReadIntPtr(argvPtr); |
| 221 | + return Marshal.PtrToStringAnsi(argv0Ptr); |
| 222 | + } |
| 223 | + |
| 224 | + private static string GetWindowsArgv0() |
| 225 | + { |
| 226 | + IntPtr argvPtr = Interop.Windows.Native.Shell32.CommandLineToArgvW( |
| 227 | + Interop.Windows.Native.Kernel32.GetCommandLine(), out _); |
| 228 | + IntPtr argv0Ptr = Marshal.ReadIntPtr(argvPtr); |
| 229 | + string argv0 = Marshal.PtrToStringAuto(argv0Ptr); |
| 230 | + Interop.Windows.Native.Kernel32.LocalFree(argvPtr); |
| 231 | + return argv0; |
| 232 | + } |
| 233 | + |
| 234 | + #endregion |
| 235 | + |
178 | 236 | #region Platform information helper methods |
179 | 237 |
|
180 | 238 | private static string GetOSType() |
|
0 commit comments