Reputation: 349
I am using Micro Focus COBOL on Windows system and have this code
.
.
01 w-param pic x(100) value 'dir > out.txt'.
.
.
call "SYSTEM" using w-param
Then I have to read out.txt file to get output from dir command. Is there any way to get output from dir (or any other system command) directly to my program as returning value?
Upvotes: 1
Views: 4876
Reputation: 7297
Answer - Part 1: NO, you can not have any output of the CALL in the return value.
Answer - Part 2: But you can get the output with a different option.
Explanation for part 1:
"return-values" are normally an integer, passed via the special register RETURN-CODE
and/or with the RETURNING
clause (in some implementations allowing you to additional get a pointer): CALL someprog RETURNING something
, see the COBOL documentation for CALL.
Explanation for part 2: What you may can do is to create a pipe and read from there, either with calling the C interface or with an implementor-specific extension.
With ACUCOBOL or MicroFocus [and maybe others] you can use pipes via line sequential file access - see the answer of Stephen Gennard for details on MicroFocus and this answer's end for ACUCOBOL.
On unix you can "natively" create a named pipe with CALL 'SYSTEM'
, a possible way of reading from there is documented in the GnuCOBOL FAQ -> named pipes, in general you can create a pipe and read from it via C interface.
A sample implementation for using pipes via C interface and a COBOL wrapper for it is cobweb-pipes (MF likely supports user defined functions, therefore the cobweb-pipes.cob likely works more or less unchanged (if you test this please answer with a comment), otherwise the cobweb-call-pipes.cob is very likely to work.
Edit:
Extension in ACUCOBOL via SEQUENTIAL
files and starting with a -P
in the assigned filename:
program-id. dircmdread.
select i-cmd
* windows:
assign to "-P %TMP% cmd.exe /c dir > %TMP%"
* unix:
* assign to "-P ls -l"
organization is sequential.
fd i-cmd.
01 i-cmd-record pic x(80).
procedure division.
open input i-cmd
perform until exit
read i-cmd
at end
exit perform
end-read
display i-cmd-record
end-perform
close i-cmd
goback.
Upvotes: 3
Reputation: 1910
Answer: YES (if you are using Micro Focus COBOL)
You can do it by using pipes via COBOL syntax
For example:
program-id. dircmdread.
select i-cmd
assign to "< cmd.exe /c dir"
organization is line sequential.
fd i-cmd.
01 i-cmd-record pic x(80).
procedure division.
open input i-cmd
perform until exit
read i-cmd
at end
exit perform
end-read
display i-cmd-record
end-perform
close i-cmd
goback.
end program dircmdread.
and execute it via:
Y:\DemoAndTests\dirread>cobol dircmdread.cbl nologo int();
* Generating dircmdread
* Data: 1048 Code: 736 Literals: 424
Y:\DemoAndTests\dirread>run dircmdread
Volume in drive Y is UserSourceCode
Volume Serial Number is EE5F-1112
Directory of Y:\DemoAndTests\dirread
29/09/2016 15:13 <DIR> .
29/09/2016 15:13 <DIR> ..
29/09/2016 15:16 509 dircmdread.cbl
29/09/2016 15:17 2,560 dircmdread.int
29/09/2016 15:17 2,330 dircmdread.obj
3 File(s) 5,399 bytes
2 Dir(s) 20,383,764,480 bytes free
Upvotes: 2