1 /* main.c -- mdemo test program
3 Copyright (C) 1998-2000, 2006, 2007 Free Software Foundation, Inc.
4 Written by Thomas Tanner, 1998
6 This file is part of GNU Libtool.
8 GNU Libtool is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 GNU Libtool is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Libtool; see the file COPYING. If not, a copy
20 can be downloaded from http://www.gnu.org/licenses/gpl.html,
21 or obtained by writing to the Free Software Foundation, Inc.,
22 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 extern int myfunc (void);
35 test_dl (char *filename, int test_ext)
38 const lt_dlinfo *info;
46 handle = lt_dlopenext (filename);
48 handle = lt_dlopen (filename);
51 fprintf (stderr, "can't open the module %s!\n", filename);
52 fprintf (stderr, "error was: %s\n", lt_dlerror());
56 info = lt_dlgetinfo(handle);
58 fprintf (stderr, "can't get module info: %s\n", lt_dlerror());
62 printf ("module name: %s\n", info->name);
64 printf ("module is not a libtool module\n");
66 printf ("module filename: %s\n", info->filename);
67 printf ("module reference count: %i\n", info->ref_count);
69 phello = (int(*)())lt_dlsym(handle, "hello");
72 int value = (*phello) ();
74 printf ("hello returned: %i\n", value);
75 if (value == HELLO_RET)
76 printf("hello is ok!\n");
80 fprintf (stderr, "did not find the `hello' function\n");
81 fprintf (stderr, "error was: %s\n", lt_dlerror());
85 pnothing = (int*)lt_dlsym(handle, "nothing");
86 /* Try assigning to the nothing variable. */
91 fprintf (stderr, "did not find the `nothing' variable\n");
92 fprintf (stderr, "error was: %s\n", lt_dlerror());
96 pfoo1 = (int(*)())lt_dlsym(handle, "foo1");
97 /* Just call the functions and check return values. */
100 if ((*pfoo1) () == FOO_RET)
101 printf("foo1 is ok!\n");
106 pfoo2 = (int(*)())lt_dlsym(handle, "foo2");
109 if ((*pfoo2) () == FOO_RET)
110 printf("foo2 is ok!\n");
115 fprintf (stderr, "did not find any of the `foo' functions\n");
116 fprintf (stderr, "error was: %s\n", lt_dlerror());
136 int (*pmyfunc)() = 0;
140 handle = lt_dlopen(0);
142 fprintf (stderr, "can't dlopen the program!\n");
143 fprintf (stderr, "error was: %s\n", lt_dlerror());
147 pmyfunc = (int(*)())lt_dlsym(handle, "myfunc");
150 int value = (*pmyfunc) ();
152 printf ("myfunc returned: %i\n", value);
153 if (value == HELLO_RET)
154 printf("myfunc is ok!\n");
158 fprintf (stderr, "did not find the `myfunc' function\n");
159 fprintf (stderr, "error was: %s\n", lt_dlerror());
163 pmyvar = (int*)lt_dlsym(handle, "myvar");
164 /* Try assigning to the variable. */
169 fprintf (stderr, "did not find the `myvar' variable\n");
170 fprintf (stderr, "error was: %s\n", lt_dlerror());
179 callback (const char *filename, void *data)
181 printf ("%s: %s\n", (char *)data, filename);
186 try_iterate (const char *search_path)
188 char *s = "try_iterate";
189 return lt_dlforeachfile (search_path, callback, s);
192 /* cheap dirname clone. We require a '/' separator, nonempty and large
193 enough input, not ending with '/', and we will overwrite the input. */
195 my_dirname (char *path)
197 char *p = strrchr (path, '/');
209 main (int argc, char **argv)
215 printf ("Welcome to GNU libtool mdemo!\n");
218 fprintf (stderr, "usage: %s module [module...]\n", argv[0]);
221 LTDL_SET_PRELOADED_SYMBOLS();
222 if (lt_dlinit() != 0) {
223 fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
227 for (i = 1; i < argc; i++)
229 if (test_dl(argv[i], 0))
231 p = strrchr(argv[i], '.');
235 if (test_dl(argv[i], 1))
244 for (i = 1; i < argc; i++)
245 if (argv[i][0] != '\0')
247 my_dirname (argv[i]);
248 if (try_iterate (argv[i]))