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