RaGa__M
RaGa__M

Reputation: 2571

How to bypass y/n questions in cmd while using popen()

Today I have been facing a critical bug in my program, it is just a tiny code that uses popen() to pass the commands and pipe its results to a file descriptor where I use fgets() to read the result, The problem for date command my program is been hanging(waiting) and the reason is as we know after issuing date it will show the current date and issues a yes or no question like the below.

The current date is: Fri 08/21/2015
Enter the new date: (mm-dd-yy)----this was shitting my code!!!!

I just want my program to skip this questionnaire.

Note: the string "command" in the below code is what I will receive from the user.

FILE *in;
char buff[512];
string cmd;
command += " 2>&1";
if (!(in = popen(command.c_str(), "r"))) {
    status = "0"; // my logic don't bother:)
}
else {
    while (fgets(buff, sizeof(buff), in) != NULL) {
        cmd += buff;
    }
}

Upvotes: 0

Views: 227

Answers (1)

RaGa__M
RaGa__M

Reputation: 2571

Thanks to Petesh.

I have changed to 2>&1<nul instead of 2>&1, the program isn't hanging now, The key to this solution is adding <nul gives some default value as an answer to the prompts.

Upvotes: 1

Related Questions