Reputation: 2035
I'm currently working on an online program. I'm writing a php script that executes a command in the command line with proc_open() (under Linux Ubuntu). This is my code so far:
<?php
$cmd = "./power";
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], "4");
fwrite($pipes[0], "5");
fclose($pipes[0]);
while($pdf_content = fgets($pipes[1]))
{
echo $pdf_content . "<br>";
}
fclose($pipes[1]);
$return_value = proc_close($process);
}
?>
power is a program that asks for input 2 times (it takes a base and an exponent and calculates base ^ exponent). It's written in Assembly. But when I run this script, I get wrong output. My output is "1" but I expect 4^5 as output.
When I Run a program that takes one input, it works (I've tested an easy program that increments the entered value by one).
I think I'm missing something regarding the fwrite command. Could anyone please help me?
Thanks in advance!
Upvotes: 2
Views: 2549
Reputation: 91983
You forgot to write a newline to the pipe, so your program will think that it got only 45
as input. Try this:
fwrite($pipes[0], "4");
fwrite($pipes[0], "\n");
fwrite($pipes[0], "5");
fclose($pipes[0]);
Or shorter:
fwrite($pipes[0], "4\n5");
fclose($pipes[0]);
Upvotes: 4