-
-
Save trackd/0ed07df0157494ddbe07e7113cd9d2b6 to your computer and use it in GitHub Desktop.
Code that can be used to generate an executable that can print how it receives arguments
This file contains hidden or 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
#include<stdio.h> | |
// gcc print_argv.c -o print_argv | |
int main(int argc, char *argv[]) | |
{ | |
int i; | |
for(i = 1;i < argc;i++) | |
{ | |
printf("[%d] %s\n", i, argv[i]); | |
} | |
return 0; | |
} |
This file contains hidden or 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
<# | |
.NOTES | |
wanted to play around and learn a bit.. | |
mine is fairly untested so use the original if you want something more reliable. | |
.EXAMPLE | |
pretty close to the original but with a few minor tweaks. | |
PS > .\print_argsv.exe -c -t -d -q | |
#outputs | |
~\print_argsv.exe -c -t -d -q | |
Pos: 0 Len: 2 Arg: '-c' | |
Pos: 1 Len: 2 Arg: '-t' | |
Pos: 2 Len: 2 Arg: '-d' | |
Pos: 3 Len: 2 Arg: '-q' | |
.EXAMPLE | |
print_argsv2 will print the pipeline input as well as the arguments. | |
I'm not sure how well it works, needs testing.. | |
PS > ping 127.0.0.1 | .\print_argsv2.exe | |
# outputs | |
~\print_argsv2.exe | |
Input (decoded): | |
Pinging 127.0.0.1 with 32 bytes of data: | |
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 | |
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 | |
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 | |
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 | |
Ping statistics for 127.0.0.1: | |
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), | |
Approximate round trip times in milli-seconds: | |
Minimum = 0ms, Maximum = 0ms, Average = 0ms | |
Pos: 2 Len: 40 Arg: 'Pinging 127.0.0.1 with 32 bytes of data:' | |
Pos: 3 Len: 47 Arg: 'Reply from 127.0.0.1: bytes=32 time<1ms TTL=128' | |
Pos: 4 Len: 47 Arg: 'Reply from 127.0.0.1: bytes=32 time<1ms TTL=128' | |
Pos: 5 Len: 47 Arg: 'Reply from 127.0.0.1: bytes=32 time<1ms TTL=128' | |
Pos: 6 Len: 47 Arg: 'Reply from 127.0.0.1: bytes=32 time<1ms TTL=128' | |
Pos: 8 Len: 30 Arg: 'Ping statistics for 127.0.0.1:' | |
Pos: 9 Len: 52 Arg: 'Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),' | |
Pos: 10 Len: 46 Arg: 'Approximate round trip times in milli-seconds:' | |
Pos: 11 Len: 43 Arg: 'Minimum = 0ms, Maximum = 0ms, Average = 0ms' | |
#> | |
Add-Type -OutputType ConsoleApplication -OutputAssembly print_argsv.exe -TypeDefinition @' | |
using System; | |
namespace PrintArguments { | |
class Program { | |
static void Main(string[] args) { | |
string cmdLine = Environment.CommandLine; | |
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; | |
cmdLine = cmdLine.Replace("\"" + exePath + "\"", "~\\" + System.IO.Path.GetFileName(exePath)); | |
Console.WriteLine(cmdLine); | |
for (int i = 0; i < args.Length; i++) { | |
Console.WriteLine("Pos: {0} Len: {1} Arg: '{2}'", i, args[i].Length, args[i]); | |
} | |
} | |
} | |
} | |
'@ | |
Add-Type -OutputType ConsoleApplication -OutputAssembly print_argsv2.exe -TypeDefinition @' | |
using System; | |
using System.IO; | |
using System.Text; | |
namespace PrintArgumentsAndPipeline { | |
class Program { | |
static void Main(string[] args) { | |
string cmdLine = Environment.CommandLine; | |
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; | |
cmdLine = cmdLine.Replace("\"" + exePath + "\"", "~\\" + System.IO.Path.GetFileName(exePath)); | |
Console.WriteLine(cmdLine); | |
if (!Console.IsInputRedirected) { | |
for (int i = 0; i < args.Length; i++) { | |
Console.WriteLine("Pos: {0} Len: {1} Arg: '{2}'", i, args[i].Length, args[i]); | |
} | |
} | |
if (Console.IsInputRedirected) { | |
using (Stream inputStream = Console.OpenStandardInput()) { | |
using (MemoryStream ms = new MemoryStream()) { | |
byte[] buffer = new byte[16 * 1024]; | |
int read; | |
while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0) { | |
ms.Write(buffer, 0, read); | |
} | |
byte[] inputBytes = ms.ToArray(); | |
string inputString = Encoding.UTF8.GetString(inputBytes); | |
// if you want more info about the input, uncomment these lines | |
// Console.WriteLine("Input (bytes):{0}{1}", Environment.NewLine, BitConverter.ToString(inputBytes).Replace("-", " ")); | |
// Console.WriteLine("Input (encoded):{0}{1}", Environment.NewLine, Convert.ToBase64String(Encoding.UTF8.GetBytes(inputString))); | |
Console.WriteLine("Input (decoded):{0}{1}", Environment.NewLine, inputString); | |
using (StringReader reader = new StringReader(inputString)) { | |
string line; | |
int pos = args.Length; | |
while ((line = reader.ReadLine()) != null) { | |
string temp = line.Trim(); | |
pos++; | |
if (temp.Length > 0) { | |
Console.WriteLine("Pos: {0} Len: {1} Arg: '{2}'", pos, temp.Length, temp); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
'@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment