C.php

From Organic Design wiki
Revision as of 09:12, 25 June 2006 by Nad (talk | contribs) (allow compiling of java source too)

<?

  1. C compiler for XmlWiki Environment
  2. Licensed under LGPL: www.gnu.org/copyleft/lesser.html

if ($GLOBALS['xwIsAdmin']) { xwSetProperty($properties,'language','TEXT'); $server = $_SERVER['HTTP_HOST']; $htdocs = $_SERVER['DOCUMENT_ROOT']; if (!ereg( "\\/$", $htdocs)) $htdocs .= '/';

$src = $htdocs.'wiki/source.c'; $gcc = '/usr/bin/gcc'; // diagnostics please if (ereg("\\.java$", $title)) $gcc .= ' -x java'; $out = $htdocs.'wiki/a.out'; $stdout = $htdocs.'wiki/a.stdout'; $stderr = $htdocs.'wiki/a.stderr';

// Write the source to tmp file source if ($handle = fopen($src, 'w+')) { fwrite($handle, "$article\n"); fclose($handle); }

// Compile & excecute $article = "Compiling using $gcc\n"; $now = microtime(); shell_exec("$gcc $src 1>$stdout 2>$stderr"); $article .= "Compilation took " . (microtime() - $now) . " usec\n"; $article .= file_get_contents($stdout).file_get_contents($stderr); $article .= "\n\nExecuting compiled result:\n"; $now = microtime(); $article .= shell_exec("$out 2>&1"); $article .= "Execution took " . (microtime() - $now) . " usec\n"; } else xwMessage('Permission denied: Only admin can compile and execute C articles', 'red'); ?>