PROGRAM 1 - Call this program with two params - First parm 250 characters, which is the IFS file name. Second parm is 32767 characters which is the return (The file)
******************************************************************
* This program is an example of using AS/400 Unix-style APIs
* in an ILE-RPG program.
*
* When you compile this program, do not take the default activationg group
* Specify QC2LE as the binding directory and QSYS as the library.
*
D/COPY HTMLSRC/QRPGLESRC,PROTOTYPES
******************************************************************
* END OF WHAT SHOULD BE IN A RPG INCLUDE FILE
*
*********************************************************************
*
*********************************************************************
* RC is used to store the Return Code of opening the file
* BR is used to store the Return Code of reading the file
*********************************************************************
DRC S 10I 0
DBR S 10I 0
*********************************************************************
* FileNam is the name of the file in the IFS namespace. You
* can change this to be whatever you want. Your life will be
* a lot easier if you make sure it starts with a slash '/'.
*********************************************************************
D*FileNam S 250A INZ('file+
D* name')
DFileNam S 250A INZ(*blanks)
D
DFileNamP S * INZ(%ADDR(FileNam))
*********************************************************************
* FileDescr is the File descriptor that gets assigned by the
* open API and gets used for all of the read, write and close APIS.
*********************************************************************
DFileDescr S 10I 0
*********************************************************************
* The following comments explain how the various numeric fields
* were assigned for the open API. The values mentioned here are
* from the FCNTL member of the H file in the QCLE library.
*********************************************************************
* The following are for the 'oflag' parameter:
*
* define O_CREAT 0x0000008 /* Create the file if not there
* define O_TRUNC 0x0000040 /* Clear file if it is there
* define O_WRONLY 0x0000002 /* Open for writing only
* define O_RDONLY 0x0000001 /* Open for reading only
* define O_TEXTDATA 0x1000000 /* Translate ebcidic/ascii
* define O_CODEPAGE 0x0800000 /* Create file in ascii ccsid
*
*********************************************************************
DO_CREAT S 10I 0 INZ(8)
DO_RDWR S 10I 0 INZ(4)
DO_RDONLY S 10I 0 INZ(1)
DO_TEXTDATA S 10I 0 INZ(16777216)
DO_CODEPAGE S 10I 0 INZ(8388608)
Doflag S 10I 0 INZ(0)
*
Drcvvar S 1256
Drcvvarsiz S 10u 0 inz(1256)
*********************************************************************
* The mode parameter for the open API is set to give access to
* all users. 0x01B6 = rw-w-w-data rights
*********************************************************************
D omode S 10U 0 INZ(438)
*
*********************************************************************
* cp is used to set the code page (CCSID) of the IFS file to be
* a common US English ASCII. Others code be substituted as
* desired.
*********************************************************************
* ASCII (ccsid 437 = 0x1B5)
D cp S 10U 0 INZ(37)
*
*********************************************************************
* The following fields are used to help format the string for
* formatting and writing...
*********************************************************************
*
D ZeroBin S 250A INZ(*ALLX'00')
D NLZero S 2A INZ(X'1500')
*
*********************************************************************
* SI_Fmt is used to hold the format that you want to put in your
* ascii file. This follows the format for the C function called
* printf. So if you are unfamiliar with it, check out a C book
* for further details. The quick tutorial is as follows:
* %d means put a SIGNED number here. (ex. -123 or 456)
* , the comma is used to delimit the fields
* %s means to put a string or name here.
*********************************************************************
*
D SI_Fmt S 50A INZ('%d, %s')
D SI_FmtP S * INZ(%ADDR(SI_Fmt))
*
*********************************************************************
* num is where I will put some numeric data. Note that the EVAL
* statement below will take care of unpacking the data from the
* DB file. The SI_Fmt above takes care of putting the '-' sign
* in the right place automatically for us||
*********************************************************************
*
*********************************************************************
* num is where I will put some numeric data. Note that the EVAL
* statement below will take care of unpacking the data from the
* DB file. The SI_Fmt above takes care of putting the '-' sign
* in the right place automatically for us||
*********************************************************************
*
Dnum_ds DS
D num_Hex 4A INZ(X'00000000')
D num 10I 0 OVERLAY(num_Hex)
*
C *Entry Plist
C parm FileNamIn 250
C parm OutPut 32767
C EVAL Output=*blanks
c* UP:LO XLATE FileNamIn FileNam
C EVAL FileNam=FileNamIn
C EVAL FileNam=%TRIM(FileNam) + ZeroBin
*********************************************************************
* 3. Use the open API to open the file with NEW oflag values.
* These will handle the ebcidic to ascii translation for us.
*********************************************************************
C Z-ADD O_RDONLY oflag
C ADD O_TEXTDATA oflag
C ADD O_CodePage oflag
C EVAL FileDescr=open(FileNamP:Oflag:Omode:cp)
*
C IF FileDescr=-1
C EVAL RC = perror(FileNamP)
C return
C ENDIF
**********
* If BR <= 0 then we read no bytes, if RC = -1 then file error
*
C DOU BR <= 0 or FileDescr = -1
C EVAL RCVVAR = *blanks
C EVAL BR=read(FileDescr:%addr(rcvvar): rcvvarsiz)
C cat RcvVar:0 Output
C ENDDO
** might want to close the file here??
C SETON LR
Program 2 - This is the include program, PROTOTYPES
******************************************************************
* This program is an example of using AS/400 Unix-style APIs
* in an ILE-RPG program.
*
* When you compile this program, do not take the default activationg group
* Specify QC2LE as the binding directory and QSYS as the library.
******************************************************************
*********************************************************************
*
* START OF WHAT SHOULD BE IN A RPG INCLUDE FILE
*
*********************************************************************
*********************************************************************
*
* (from member STDIO, file H, library QSYSINC)
*
* QBFC_EXTERN void perror(const char *);
*
*********************************************************************
*********************************************************************
*
perror PR 10I 0 EXTPROC('perror')
* string to be filled
* VALUE
*********************************************************************
*
* (from member STDIO, file H, library QSYSINC)
*
* QBFC_EXTERN int sprintf(const char *, const char *, ...);
*
*********************************************************************
* value returned = 0 (OK), -1 (error)
Dsprintf PR 10I 0 EXTPROC('write')
* string to be filled
D * VALUE
* format string
D * VALUE
*
D 10I 0 VALUE OPTIONS(*NOPASS)
*
D * VALUE OPTIONS(*NOPASS)
*********************************************************************
*********************************************************************
*
* (from member FCNTL, file H, library QSYSINC)
*
* QBFC_EXTERN int open(const char *, int, ...);
*
*********************************************************************
* value returned = file descriptor (OK), -1 (error)
Dopen PR 10I 0 EXTPROC('open')
* path to be opened.
D * VALUE
* Open flags
D 10I 0 VALUE
* (OPTIONAL) mode (describes access rights to the file)
D 10U 0 VALUE OPTIONS(*NOPASS)
* (OPTIONAL) codepage (specified for ascii data)
D 10U 0 VALUE OPTIONS(*NOPASS)
*********************************************************************
*********************************************************************
*
* (from member UNISTD, file H, library QSYSINC)
*
* QBFC_EXTERN ssize_t read(int, void *, size_t);
*
*********************************************************************
* value returned = number of bytes actually read, or -1
Dread PR 10I 0 EXTPROC('read')
* file descriptor returned from open()
D 10I 0 VALUE
* data received
D * VALUE
* number of bytes to read
D 10U 0 VALUE
*********************************************************************
*********************************************************************
*
* (from member UNISTD, file H, library QSYSINC)
*
*
* QBFC_EXTERN ssize_t write(int, const void *, size_t);
*
* Definition of type ssize_t
* (from member TYPES, file SYS, library QSYSINC)
*
* typedef int ssize_t;
*
* Definition of type size_t
* (from member TYPES, file SYS, library QSYSINC)
*
* typedef unsigned int size_t;
*
*********************************************************************
* value returned = number of bytes actually written, or -1
Dwrite PR 10I 0 EXTPROC('write')
* file descriptor returned from open()
D 10I 0 VALUE
* data to be written
D * VALUE
* number of bytes to write
D 10U 0 VALUE
*********************************************************************
*********************************************************************
*
*
* (from member UNISTD, file H, library QSYSINC)
*
* QBFC_EXTERN int close(int);
*
*********************************************************************
* value returned = 0 (OK), or -1
Dclose PR 10I 0 EXTPROC('close')
* file descriptor returned from open()
D 10I 0 VALUE
*********************************************************************
*********************************************************************
Dreade PR 10I 0 EXTPROC('read')
* file descriptor returned from open()
D 10I 0 VALUE
* data to be read
D * VALUE
D 10u 0 VALUE
*********************************************************************
*********************************************************************
*
* END OF WHAT SHOULD BE IN A RPG INCLUDE FILE
*
*********************************************************************