#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
extern int foo();
extern int bar();
typedef int (*IntProc)();
int main()
{
const char* suffix = getenv("DYLD_IMAGE_SUFFIX");
if ( suffix == NULL )
suffix = "";
printf("[BEGIN] env-DYLD_IMAGE_SUFFIX-%s\n", suffix);
const int expectedFoo = (strstr(suffix, "_other") != NULL) ? 42 : 1;
const int expectedBar = (strstr(suffix, "_alt") != NULL) ? 42 : 1;;
#ifdef RUN_DIR
void* fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
if ( fooHandle == NULL ) {
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, libfoo.dylib could not be loaded, %s\n", suffix, dlerror());
return 0;
}
void* barHandle = dlopen(RUN_DIR "/Bar.framework/Bar", RTLD_LAZY);
if ( barHandle == NULL ) {
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, Bar.framework/Bar could not be loaded, %s\n", suffix, dlerror());
return 0;
}
IntProc fooProc = (IntProc)dlsym(fooHandle, "foo");
if ( fooProc == NULL ) {
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, symbol 'foo' not found %s\n", suffix, dlerror());
return 0;
}
IntProc barProc = (IntProc)dlsym(barHandle, "bar");
if ( barProc == NULL ) {
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, symbol 'bar' not found %s\n", suffix, dlerror());
return 0;
}
int fooValue = (*fooProc)();
int barValue = (*barProc)();
#else
int fooValue = foo();
int barValue = bar();
#endif
if ( fooValue != expectedFoo )
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, foo()=%d expected=%d\n", suffix, fooValue, expectedFoo);
else if ( barValue != expectedBar )
printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, bar()=%d expected=%d\n", suffix, barValue, expectedBar);
else
printf("[PASS] env-DYLD_IMAGE_SUFFIX-%s\n", suffix);
return 0;
}