Make it compile on AIX. (#141159, Michael Wilson)
[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
29 #include <dlfcn.h>
30
31 /* --- functions --- */
32 static gchar*
33 fetch_dlerror (gboolean replace_null)
34 {
35   gchar *msg = dlerror ();
36
37   /* make sure we always return an error message != NULL, if
38    * expected to do so. */
39
40   if (!msg && replace_null)
41     return "unknown dl-error";
42
43   return msg;
44 }
45
46 static gchar* _g_module_get_member(const gchar* file_name)
47 {
48   gchar* member = NULL;
49   struct fl_hdr file_header;
50   struct ar_hdr ar_header;
51   long first_member;
52   long name_len;
53   int fd;
54
55   fd = open(file_name, O_RDONLY);
56   if (fd == -1)
57     return NULL;
58
59   if (read(fd, (void*)&file_header, FL_HSZ) != FL_HSZ)
60     goto exit;
61
62   if (strncmp(file_header.fl_magic, AIAMAGBIG, SAIAMAG) != 0)
63     goto exit;
64
65   /* read first archive file member header */
66
67   first_member = atol(file_header.fl_fstmoff);
68
69   if (lseek(fd, first_member, SEEK_SET) != first_member)
70     goto exit;
71
72   if (read(fd, (void*)&ar_header, AR_HSZ - 2) != AR_HSZ - 2)
73     goto exit;
74
75   /* read member name */
76
77   name_len = atol(ar_header.ar_namlen);
78
79   member = g_malloc(name_len+1);
80   if (!member)
81     goto exit;
82
83   if (read(fd, (void*)member, name_len) != name_len)
84     {
85       g_free(member);
86       member = NULL;
87       goto exit;
88     }
89
90   member[name_len] = 0;
91
92 exit:
93   close(fd);
94
95   return member;
96 }
97
98 static gpointer
99 _g_module_open (const gchar *file_name,
100                 gboolean     bind_lazy,
101                 gboolean     bind_local)
102 {
103   gpointer handle;
104   gchar* member;
105   gchar* full_name;
106
107   /* extract name of first member of archive */
108
109   member = _g_module_get_member (file_name);
110   if (member != NULL)
111     {
112       full_name = g_strconcat (file_name, "(", member, ")", NULL);
113       g_free (member);
114     }
115   else
116     full_name = g_strdup (file_name);
117   
118   handle = dlopen (full_name, 
119                    (bind_local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_MEMBER | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
120
121   g_free (full_name);
122
123   if (!handle)
124     g_module_set_error (fetch_dlerror (TRUE));
125   
126   return handle;
127 }
128
129 static gpointer
130 _g_module_self (void)
131 {
132   gpointer handle;
133
134   handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
135   if (!handle)
136     g_module_set_error (fetch_dlerror (TRUE));
137   
138   return handle;
139 }
140
141 static void
142 _g_module_close (gpointer handle,
143                  gboolean is_unref)
144 {
145   /* are there any systems out there that have dlopen()/dlclose()
146    * without a reference count implementation?
147    */
148   is_unref |= 1;
149   
150   if (is_unref)
151     {
152       if (dlclose (handle) != 0)
153         g_module_set_error (fetch_dlerror (TRUE));
154     }
155 }
156
157 static gpointer
158 _g_module_symbol (gpointer     handle,
159                   const gchar *symbol_name)
160 {
161   gpointer p;
162   
163   p = dlsym (handle, symbol_name);
164   if (!p)
165     g_module_set_error (fetch_dlerror (FALSE));
166   
167   return p;
168 }
169
170 static gchar*
171 _g_module_build_path (const gchar *directory,
172                       const gchar *module_name)
173 {
174   if (directory && *directory) {
175     if (strncmp (module_name, "lib", 3) == 0)
176       return g_strconcat (directory, "/", module_name, NULL);
177     else
178       return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
179   } else if (strncmp (module_name, "lib", 3) == 0)
180     return g_strdup (module_name);
181   else
182     return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
183 }