Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / mdemo / main.c
1 /* main.c -- mdemo test program
2
3    Copyright (C) 1998-2000, 2006, 2007 Free Software Foundation, Inc.
4    Written by Thomas Tanner, 1998
5
6    This file is part of GNU Libtool.
7
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.
12
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.
17
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.
23 */
24
25 #include "foo.h"
26 #include "ltdl.h"
27 #include <stdio.h>
28 #include <string.h>
29
30 LT_BEGIN_C_DECLS
31 extern int myfunc (void);
32 LT_END_C_DECLS
33
34 int
35 test_dl (char *filename, int test_ext)
36 {
37   lt_dlhandle handle;   
38   const lt_dlinfo *info;
39   int (*pfoo1)() = 0;
40   int (*pfoo2)() = 0;
41   int (*phello)() = 0;
42   int *pnothing = 0;
43   int ret = 0;
44
45   if (test_ext)
46     handle = lt_dlopenext (filename);
47   else
48     handle = lt_dlopen (filename);
49
50   if (!handle) {
51     fprintf (stderr, "can't open the module %s!\n", filename);
52     fprintf (stderr, "error was: %s\n", lt_dlerror());
53     return 1;
54   }
55
56   info = lt_dlgetinfo(handle);
57   if (!info) {
58     fprintf (stderr, "can't get module info: %s\n", lt_dlerror());
59     return 1;
60   }
61   if (info->name) {
62     printf ("module name: %s\n", info->name);
63   } else {
64     printf ("module is not a libtool module\n");
65   }
66   printf ("module filename: %s\n", info->filename);
67   printf ("module reference count: %i\n", info->ref_count);
68   
69   phello = (int(*)())lt_dlsym(handle, "hello");  
70   if (phello)
71     {
72       int value = (*phello) ();
73       
74       printf ("hello returned: %i\n", value);
75       if (value == HELLO_RET)
76         printf("hello is ok!\n");
77     }
78   else
79     {
80       fprintf (stderr, "did not find the `hello' function\n");
81       fprintf (stderr, "error was: %s\n", lt_dlerror());
82       ret = 1;
83     }
84
85   pnothing = (int*)lt_dlsym(handle, "nothing");  
86   /* Try assigning to the nothing variable. */
87   if (pnothing)
88     *pnothing = 1;
89   else
90     {
91       fprintf (stderr, "did not find the `nothing' variable\n");
92       fprintf (stderr, "error was: %s\n", lt_dlerror());
93       ret = 1;
94     }
95
96   pfoo1 = (int(*)())lt_dlsym(handle, "foo1");  
97   /* Just call the functions and check return values. */
98   if (pfoo1)
99     {
100       if ((*pfoo1) () == FOO_RET)
101         printf("foo1 is ok!\n");
102       else
103         ret = 1;
104     }
105   else {
106     pfoo2 = (int(*)())lt_dlsym(handle, "foo2");  
107     if (pfoo2)
108       {
109         if ((*pfoo2) () == FOO_RET)
110           printf("foo2 is ok!\n");
111         else ret = 1;
112       }
113     else
114       {
115         fprintf (stderr, "did not find any of the `foo' functions\n");
116         fprintf (stderr, "error was: %s\n", lt_dlerror());
117         ret = 1;
118       }
119   }
120   lt_dlclose(handle);
121   return ret;
122 }
123
124 int
125 myfunc ()
126 {
127   return HELLO_RET;
128 }
129
130 int myvar;
131
132 int
133 test_dlself ()
134 {
135   lt_dlhandle handle;   
136   int (*pmyfunc)() = 0;
137   int *pmyvar = 0;
138   int ret = 0;
139
140   handle = lt_dlopen(0);
141   if (!handle) {
142     fprintf (stderr, "can't dlopen the program!\n");
143     fprintf (stderr, "error was: %s\n", lt_dlerror());
144     return 1;
145   }
146
147   pmyfunc = (int(*)())lt_dlsym(handle, "myfunc");
148   if (pmyfunc)
149     {
150       int value = (*pmyfunc) ();
151       
152       printf ("myfunc returned: %i\n", value);
153       if (value == HELLO_RET)
154         printf("myfunc is ok!\n");
155     }
156   else
157     {
158       fprintf (stderr, "did not find the `myfunc' function\n");
159       fprintf (stderr, "error was: %s\n", lt_dlerror());
160       ret = 1;
161     }
162
163   pmyvar = (int*)lt_dlsym(handle, "myvar");  
164   /* Try assigning to the variable. */
165   if (pmyvar)
166     *pmyvar = 1;
167   else
168     {
169       fprintf (stderr, "did not find the `myvar' variable\n");
170       fprintf (stderr, "error was: %s\n", lt_dlerror());
171       ret = 1;
172     }
173
174   lt_dlclose(handle);
175   return ret;
176 }
177
178 static int
179 callback (const char *filename, void *data)
180 {
181   printf ("%s: %s\n", (char *)data, filename);
182   return 0;
183 }
184
185 static int
186 try_iterate (const char *search_path)
187 {
188   char *s = "try_iterate";
189   return lt_dlforeachfile (search_path, callback, s);
190 }
191
192 /* cheap dirname clone.  We require a '/' separator, nonempty and large
193    enough input, not ending with '/', and we will overwrite the input. */
194 static char *
195 my_dirname (char *path)
196 {
197   char *p = strrchr (path, '/');
198   if (p)
199     *p = '\0';
200   else
201     {
202       path[0] = '.';
203       path[1] = '\0';
204     }
205   return path;
206 }
207
208 int
209 main (int argc, char **argv)
210 {
211   int i;
212   int ret = 0;
213   char *p;
214
215   printf ("Welcome to GNU libtool mdemo!\n");
216
217   if (argc < 2) {
218     fprintf (stderr, "usage: %s module [module...]\n", argv[0]);
219   }
220
221   LTDL_SET_PRELOADED_SYMBOLS();
222   if (lt_dlinit() != 0) {
223     fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
224     return 1;
225   }
226
227   for (i = 1; i < argc; i++)
228   {
229     if (test_dl(argv[i], 0))
230        ret = 1;
231     p = strrchr(argv[i], '.');
232     if (p)
233       {
234         *p = '\0';
235         if (test_dl(argv[i], 1))
236           ret = 1;
237         *p = '.';
238       }
239   }
240
241   if (test_dlself())
242     ret = 1;
243
244   for (i = 1; i < argc; i++)
245     if (argv[i][0] != '\0')
246       {
247         my_dirname (argv[i]);
248         if (try_iterate (argv[i]))
249           ret = 1;
250       }
251
252   lt_dlexit();
253   return ret;
254 }