Skip to content

Commit

Permalink
feat: support command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
xhcoding committed Dec 2, 2023
1 parent c2473d3 commit 215dbdf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -29,5 +29,5 @@ Other options:
# Examples
```sh
sshpass.exe -p 12345 "ssh [email protected] ls"
sshpass.exe -p 12345 ssh [email protected] ls
```
30 changes: 19 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strsafe.h>

#include "argparse.h"

static const char* const usages[] = {
"sshpass [ options ] command",
"sshpass [options] command arguments",
NULL,
};

Expand All @@ -23,7 +24,7 @@ typedef struct {
const char* passPrompt;
int verbose;

const char* cmd;
char* cmd;
} Args;

typedef struct {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 215dbdf

Please sign in to comment.