Last active
August 29, 2015 14:01
-
-
Save stubbetje/b45b173ec10a5bc26d4a to your computer and use it in GitHub Desktop.
Calling COBOL from C with variable arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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