dyld-750.5.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-DYLD_LIBRARY_PATH.dtest / main.c
1
2 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/door1/libbar.dylib -install_name $RUN_DIR/libbar.dylib -DVALUE=3
3 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/door2/libbar.dylib -install_name $RUN_DIR/libbar.dylib -DVALUE=17
4 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/door1/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=10 $BUILD_DIR/door1/libbar.dylib
5 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/door2/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=25 $BUILD_DIR/door2/libbar.dylib
6 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-DYLD_LIBRARY_PATH.exe
7 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/dlopen-DYLD_LIBRARY_PATH.exe
8
9 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door1/ ./dlopen-DYLD_LIBRARY_PATH.exe 13
10 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door2 ./dlopen-DYLD_LIBRARY_PATH.exe 42
11 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door3/:$RUN_DIR/door2/ ./dlopen-DYLD_LIBRARY_PATH.exe 42
12
13 #include <stdio.h>
14 #include <dlfcn.h>
15 #include <stdlib.h>
16
17 #include "test_support.h"
18
19 // Program dlopen()s libfoo.dylib which was linked against libbar.dylib
20 // Neither have valid paths and must be found via DYLD_LIBRARY_PATH
21 // This test direct and indirect loading.
22
23 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
24 const char* env = getenv("DYLD_LIBRARY_PATH");
25 if ( env == NULL ) {
26 FAIL("env not set");
27 }
28 const char* valueStr = argv[1];
29 if ( valueStr == NULL ) {
30 FAIL("arg1 value not set");
31 }
32 char* end;
33 long value = strtol(valueStr, &end, 0);
34
35
36 void* handle = dlopen("/bogus/libfoo.dylib", RTLD_LAZY);
37 if ( handle == NULL ) {
38 FAIL("dlerror(\"/bogus/libfoo.dylib\"): %s", dlerror());
39 }
40
41 typedef int (*FooProc)();
42
43 FooProc sym = (FooProc)dlsym(handle, "foo");
44 if ( sym == NULL ) {
45 FAIL("dlerror(): %s", dlerror());
46 }
47
48 int result = (*sym)();
49 if ( result != value ) {
50 FAIL("result=%d, expected %ld (str=%s)", result, value, valueStr);
51 }
52
53 int r = dlclose(handle);
54 if ( r != 0 ) {
55 FAIL("dlclose() returned %d", r);
56 }
57
58 void* handle2 = dlopen("/junk/libfoo.dylib", RTLD_LAZY);
59 if ( handle2 == NULL ) {
60 FAIL("dlerror(\"/junk/libfoo.dylib\"): %s", dlerror());
61 }
62
63 PASS("Success");
64 }
65