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

char *regex(const char *re, const  char  *subject, ...) {

  va_list ap;
  int vacount;
  void *fptr;
  char *retval;

  char *ret0, *ret1;

  fptr = dlsym(_ChLibgen_handle, "regex_chdl");
  if(fptr == NULL) {
    fprintf(_stderr, "Error: %s(): dlsym(): %s\n", __func__,  dlerror());
    return NULL;
  }

  va_start(ap, subject);
  vacount = va_count(ap);

  if(vacount == 0) 
    dlrunfun(fptr, &retval, regex, re, subject, 0);

  else if(vacount == 1) {
    ret0 = va_arg(ap, char *);
    dlrunfun(fptr, &retval, regex, re, subject, 1, ret0);
  }

  else if(vacount == 2) {
    ret0 = va_arg(ap, char *);
    ret1 = va_arg(ap, char *);
    dlrunfun(fptr, &retval, regex, re, subject, 2, ret0, ret1);
  }

  else {
    fprintf(_stderr, "Error: Too more arguments in regex()\n");
    return NULL;
  }
  
  va_end(ap);
  return retval;
} 
