Varun Gupta
Varun Gupta

Reputation: 27

Handing user input on terminal for application launched via exec in C++

xfreerdp asks for password if /p is not supplied as command line argument; when launched via terminal.

But when it is launched via execvp or exec, there is no prompt?

How to show this prompt? Is there a way where I can directly input password on prompt programmatically?

Same is automatically handled in Mac using swift using tasks & pipes. How to do it in C++.

Upvotes: 0

Views: 205

Answers (1)

David Collins
David Collins

Reputation: 3022

Is there a way where I can directly input password on prompt programmatically?

An example (written in C) using popen() ...

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    const char *cmd = "xfreerdp";
    char output[128] = {'\0'};
    const char *arg = "myargs";    

    // Open process
    FILE *fp = popen(cmd, "w");
    if (!fp) {
        fprintf(stderr, "Could not execute command ...\n");
        exit(EXIT_FAILURE);
    }

    // Pass arguments
    if (fprintf(fp, "%s", arg) < 0) {
        puts("Could not pass arguments ...");
    }

    // Print command output (if required)
    while (fgets(output, sizeof(output), fp) != NULL) {
        puts(output);
    }

    pclose(fp);

    return 0;
}

Upvotes: 1

Related Questions