forked from dmbaturin/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceval.pl
executable file
·63 lines (50 loc) · 985 Bytes
/
ceval.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
my $CC = "gcc -x c -lm";
my $TMP_DIR = "/tmp/";
my $template = <<EOL;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
int main(void)
{
__PLACEHOLDER__;
return(0);
}
EOL
if( $#ARGV < 0 )
{
print "I see no point in evaluating empty string.\n";
exit(1);
}
elsif( $#ARGV > 0)
{
print "Too many arguments\n";
exit(1);
}
my $input = $ARGV[0];
# Join timestamp and a random number in hope
# it gives a unique file name
my $file_name = $TMP_DIR . time() . int(rand(1000));
my $src_file_name = $file_name . ".c";
my $code = $template;
$code =~ s/__PLACEHOLDER__/$input/;
open( HANDLE, ">$src_file_name" );
print HANDLE $code;
close( HANDLE );
my $cc_output = `$CC -o $file_name $src_file_name`;
unlink($src_file_name);
print "$cc_output";
if( $? == 0 )
{
my $output = `$file_name`;
print "$output\n";
unlink($file_name);
exit(0);
}
else
{
exit(1);
}