2.9.0
[platform/upstream/glib.git] / gmodule / gmodule-ar.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* 
21  * MT safe
22  */
23
24 /* because we are compatible with archive format only since AIX 4.3 */
25
26 #define __AR_BIG__
27 #include <ar.h>
28 #include <stdlib.h>
29
30 #include <dlfcn.h>
31
32 /* --- functions --- */
33 static gchar*
34 fetch_dlerror (gboolean replace_null)
35 {
36   gchar *msg = dlerror ();
37
38   /* make sure we always return an error message != NULL, if
39    * expected to do so. */
40
41   if (!msg && replace_null)
42     return "unknown dl-error";
43
44   return msg;
45 }
46
47 static gchar* _g_module_get_member(const gchar* file_name)
48 {
49   gchar* member = NULL;
50   struct fl_hdr file_header;
51   struct ar_hdr ar_header;
52   long first_member;
53   long name_len;
54   int fd;
55
56   fd = open(file_name, O_RDONLY);
57   if (fd == -1)
58     return NULL;
59
60   if (read(fd, (void*)&file_header, FL_HSZ) != FL_HSZ)
61     goto exit;
62
63   if (strncmp(file_header.fl_magic, AIAMAGBIG, SAIAMAG) != 0)
64     goto exit;
65
66   /* read first archive file member header */
67
68   first_member = atol(file_header.fl_fstmoff);
69
70   if (lseek(fd, first_member, SEEK_SET) != first_member)
71     goto exit;
72
73   if (read(fd, (void*)&ar_header, AR_HSZ - 2) != AR_HSZ - 2)
74     goto exit;
75
76   /* read member name */
77
78   name_len = atol(ar_header.ar_namlen);
79
80   member = g_malloc(name_len+1);
81   if (!member)
82     goto exit;
83
84   if (read(fd, (void*)member, name_len) != name_len)
85     {
86       g_free(member);
87       member = NULL;
88       goto exit;
89     }
90
91   member[name_len] = 0;
92
93 exit:
94   close(fd);
95
96   return member;
97 }
98
99 static gpointer
100 _g_module_open (const gchar *file_name,
101                 gboolean     bind_lazy,
102                 gboolean     bind_local)
103 {
104   gpointer handle;
105   gchar* member;
106   gchar* full_name;
107
108   /* extract name of first member of archive */
109
110   member = _g_module_get_member (file_name);
111   if (member != NULL)
112     {
113       full_name = g_strconcat (file_name, "(", member, ")", NULL);
114       g_free (member);
115     }
116   else
117     full_name = g_strdup (file_name);
118   
119   handle = dlopen (full_name, 
120                    (bind_local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_MEMBER | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
121
122   g_free (full_name);
123
124   if (!handle)
125     g_module_set_error (fetch_dlerror (TRUE));
126   
127   return handle;
128 }
129
130 static gpointer
131 _g_module_self (void)
132 {
133   gpointer handle;
134
135   handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
136   if (!handle)
137     g_module_set_error (fetch_dlerror (TRUE));
138   
139   return handle;
140 }
141
142 static void
143 _g_module_close (gpointer handle,
144                  gboolean is_unref)
145 {
146   /* are there any systems out there that have dlopen()/dlclose()
147    * without a reference count implementation?
148    */
149   is_unref |= 1;
150   
151   if (is_unref)
152     {
153       if (dlclose (handle) != 0)
154         g_module_set_error (fetch_dlerror (TRUE));
155     }
156 }
157
158 static gpointer
159 _g_module_symbol (gpointer     handle,
160                   const gchar *symbol_name)
161 {
162   gpointer p;
163   
164   p = dlsym (handle, symbol_name);
165   if (!p)
166     g_module_set_error (fetch_dlerror (FALSE));
167   
168   return p;
169 }
170
171 static gchar*
172 _g_module_build_path (const gchar *directory,
173                       const gchar *module_name)
174 {
175   if (directory && *directory) {
176     if (strncmp (module_name, "lib", 3) == 0)
177       return g_strconcat (directory, "/", module_name, NULL);
178     else
179       return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
180   } else if (strncmp (module_name, "lib", 3) == 0)
181     return g_strdup (module_name);
182   else
183     return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
184 }