1/* main.c -- mlib library
2   Copyright (C) 2002 Free Software Foundation, Inc.
3   Originally by greg Eisenhauer <eisen at cc.gatech.edu>
4   Extracted from mdemo.c
5   This file is part of GNU Libtool.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
20USA. */
21
22#include "foo.h"
23#include "ltdl.h"
24#include <stdio.h>
25
26int
27test_dl (filename)
28  char *filename;
29{
30  lt_dlhandle handle;
31  const lt_dlinfo *info;
32  int (*pfoo1)() = 0;
33  int (*pfoo2)() = 0;
34  int (*phello)() = 0;
35  int *pnothing = 0;
36  int ret = 0;
37
38  handle = lt_dlopen(filename);
39  if (!handle) {
40    fprintf (stderr, "can't open the module %s!\n", filename);
41    fprintf (stderr, "error was: %s\n", lt_dlerror());
42    return 1;
43  }
44
45  info = lt_dlgetinfo(handle);
46  if (!info) {
47    fprintf (stderr, "can't get module info: %s\n", lt_dlerror());
48    return 1;
49  }
50  if (info->name) {
51    printf ("module name: %s\n", info->name);
52  } else {
53    printf ("module is not a libtool module\n");
54  }
55  printf ("module filename: %s\n", info->filename);
56  printf ("module reference count: %i\n", info->ref_count);
57
58  phello = (int(*)())lt_dlsym(handle, "hello");
59  if (phello)
60    {
61      int value = (*phello) ();
62
63      printf ("hello returned: %i\n", value);
64      if (value == HELLO_RET)
65        printf("hello is ok!\n");
66    }
67  else
68    {
69      fprintf (stderr, "did not find the `hello' function\n");
70      fprintf (stderr, "error was: %s\n", lt_dlerror());
71      ret = 1;
72    }
73
74  pnothing = (int*)lt_dlsym(handle, "nothing");
75  /* Try assigning to the nothing variable. */
76  if (pnothing)
77    *pnothing = 1;
78  else
79    {
80      fprintf (stderr, "did not find the `nothing' variable\n");
81      fprintf (stderr, "error was: %s\n", lt_dlerror());
82      ret = 1;
83    }
84
85  pfoo1 = (int(*)())lt_dlsym(handle, "foo1");
86  /* Just call the functions and check return values. */
87  if (pfoo1)
88    {
89      if ((*pfoo1) () == FOO_RET)
90        printf("foo1 is ok!\n");
91      else
92	ret = 1;
93    }
94  else {
95    pfoo2 = (int(*)())lt_dlsym(handle, "foo2");
96    if (pfoo2)
97      {
98        if ((*pfoo2) () == FOO_RET)
99          printf("foo2 is ok!\n");
100        else ret = 1;
101      }
102    else
103      {
104        fprintf (stderr, "did not find any of the `foo' functions\n");
105        fprintf (stderr, "error was: %s\n", lt_dlerror());
106        ret = 1;
107      }
108  }
109  lt_dlclose(handle);
110  return ret;
111}
112int
113mlib_func(argc, argv)
114int argc;
115char **argv;
116{
117  int ret = 0;
118  int i;
119  /*
120   * Would be nice if this somehow worked for libraries, not just executables.
121   * LTDL_SET_PRELOADED_SYMBOLS();
122   */
123  if (lt_dlinit() != 0) {
124    fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
125    return 1;
126  }
127
128  for (i = 1; i < argc; i++)
129    if (test_dl(argv[i]))
130       ret = 1;
131
132  lt_dlexit();
133  return ret;
134}
135