Search code examples
cibm-midrange

call c program from other c program in ILE C (AS/400 C)


I need to call a compiled c program, from a c program in AS/400 passing arguments by reference. I began by trying this code in as/400 that should call a program:

#include <unistd.h>
 
int main(void) {
  char *programName = "ls";
  char *arg1 = "-lh";
  char *arg2 = "/home";
 
  execlp(programName, programName, arg1, arg2, NULL);
 
  return 0;
}

which works in linux, but as400 doesn't recognize "execlp".

In COBOL I can easily call a C compiled program passing arguments by reference, which I need, like this:

 CALL "MYPROGRAM" USING BY REFERENCE ARG1, ARG2.

I thought of using system(), but I could not pass a variable by reference. Any ideas?


Solution

  • use #pragma to define the program name as uppercase and set the linkage to OS

    #include <decimal.h>  
    
    // -------------------- qcmdexc -------------------------------
    #pragma map( qcmdexc, "QCMDEXC" )                              
    #pragma linkage( qcmdexc, OS )                                 
    void qcmdexc( char[], decimal(15,5)) ;                              
    
    decimal(15,5) x = 30d ;    
    char        cmds[256] ;  
    
    // dlyjob 5 seconds                                        
    strcpy( cmds, "dlyjob dly(5)                         " ) ; 
    qcmdexc( cmds, x ) ;
    

    should not matter that the program you are calling is a C program. All ILE programs have the same interface.