2

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!
Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21

2 Answers2

1

Compilers likely give output on stderr, and not stdout. The backticks read stdout. You need to use proc_open with the pipes as you can see in the first example at https://www.php.net/manual/en/function.proc-open.php

Derick
  • 35,169
  • 5
  • 76
  • 99
0

After asking around, I finally found another way to solve this issue.

The problem was that, as stated above by @Derick, g++ was outputting to stderr. So, the solution was to redirect this output to stdout.

This can be done by appending 2>&1 to the end of the command, like so:

<?php echo `g++ -std=c++14 sample.cpp -o sample.exe 2>&1` ?>

This command tells the shell to:

1) Take the error stream (2) of g++ and
2) redirect it (>)
3) to the file descriptor of stdout (&1).

Hence, the output is transmitted on the proper channel and received by Execution Operator.

Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21