Skip to content
Advertisement

C Shell to execute commands

I have the following code:

JavaScript

The programme works fine, however when I parse something like “ps -f” it retunrs “execvp echoué”, as the code removes the spaces. What should I exactly do to make it accept comands with options such as ps -f?

Advertisement

Answer

You should use fgets() instead of scanf(). Because fgets does not delete white spaces from your input. scanf function only consumes input until the whitespace. For example if you enter “ps -f” scanf consumes this input as “ps”. If you write ps directly to your shell terminal you can face with same exception that is what your programme exception. You can use fgets as stream reader that can be file stream or standard input stream(stdin). I have written code that solves your problem. You can check how you can use.

JavaScript

output

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement