Skip to content

Instantly share code, notes, and snippets.

@stubbetje
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stubbetje/b45b173ec10a5bc26d4a to your computer and use it in GitHub Desktop.
Save stubbetje/b45b173ec10a5bc26d4a to your computer and use it in GitHub Desktop.
Calling COBOL from C with variable arguments
#include <stdio.h>
#include <stdarg.h>
/*
COBOL will be calling the function like this:
CALL "calling_c" USING BY REFERENCE
"d" & x"00"
"FOO" & x"00" WS-DEPTH-1 *> ws-depth-1 and ws-depth-2 are PIC 9(4) COMP-5
"BAR" & x"00" WS-DEPTH-2
x"00".
*/
#define PRINT( fmt, ... ) \
printf( "%s:%d ==> " fmt "\n", __FUNCTION__ , __LINE__ , ## __VA_ARGS__ )
int calling_c( void * arg );
int calling_c( char * data_type, ... )
{
va_list ap;
char * key;
unsigned short depth;
int i = 0;
PRINT( "Hello from %s", __FUNCTION__ );
PRINT( "data_type = %s", data_type );
va_start( ap, data_type );
do {
key = va_arg( ap, char *);
if( key [0] != 0 ) {
depth = * ( va_arg( ap, unsigned short *) );
PRINT( "key = %s; depth = %d", key, depth );
i+= 2;
}
} while( key[ 0 ] != 0 );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment