[C++] #BR6 case passes without any code changes

Hi,

I am not sure why but this case passed immediately without any code change from the previous stage (#WT6). Initially I thought it might be because of the readline library but I didn’t find any reference in the documentation that the library provides piping.

Can you point me towards what could possibly be the reason behind this?

You don’t mention your language or how you are running the commands? Seen that quite some people just offload process running during that stage to the int system(const char *string) function instead of doing their own process launching. The entire challenge builds a shell that resembles Bash, and since the system shell on the test server is also Bash (or close enough), then when your shell receives cat myfile.txt | wc and if you end up passing it to system like system("cat myfile.txt | wc") then it is likely you will pass the tests.

Hi,
Thank you for your reply. I have mentioned my language in the title. It is C++.
And you are absolutely right in that I am using std::system to run the command. Should I be running the commands in a different way (so that I don’t get the stage for free)?

Silly me, you did indeed mention the language.

I can’t tell how far one can get with (std::)system function, or whether there will come some hard roadblocks up ahead. You have to quote the arguments you just spent time parsing to pass to system, to handle symbols and spaces in file names etc.. I think most passes the tests even with system because the test after command launching do not require much, if any, argument escaping.

And I don’t want to say that it is wrong, and people have their own reason for doing the challenges and what they hope to get out of it. Personally, though I find it antithetical to implement a shell by offloading the actual job to another shell—which is what system does. I don’t think there is a right answer, but I do think the intended answer for Linux is to use the fork, and execve (or one of the other exec, maybe execvp) functions. To switch to these will be an initial hurdle, mostly with setting up redirection/file handles, but there are common idioms for doing this, and plenty of tutorials one can search for. I think posix_spawn handles both some of the nuances of fork/exec, but I don’t know if the testers support that; I suspect they do. And if you do this work would have more control over exactly what happens, and learned how OS process handling actually works.

2 Likes

Thank you for the detailed answer. I think I will go ahead and try implementing the fork/exec combination.