Let's say we wanted to write a program to read a file and dump it as formatted hex - using the C functions fopen and fread.
The code for this program is straightforward:
filedump:
proc(fn) options(noexecops main);
dcl fn char(*) var;
%include filedump;
file = fopen( fn, 'rb' );
if file = sysnull() then
do;
display( 'file could not be opened' );
return;
end;
do forever;
unspec(buffer) = ''b;
read_In = fread( addr(buffer), 1, stg(buffer), file );
if read_In = 0 then
leave;
display( heximage(addr(buffer),16,' ') || ' '
|| translate(buffer,(32)'.',unprintable) );
if read_In < stg(buffer) then
leave;
end;
call fclose( file );
end filedump;Most of the declarations in the INCLUDE file filedump are obvious:
define struct 1 file;
define alias file_Handle handle file;
define alias size_t unsigned fixed bin(32);
define alias int signed fixed bin(31);
dcl file type(file_Handle);
dcl read_In fixed bin(31);
dcl buffer char(16);
dcl unprintable char(32) value( substr(collate(),1,32) );