Executing Commands with system() Function

It’s common to use the QCMDEXC or QCAPCMD API when you want to execute a CL command from an RPG program. But you may find it more convenient to use a C runtime library function, system(), to accomplish the same purpose. The system() function will pass a command string to the command processor, without the need to pass the length of the command string, or any other parameters for that matter.

To call the system() function, you simply pass it a pointer to the command string. Here’s the suggested prototype (along with some necessary H-specs):

 /If Defined(*Crtbndrpg) 
H Dftactgrp(*No) 
 /Endif
H Bnddir('QC2LE')
  // ------------------------------------------------------- Prototypes 
D GoCmd           PR            10I 0 Extproc('system') 
D   CmdString                     *   Value 
D                                     Options(*String) 

When it comes time in your program to execute a command, you can refer to the prototype. The command string may be a a variable, literal, named constant, or an expression. The following example shows a typical use:

 /If Defined(*Crtbndrpg) 
H Dftactgrp(*No) 
 /Endif 
H Bnddir('QC2LE') 
  // ------------------------------------------------------- Prototypes 
D GoCmd           PR            10I 0 Extproc('system') 
D   CmdString                     *   Value 
D                                     Options(*String) 

D NullString      C                   -1 
D Success         C                   0 

D Returncode      S             10I 0 
D User            S             10    Inz(*User) Varying 

 /Free
   Returncode = Gocmd('WRKSPLF SELECT(' + User + ') OUTPUT(*PRINT)');

   Select;
     When Returncode = Success;          // Command was successful
       ...
     When Returncode = NullString;       // Command string was null
       ...
     Other;                              // Command failed       ...
   Endsl;
 /End-free 

The return code will let you check for the success or failure of the system() function. The return code is zero if the command is successful, or 1 if the command fails. If you pass a null pointer to a string, system() returns -1, and the command processor is not called.

If the system() function fails (i.e., return code is 1), it sets a global variable _EXCP_MSGID with the CPF message ID. You can import this variable into your program to check for specific errors, as the following example shows:

 /If Defined(*Crtbndrpg) 
H Dftactgrp(*No) 
 /Endif 
H Bnddir('QC2LE')

  // ------------------------------------------------------- Prototypes 
D GoCmd           PR            10I 0 Extproc('system') 
D   CmdString                     *   Value 
D                                     Options(*String) 

D NullString      C                   -1 
D Success         
D ObjectNotFound  C                   'CPF3142' 
D ObjectInUse     C                   'CPF3156' 

D Errmsgid        S              7    Import('_EXCP_MSGID') 
D Returncode      S             10I 0

 /Free
   Returncode = Gocmd('DLTF MYLIB/MYFILE');
   Select;
     When Returncode = Success;          // Command was successful
       ...
     When Returncode = NullString;       // Command string was null
       ...
     When Errmsgid = ObjectNotFound;     // CPF3142
       ...
     When ErrMsgid = ObjectInUse;        // CPF3156
       ...
     Other;                              // Some other error
       ...   Endsl;
 /End-free 

To use the system() function, you must refer to binding directory QC2LE when compiling and/or binding the program. The above examples name QC2LE in the H-specs.