/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include <stdarg.h>
#include <semaphore.h>
sem_t *sem_open(const char *name, int oflag, ...) {
    void *fptr;
    mode_t mode;
    unsigned int value;
    va_list ap;
    int count;
    sem_t  *retval;

    va_start(ap, oflag); 
    count = va_count(ap);
    if(count == 0)
      fptr = dlsym(_ChSemaphore_handle, "sem_open2_chdl");
    else if(count == 2) {
      fptr = dlsym(_ChSemaphore_handle, "sem_open4_chdl");
      mode = va_arg(ap, mode_t);
      value = va_arg(ap, int);
    }
    else {
       printf("Error: %s(): invalid number of arguments\n", __func__);
       return NULL;
    }

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

    if(count == 0)
      dlrunfun(fptr, &retval, sem_open, name, oflag);
    else if(count == 2) 
      dlrunfun(fptr, &retval, sem_open, name, oflag, mode, value);
 
    va_end(ap);

    return retval;
}
