Hi Scott,
Let's take a closer look at the way you're sending the commands to sftp.
Here's the relevant code:
D cmdtext S 100a varying
cmdtext = 'cd import' + EOL;
callp write(stdin(INPUT_END): %addr(cmdtext): %len(cmdtext));
The important thing to understand is that a field declared with VARYING
in RPG is stored in memory as a 2-byte binary integer that indicates the
length of the string, followed by the actual data. So, assuming that
EOL contains x'25' (Unix newline) your cmdtext variable is 10 bytes
long, and therefore will contain:
x'000a' + 'cd import' + x'25'
When you do the write(), the first two bytes you write to QShell are
x'00', which in the C language is used to terminate a string. SFTP was
written in C. So when it receives x'00' in the first byte, it says
"that's it... that's the end of the string" and doesn't read far enough
to see the "cd import" that you put later in the data.
Also, since the %len() BIF doesn't include these two extra bytes when it
returns the length, you'll actually stop two bytes short of writing your
entire command (which will mean that the EOL gets stripped off, and
therefore SFTP never sees the end of the line)
The fix is easy. Just tell the write() API to start writing two bytes
LATER in memory than the address of your cmdtext variable. How do you
tell it two bytes later? By adding two to %addr().
callp write(stdin(INPUT_END): %addr(cmdtext)+2: %len(cmdtext));
Just adding that +2 should solve your problem. (but, do it everywhere
you call write() on a VARYING field...) Or, perhaps, use a named
constant like this to make it clearer:
D VARPREFLEN C const(2)
callp write( stdin(INPUT_END)
: %addr(cmdtext) + VARPREFLEN
: %len(cmdtext) );
Hopefully you get the idea.
The "callp write" lines execute but SFTP does not execute any of the
commands I am trying to send it.
Hopefully you now see why that is...
Also, if I don't close the input side of the stdin pipe after sending
all my commands, the program will just hang at the fgets line for cout.
The fgets() loop for "cout" (the stdout of the sftp command) will
continue to run until the sftp program terminates. The sftp program
won't terminate if you don't close stdin, because it'll be expecting you
to send more commands. Closing stdin tells it that you're done... and
therefore, it ends and closes it's stdout...
As an Amazon Associate we earn from qualifying purchases.