开发者

Online C Compiler [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 1 year ago.

Improve this question

I'm doing my final year project on Software as a Service and "Online C Compiler" is one of the intended services. Please help me how can i call a c compiler like GCC to execute the C code written in the text area of the browser and give back the output which is in tu开发者_运维百科rn displayed on the browser.

Thank you.


Easy! Just run it through one of the many PHP execution functions.

Example code:

// atomic temp file with .c extension
do {
    $tmpfile = tempnam(sys_get_temp_dir(),'source');
}while(!@rename($tmpfile,$tmpfile.'.c'));

$tmpfile.='.c'; // rename succeeded, update file name
$exefile='test.exe'; // works on linux as well, linux ignores extension

file_put_contents($tmpfile,$_REQUEST['c_code']);

// invoke GCC
$output = shell_exec('gcc '.escapeshellarg($tmpfile).' -o '.escapeshellarg($exefile));
// set sticky bit
$output.= shell_exec('sudo +s '.escapeshellarg($exefile)); // I need to set this on my server
// run the created program
$output.= shell_exec(escapeshellarg($exefile));

echo '<pre>'.htmlspecialchars($output,ENT_QUOTES).'</pre>';

The above code (though untested) should work. If you want a more advanced process execution routine (to write STDIN, and read both STDOUT and STDERR as well as get return code):

/**
 * Executes a program and waits for it to finish, taking pipes into account.
 * @param string $cmd Command line to execute, including any arguments.
 * @param string $input Data for standard input.
 * @return array Array of "stdout", "stderr" and "return".
 * @copyright 2011 K2F Framework / Covac Software
 */
function execute($cmd,$stdin=null){
    $proc=proc_open($cmd,array(0=>array('pipe','r'),1=>array('pipe','w'),2=>array('pipe','w')),$pipes);
    fwrite($pipes[0],$stdin);                      fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);        fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);        fclose($pipes[2]);
    $return=proc_close($proc);
    return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}


I don't think you want to be executing arbitrary code outside of some kind of container. While the code should be running underprivileged under most circumstances, you run the risk of:

  • Someone tickling a privilege escalation bug in your kernel (the old vmsplice bug comes to mind)
  • Someone setting up a nasty network service
  • Someone hurling SPAM to the four corners of the Internet (I've never seen them, but I'm pretty sure the Internet does have corners)
  • Someone deliberately interfering with the normal use of your server by executing annoying code.

I would recommend, given those potential issues that you use some kind of virtualization technology to actually run the code. Some candidates would be:

  • QEMU (Quick n dirty)
  • Lguest (Quick n dirty)
  • OpenVZ (A bit more complicated)
  • Xen Paravirtualization (Requires a hypervisor, but ideal for your use)

If your university has access to tools like Simics, you could probably use it instead and offer more comprehensive machine level debugging. I'm not sure, it's been a while since I used Simics.

In those cases, you would make a copy of a bare bones OS, place the uploaded code in it and execute scripts to build, run and collect the output upon boot. This lets you set strict rules on how long code can run, keep the running code isolated and offer builds on more than one architecture. Just direct the build and program output to a file that you retrieve after it executes.

You'll also want to make sure that the build environment supports as many locales as possible.

If you want to get bonus points, make use of Valgrind as well. Still, you'll want to put a limit on the level of build complexity you want to handle... so that you can focus on features that make your system nice for its advertised purpose.

In either event, PHP's exec() / shell_exec() family of calls should be adequate for your front end to run a script that kicks off everything else.


You may find this project useful. It's a gcc REPL (Read Evaluate Print Loop). Wrapping it in a CGI of some sort ought to give you what you want.


You can not execute the C code in the browser, but you can write a cgi program(either in C/C++ or Perl) which can take C code as input and then execute that C code with local gcc compiler. You can call this cgi from browser like any other http link. At last you have to read the response of the cgi and print the response in your browser. To have some idea about CGI have a look at http://httpd.apache.org/docs/1.3/howto/cgi.html and http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/


Since you've tagged this with PHP, see the php man page for the shell_exec function. You could call GCC this way, for example:

$command = "gcc -w -Wall -ansi -pedantic -g -o /path/to/executable /path/to/sourcefile";
$output = shell_exec($command);
echo $output;

EDIT: In case it's not obvious, you would first need to get and sanitise the user input from the text area, write it to a file, and then try to compile it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜