I am making a code editor for my website, which should take a user's code, compile and run it, and give the user a success or a failure by matching the code's output with the correct predetermined output.
Let's say I have a file editor.php
:
<?php echo `dir` ?>
this displays the contents of my directory perfectly on the webpage:
Volume in drive C is OS Volume Serial Number is D239-CB9C Directory of C:\Users\Chander Shekhar\OneDrive\Documents\Projects\PHPProject\AlgoLand\campaign\code 05/03/2020 07:35 PM
. 05/03/2020 07:35 PM
.. 03/15/2020 09:45 PM 4,068 color-mode.png 05/03/2020 07:06 PM 4,060 editor.css 05/03/2020 04:49 PM 3,709 editor.js 05/03/2020 07:35 PM 19 editor.php 05/03/2020 07:04 PM 2,415 l1q1.php 05/03/2020 07:34 PM 86 sample.cpp 6 File(s) 14,357 bytes 2 Dir(s) 242,218,971,136 bytes free
In more readable form (note the sample.cpp):
Volume in drive C is OS
Volume Serial Number is D239-CB9C
Directory of C:\Users\Chander Shekhar\OneDrive\Documents\Projects\PHPProject\AlgoLand\campaign\code
05/03/2020 07:35 PM <DIR> .
05/03/2020 07:35 PM <DIR> ..
03/15/2020 09:45 PM 4,068 color-mode.png
05/03/2020 07:06 PM 4,060 editor.css
05/03/2020 04:49 PM 3,709 editor.js
05/03/2020 07:35 PM 55 editor.php
05/03/2020 07:04 PM 2,415 l1q1.php
05/03/2020 07:34 PM 86 sample.cpp
6 File(s) 14,393 bytes
2 Dir(s) 242,215,878,656 bytes free
However,
<?php echo `g++ -std=c++14 sample.cpp -o sample.exe` ?>
does not show me any error.
Infact, even a:
<?php echo `g++` ?>
does not give me any output.
The contents of sample.cpp
is:
#include <iostream>
which should output:
C:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
when compiled at the command line. What gives?
I can also run other programs.
If I change sample.cpp
to:
#include <iostream>
int main(){
std::cout << "Hello World!";
return 0;
}
and change editor.php
to:
<?php echo `sample.exe` ?>
This gives the proper output:
Hello World!