1/* main.c -- mdemo test program 2 Copyright (C) 1998-2000 Free Software Foundation, Inc. 3 Originally by Thomas Tanner <tanner@ffii.org> 4 This file is part of GNU Libtool. 5 6This program is free software; you can redistribute it and/or modify 7it under the terms of the GNU General Public License as published by 8the Free Software Foundation; either version 2 of the License, or 9(at your option) any later version. 10 11This program is distributed in the hope that it will be useful, 12but WITHOUT ANY WARRANTY; without even the implied warranty of 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14GNU General Public License for more details. 15 16You should have received a copy of the GNU General Public License 17along with this program; if not, write to the Free Software 18Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 19USA. */ 20 21#include "foo.h" 22#include "ltdl.h" 23#include <stdio.h> 24 25int 26test_dl (filename) 27 char *filename; 28{ 29 lt_dlhandle handle; 30 const lt_dlinfo *info; 31 int (*pfoo1)() = 0; 32 int (*pfoo2)() = 0; 33 int (*phello)() = 0; 34 int *pnothing = 0; 35 int ret = 0; 36 37 handle = lt_dlopen(filename); 38 if (!handle) { 39 fprintf (stderr, "can't open the module %s!\n", filename); 40 fprintf (stderr, "error was: %s\n", lt_dlerror()); 41 return 1; 42 } 43 44 info = lt_dlgetinfo(handle); 45 if (!info) { 46 fprintf (stderr, "can't get module info: %s\n", lt_dlerror()); 47 return 1; 48 } 49 if (info->name) { 50 printf ("module name: %s\n", info->name); 51 } else { 52 printf ("module is not a libtool module\n"); 53 } 54 printf ("module filename: %s\n", info->filename); 55 printf ("module reference count: %i\n", info->ref_count); 56 57 phello = (int(*)())lt_dlsym(handle, "hello"); 58 if (phello) 59 { 60 int value = (*phello) (); 61 62 printf ("hello returned: %i\n", value); 63 if (value == HELLO_RET) 64 printf("hello is ok!\n"); 65 } 66 else 67 { 68 fprintf (stderr, "did not find the `hello' function\n"); 69 fprintf (stderr, "error was: %s\n", lt_dlerror()); 70 ret = 1; 71 } 72 73 pnothing = (int*)lt_dlsym(handle, "nothing"); 74 /* Try assigning to the nothing variable. */ 75 if (pnothing) 76 *pnothing = 1; 77 else 78 { 79 fprintf (stderr, "did not find the `nothing' variable\n"); 80 fprintf (stderr, "error was: %s\n", lt_dlerror()); 81 ret = 1; 82 } 83 84 pfoo1 = (int(*)())lt_dlsym(handle, "foo1"); 85 /* Just call the functions and check return values. */ 86 if (pfoo1) 87 { 88 if ((*pfoo1) () == FOO_RET) 89 printf("foo1 is ok!\n"); 90 else 91 ret = 1; 92 } 93 else { 94 pfoo2 = (int(*)())lt_dlsym(handle, "foo2"); 95 if (pfoo2) 96 { 97 if ((*pfoo2) () == FOO_RET) 98 printf("foo2 is ok!\n"); 99 else ret = 1; 100 } 101 else 102 { 103 fprintf (stderr, "did not find any of the `foo' functions\n"); 104 fprintf (stderr, "error was: %s\n", lt_dlerror()); 105 ret = 1; 106 } 107 } 108 lt_dlclose(handle); 109 return ret; 110} 111 112int 113myfunc () 114{ 115 return HELLO_RET; 116} 117 118int myvar; 119 120int 121test_dlself () 122{ 123 lt_dlhandle handle; 124 int (*pmyfunc)() = 0; 125 int *pmyvar = 0; 126 int ret = 0; 127 128 handle = lt_dlopen(0); 129 if (!handle) { 130 fprintf (stderr, "can't dlopen the program!\n"); 131 fprintf (stderr, "error was: %s\n", lt_dlerror()); 132 return 1; 133 } 134 135 pmyfunc = (int(*)())lt_dlsym(handle, "myfunc"); 136 if (pmyfunc) 137 { 138 int value = (*pmyfunc) (); 139 140 printf ("myfunc returned: %i\n", value); 141 if (value == HELLO_RET) 142 printf("myfunc is ok!\n"); 143 } 144 else 145 { 146 fprintf (stderr, "did not find the `myfunc' function\n"); 147 fprintf (stderr, "error was: %s\n", lt_dlerror()); 148 ret = 1; 149 } 150 151 pmyvar = (int*)lt_dlsym(handle, "myvar"); 152 /* Try assigning to the variable. */ 153 if (pmyvar) 154 *pmyvar = 1; 155 else 156 { 157 fprintf (stderr, "did not find the `myvar' variable\n"); 158 fprintf (stderr, "error was: %s\n", lt_dlerror()); 159 ret = 1; 160 } 161 162 lt_dlclose(handle); 163 return ret; 164} 165 166int 167main (argc, argv) 168 int argc; 169 char **argv; 170{ 171 int i; 172 int ret = 0; 173 174 printf ("Welcome to GNU libtool mdemo!\n"); 175 176 if (argc < 2) { 177 fprintf (stderr, "usage: %s module [module...]\n", argv[0]); 178 } 179 180 LTDL_SET_PRELOADED_SYMBOLS(); 181 if (lt_dlinit() != 0) { 182 fprintf (stderr, "error during initialization: %s\n", lt_dlerror()); 183 return 1; 184 } 185 186 for (i = 1; i < argc; i++) 187 if (test_dl(argv[i])) 188 ret = 1; 189 190 if (test_dlself()) 191 ret = 1; 192 193 lt_dlexit(); 194 return ret; 195} 196