I was making an online judge and ran into bit of a problem
What I've done till now?
So my php code takes user code and gives it to a function Compile() which compiles the code and reports back whether the compilation has been successful or not. This part of the code works pretty well.
Now the things left are running the code and evaluation
My problem
My problem is how to handle stdin inputs for user. User generally takes input from stdin using scanf, BufferedReader etc and these values are generally taken using keyboard. Now supposing that i have written those values in a file. How will i feed them these value.
My Attempts
Well I was searching for various ways and i came across this
fopen('php://stdin', 'w')
If i believe that this works like a file than wouldn't it cause problem if multiple users use stdin at the sametime.
If you have the compiled code and knows the language of it, it is a little simpler.
You can use the exec function to execute the code, and you can use as a command, something like this (for a c program, and tested):
$output = array();
exec("./main < sample_input.txt", $output);
And if you now inspect the $output var, it has an entry in the array for each line outputted.
Hope this helps.
Exactly, exec is all what you need in PHP.
However, as long as you need not only running but also evaluating, you should build some shell script to run, feed, receive output and check if output is correct. Remember to properly sandbox that script because it's security threat.
Exec btw. offers also third parameter that is return value. It is very useful as long as you build your own shell script with its own return codes.
exec ('./script.sh',$output,$exit_code)
Remember that php script must have permisions to files and directories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With