diff --git a/README.md b/README.md index 6501b18..1fd76ac 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To run sshpass, you must install: # Usage ```sh -Usage: sshpass [ options ] command +Usage: sshpass [ options ] command arguments -h, --help show this help message and exit @@ -29,5 +29,5 @@ Other options: # Examples ```sh -sshpass.exe -p 12345 "ssh xhcoding@192.168.139.128 ls" +sshpass.exe -p 12345 ssh xhcoding@192.168.139.128 ls ``` diff --git a/main.c b/main.c index 3d0dd23..0f784c8 100644 --- a/main.c +++ b/main.c @@ -3,12 +3,13 @@ #include #include #include +#include #include #include "argparse.h" static const char* const usages[] = { - "sshpass [ options ] command", + "sshpass [options] command arguments", NULL, }; @@ -23,7 +24,7 @@ typedef struct { const char* passPrompt; int verbose; - const char* cmd; + char* cmd; } Args; typedef struct { @@ -49,13 +50,6 @@ int main(int argc, const char* argv[]) { ParseArgs(argc, argv, &ctx); - if (ctx.args.verbose) { - fprintf(stdout, "cmd: %s\n", ctx.args.cmd); - if (ctx.args.pwtype == PWT_PASS) { - fprintf(stdout, "password: |%s|\n", ctx.args.pwsrc.password); - } - } - HRESULT hr = E_UNEXPECTED; ctx.pipeIn = INVALID_HANDLE_VALUE; @@ -144,7 +138,7 @@ static void ParseArgs(int argc, const char* argv[], Context* ctx) { }; struct argparse argparse; - argparse_init(&argparse, options, usages, 0); + argparse_init(&argparse, options, usages, ARGPARSE_STOP_AT_NON_OPTION); argc = argparse_parse(&argparse, argc, argv); if (argc == 0) { argparse_usage(&argparse); @@ -174,7 +168,21 @@ static void ParseArgs(int argc, const char* argv[], Context* ctx) { ctx->args.passPrompt = "password:"; } - ctx->args.cmd = argv[0]; + int cmdLen = 0; + for (int i = 0; i < argc; i++) { + cmdLen += strlen(argv[i]) + 2; + } + + ctx->args.cmd = malloc(sizeof(char) * cmdLen); + memset(ctx->args.cmd, 0, sizeof(char) * cmdLen); + for (int i = 0; i < argc; i++) { + StringCchCatA(ctx->args.cmd, sizeof(char) * cmdLen, argv[i]); + StringCchCatA(ctx->args.cmd, sizeof(char) * cmdLen, " "); + } + + if (ctx->args.verbose) { + fprintf(stdout, "cmd: %s\n", ctx->args.cmd); + } } static HRESULT CreatePseudoConsoleAndPipes(HPCON* hpcon, Context* ctx) {