libtool/mdemo/main.c
Thomas Tanner c361d1cfeb * cdemo/Makefile.am: renamed *foo1 to *foo, removed $(MATH_LIB)
* cdemo/foo1.c: renamed to foo.c
* libltdl/ltdl.c: eliminated lt_dltype, in presym_open():
  search for @PROGRAM@ if filename == 0, in lt_dlopen():
  fixed bug for filename == 0
* libltdl/ltdl.h: use __P macro for lt_dlmalloc/free
* libtool.m4: new macro AC_LIBTOOL_DLOPEN: checks for dlopen
  and dlopen(NULL) support
* ltconfig.in: added new variables dlopen and dlopen_self,
  --enable-dlopen[-self] flags for AC_LIBTOOL_DLOPEN
* ltmain.in: use dlopen[_self], dlpreopen files if there's no
  dlopen support, build lt_preloaded_symbols only if
  -dl[pre]open was used, include program symbols if -export-dynamic
  was specified and there's no dlopen(NULL) support
* mdemo/configure.in: use AC_LIBTOOL_DLOPEN
* mdemo/main.c: demonstrate lt_dlopen(0)
1999-02-15 16:46:55 +00:00

155 lines
3.4 KiB
C

/* main.c -- mdemo test program
Copyright (C) 1998-1999 Free Software Foundation, Inc.
Originally by Thomas Tanner <tanner@gmx.de>
This file is part of GNU Libtool.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
#include "foo.h"
#include "ltdl.h"
#include <stdio.h>
int
test_dl (filename)
char *filename;
{
lt_dlhandle handle;
int (*pfoo1)() = 0;
int (*pfoo2)() = 0;
int (*phello)() = 0;
int *pnothing = 0;
handle = lt_dlopen(filename);
if (!handle) {
fprintf (stderr, "can't open the module %s!\n", filename);
fprintf (stderr, "error was: %s\n", lt_dlerror());
return 1;
}
phello = (int(*)())lt_dlsym(handle, "hello");
pfoo1 = (int(*)())lt_dlsym(handle, "foo1");
pfoo2 = (int(*)())lt_dlsym(handle, "foo2");
pnothing = (int*)lt_dlsym(handle, "nothing");
if (phello)
{
int value = (*phello) ();
printf ("hello returned: %i\n", value);
if (value == HELLO_RET)
printf("hello is ok!\n");
}
else
fprintf (stderr, "did not find the `hello' function\n");
/* Try assigning to the nothing variable. */
if (pnothing)
*pnothing = 1;
else
fprintf (stderr, "did not find the `nothing' variable\n");
/* Just call the functions and check return values. */
if (pfoo1)
{
if ((*pfoo1) () == FOO_RET)
printf("foo1 is ok!\n");
}
else if (pfoo2)
{
if ((*pfoo2) () == FOO_RET)
printf("foo2 is ok!\n");
}
else
fprintf (stderr, "did not find the `foo' function\n");
lt_dlclose(handle);
return 0;
}
int
myfunc ()
{
return HELLO_RET;
}
int myvar;
int
test_dlself ()
{
lt_dlhandle handle;
int (*pmyfunc)() = 0;
int *pmyvar = 0;
handle = lt_dlopen(0);
if (!handle) {
fprintf (stderr, "can't dlopen the program!\n");
fprintf (stderr, "error was: %s\n", lt_dlerror());
return 1;
}
pmyfunc = (int(*)())lt_dlsym(handle, "myfunc");
pmyvar = (int*)lt_dlsym(handle, "myvar");
if (pmyfunc)
{
int value = (*pmyfunc) ();
printf ("myfunc returned: %i\n", value);
if (value == HELLO_RET)
printf("myfunc is ok!\n");
}
else
fprintf (stderr, "did not find the `myfunc' function\n");
/* Try assigning to the variable. */
if (pmyvar)
*pmyvar = 1;
else
fprintf (stderr, "did not find the `myvar' variable\n");
lt_dlclose(handle);
return 0;
}
int
main (argc, argv)
int argc;
char **argv;
{
int i;
printf ("Welcome GNU libtool mdemo!\n");
if (argc < 2) {
fprintf (stderr, "usage: %s module [module...]\n", argv[0]);
}
LTDL_SET_PRELOADED_SYMBOLS();
if (lt_dlinit() != 0) {
fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
return 1;
}
for (i = 1; i < argc; i++)
if (test_dl(argv[i]))
return 1;
if (test_dlself())
return 1;
lt_dlexit();
return 0;
}