Now that we have translated fread correctly, we might try this translation for fopen:
dcl fopen ext('fopen')
entry( char(*) varyingz byvalue,
char(*) varyingz byvalue )
returns( byvalue type file_handle );
But C really has no strings, only pointers, and these pointers would be passed byvalue; so the strings should be byaddr:
dcl fopen ext('fopen')
entry( char(*) varyingz byaddr,
char(*) varyingz byaddr )
returns( byvalue type file_handle );
But PL/I passes descriptors with strings and C doesn't understand them, so they must be suppressed. We can do this by adding options(nodescriptor) to the declaration:
dcl fopen ext('fopen')
entry( char(*) varyingz byaddr,
char(*) varyingz byaddr )
returns( byvalue type file_handle )
options ( nodescriptor );This will work, but isn't optimal since the parameters are input-only; if the parameter is a constant, the nonassignable attribute will prevent a copy being made and passed. Hence, the best translation of the declaration of fopen is:
dcl fopen ext('fopen')
entry( char(*) varyingz nonasgn byaddr,
char(*) varyingz nonasgn byaddr )
returns( byvalue type file_handle )
options ( nodescriptor );At this point, the declare for the fclose function presents few surprises except perhaps for the optional attribute in the returns specification. This attribute allows us to invoke the fclose function via a CALL statement and not have to dispose of the return code. But please note that if the file were an output file, the return code on fclose should always be checked since the last buffer may be written out only when the file is closed and that write could fail for lack of space.
dcl fclose ext('fclose')
entry( type file_handle byvalue )
returns( optional type int byvalue )
options ( nodescriptor );Now, on z/OS UNIX, we can compile and run the programs with the commands:
pli -qdisplay=std filedump.pli
filedump filedump.pli
This would produce the following output:
15408689 938584A4 94977A40 97999683 . filedump: proc
4D86955D 409697A3 899695A2 4D959685 (fn) options(noe
A7858396 97A24094 8189955D 5E151540 xecops main);..