/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include <dlfcn.h>
#include <stdarg.h>

typedef int (*FUNC)(int num, ...);

int f(int num, ...) { // function to be set by func1()
  int vacount, val_i, i;
  float val_f;
  double val_d;
  va_list ap;
 
  va_start(ap, num);
  vacount = va_count(ap);
  printf("vacount = %d\n", vacount);
  printf("num = %d\n", num);
  for(i=0; i<vacount; i++) {
     if(va_elementtype(ap) == elementtype(int)) {
       val_i = va_arg(ap, int);
       printf("val_i = %d\n", val_i);
     }
     else if (va_elementtype(ap) == elementtype(float)) {
       val_f = va_arg(ap, float);
       printf("val_f = %f\n", val_f);
     }
     else if (va_elementtype(ap) == elementtype(double)) {
       val_d = va_arg(ap, double);
       printf("val_f = %f\n", val_d);
     }
  }
  printf("\n");
  return 0;
}

int func1(FUNC f) {
   void *handle, *fptr;
   int retval;

   handle = dlopen("libprog.dl", RTLD_LAZY);
   if(handle == NULL) {
      printf("Error: %s(): dlopen(): %s\n", __func__, dlerror());
      return -1;
   }
   fptr = dlsym(handle, "func1_chdl");
   if(fptr == NULL) {
      printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
      return -1;
   }
   dlrunfun(fptr, &retval, func1, f);
   return retval;
}

int main() {
   func1(f);
   return 0;
}

