proc_open problem: \n translates to Windows-style line return \r\n

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Klein

    proc_open problem: \n translates to Windows-style line return \r\n

    Without getting into a lot of unnecessary detail, I'm working on a project
    to allow PHP to communicate with a proprietary database.

    The IPC mechanism I'm using is 'proc_open'; there is a server-side component
    (written in C) that 'listens' for PHP requests (via its stdin) and spits out
    the appropriate response (via its stdout). Conversely, the PHP side uses
    fwrite() to send to the component's stdin and fread() to read from the
    component's stdout.

    Ok so far?

    What happens is that there is a situation where the C component will send a
    a string with an embedded chr(10) via stdout to PHP's stdin. On Windows this
    is getting converted to chr(13).chr(10) .

    So the problem is that if I send a n byte string ( with an embedded chr(10)
    ) then fread() gets an n+1 byte string ( with the addition of the chr(13) ).

    I've already tried using the 'binary_pipes' in the 'other_options' array.

    I've also tried adding a 'b' as a 'pipe' option (see code below) but PHP
    doesn't like it.

    Here's simplified PHP code to illustrate the problem :

    $descriptorspec = array(
    0 =array("pipe", "r"),
    1 =array("pipe", "w"), //tried doing "wb"
    2 =array("pipe", "r"));

    $process = proc_open('C:\s andbox\bin\cpro g.exe', $descriptorspec , $pipes);

    if (is_resource($p rocess)) {
    $rtnval = fread($pipes[1], 1024);

    echo "$rtnval <br />"
    echo strlen($rtnval) ."<br /><br />";

    $a = str_split($rtnv al);
    foreach ($a as $character) {
    echo ord($character) ."<br />";
    }

    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($pro cess);
    }

    If the C program sends the string 'LINE1'.chr(10) .'LINE2' then PHP responds
    with:

    LINE1 LINE2
    12

    76
    73
    78
    69
    49
    13 <---this is being added in by PHP
    10
    76
    73
    78
    69
    50


    Is there some 'switch' that I can use to turn this off? Or am I going to
    have to filter this out myself?

    Daniel Klein
Working...