-
Notifications
You must be signed in to change notification settings - Fork 4
/
c.php
executable file
·125 lines (116 loc) · 3.5 KB
/
c.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
session_start();
#
# && isset( $_POST['checkbox'] )
#
#ini_set('memory_limit', '1M');
$prefix = $_SESSION['username'];
$main = "main.c";
$error_file = 'error.txt';
$output_file = 'output.txt';
$input_file = 'input.txt';
$exec_file = './a.out';
function make_files_and_read_data(){
global $main,$output_file,$prefix,$input_file;
//exec("mkdir $prefix");
if (!is_dir($prefix)) {
mkdir($prefix);
}
exec("chmod -R 777 $prefix");
chdir($prefix);
// echo getcwd();
if( isset( $_POST['code'] ) ){
$code = $_POST['code'];
$c = "code.c";
$file_to_save = fopen($c, "w+");
fwrite( $file_to_save, $code );
fclose( $file_to_save);
$f =fopen($main,"w+");
$d = <<<EOD
#include<stdlib.h>
#include<unistd.h>
#define system <stdlib.h>
#define exec <unistd.h>
\n
EOD;
fwrite($f,$d);
fclose($f);
shell_exec("cat $c>>$main");
shell_exec( "chmod 777 $main" );
shell_exec("touch $output_file");
shell_exec( "chmod 777 $output_file" );
shell_exec("touch $input_file");
shell_exec( "chmod 777 $input_file" );
exec("rm $c");
//echo getcwd();
}
}
function c_compile(){
global $main,$error_file,$exec_file,$prefix;
make_files_and_read_data();
$command = "gcc -lm main.c 2> ".$error_file;
shell_exec( $command );
exec("chmod 777 a.out");
if(filesize( $error_file)!=0 ){
echo nl2br(file_get_contents( $error_file ));
}
//echo getcwd();
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
function c_run(){
global $input_file,$output_file,$prefix,$exec_file;
chdir($prefix);
// echo exec("ls -a");
$run = "timeout 5s ./a.out";
$input = fopen( $input_file, "w+" );
if( isset($_POST['in']) ){
fwrite( $input, $_POST['in']);
$run = $run. "<"."$input_file";
}
fclose( $input );
$run = $run.">"."$output_file";
// echo $run;
// echo "<br>";
// echo getcwd();
shell_exec( $run );
//echo filesize($output_file)<=134217728;
if(filesize($output_file)<134217728){
$handle = @fopen($output_file, "r");
$content = fread($handle, 20000);
echo nl2br($content);
fclose($handle);
}
else{
echo '<p style="color:red;text-align:center;font-size:20px;">Time limit exceeded ! </p>'.'<br><br>';
$handle = @fopen($output_file, "r");
$content = fread($handle, 4096);
echo nl2br($content);
fclose($handle);
}
$dir = realpath($prefix);
//echo $dir;
// exec('chmod 777 $prefix');
// rrmdir($dir);
// $path = "./".$prefix;
// exec('chmod 777 $prefix');
// exec('cd $prefix && rm *');
// exec('rmdir $prefix');
// echo $prefix;
// echo shell_exec("pwd");
// echo shell_exec("cd $prefix && pwd");
// exec("rm $input_file");
}
?>