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

int chglobalsymboladdrbyname() {
  void *handle, *fptr;
  int retval;

  handle = dlopen("libch.dl", RTLD_LAZY);
  if(handle == NULL) {
       printf("Error: %s(): dlopen(): %s\n", __func__, dlerror());
       return -1;
  }
  fptr = dlsym(handle, "chglobalsymboladdrbyname_chdl");
  if(fptr == NULL)  {
     printf("Error: %s(): dlsym(): %s\n", __func__, dlerror());
     return -1;
  }
  dlrunfun(fptr, &retval, chglobalsymboladdrbyname);
  if(dlclose(handle)!=0) {
     printf("Error: %s(): dlclose(): %s\n", __func__, dlerror());
     return -1;
  }
  return retval;
}

int ch_i;      /* ch_i is an extern variable */
int *ch_p;     /* ch_p is an extern variable */
float ch_f;    /* ch_f is an extern variable */
int *ch_a;    
int a[3] = {10, 20, 30};
array int aa[3] = {100, 200, 300}; /* computatinal array */
char *str[] = {"abcd", "ABCD", NULL};

int main() {
  int retval;

  ch_i = 10;
  ch_p = &ch_i;
  ch_f = 20;
  ch_a = a;    

  printf("&ch_i in Ch space: %p\n", &ch_i);
  chglobalsymboladdrbyname();
  printf("ch_i value is re-assigned by dynamically loaded object to: %d\n", ch_i);
  printf("ch_f value is re-assigned by dynamically loaded object to: %f\n", ch_f);
}
