2

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!

1 Answer 1

4

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.