I want to create an online compiler of C/C++.
Till now I have developed the following code:
<?php
error_reporting(E_ALL);
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
compile();
}
function compile()
{
$a=shell_exec('gcc -o Compile Compile.c');
echo $a;
$b=shell_exec('./Compile');
echo $b;
}
?>
The file Compile.c is getting uploaded and after that compiled by gcc. What I want to do is:
- Read error from stderr when compilation results in error and display on webpage.
- If no error then execute the code on an input file and display time of execution, if time exceeds a particular value, then show time limit exceeded error.
I searched internet and found that if compilation statement is appended by "2>&1" as
$a=shell_exec('gcc -o Compile Compile.c 2>&1');
Then the output of compilation error is returned to assigned variable($a in above case), but not without it. So my problem is how to check for error and then display it on webpage without appending "2>&1" and if no error, then carry out second step given above .