3 Copyright 1998, 2000 Cygnus Solutions
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
19 #define _dl_error _reent_winsup()->_dl_error
20 #define _dl_buffer _reent_winsup()->_dl_buffer
23 set_dl_error (const char *str)
25 __small_sprintf (_dl_buffer, "%s: %E", str);
29 /* Check for existence of a file specified by the directory
30 and name components. If successful, return a pointer the
31 full pathname (static buffer), else return 0. */
32 static const char * __stdcall
33 check_access (const char *dir, const char *name)
35 static char buf[MAX_PATH];
43 if (!access (buf, F_OK))
48 /* Look for an executable file given the name and the environment
49 variable to use for searching (eg., PATH); returns the full
50 pathname (static buffer) if found or NULL if not. */
51 static const char * __stdcall
52 check_path_access (const char *mywinenv, const char *name)
55 return find_exec (name, buf, mywinenv, TRUE);
58 /* Simulate the same search as LoadLibary + check environment
59 variable LD_LIBRARY_PATH. If found, return the full pathname
60 (static buffer); if illegal, return the input string unchanged
61 and let the caller deal with it; return NULL otherwise.
63 Note that this should never be called with a NULL string, since
64 that is the introspective case, and the caller should not call
65 this function at all. */
66 static const char * __stdcall
67 get_full_path_of_dll (const char* str)
69 int len = (str) ? strlen (str) : 0;
71 /* NULL or empty string or too long to be legal win32 pathname? */
72 if (len == 0 || len >= MAX_PATH - 1)
76 static char name[MAX_PATH];
81 /* Add extension if necessary, but leave a trailing '.', if any, alone.
82 Files with trailing '.'s are handled differently by win32 API. */
83 if (str[len - 1] != '.')
85 /* Add .dll only if no extension provided. Handle various cases:
86 ./shlib --> ./shlib.dll
87 ./dir/shlib.so --> ./dir/shlib.so
89 shlib.dll --> shlib.dll
90 shlib.so --> shlib.so */
91 const char *p = strrchr (str, '.');
92 if (!p || isdirsep (p[1]))
93 strcat (name, ".dll");
96 /* Deal with fully qualified filename right away. Do the actual
97 conversion to win32 filename just before returning however. */
101 /* current directory */
104 if (GetCurrentDirectory (MAX_PATH, buf) == 0)
105 small_printf ("WARNING: get_full_path_of_dll can't get current directory win32 %E\n");
107 ret = check_access (buf, name);
110 /* LD_LIBRARY_PATH */
112 ret = check_path_access ("LD_LIBRARY_PATH=", name);
116 if (GetSystemDirectory (buf, MAX_PATH) == 0)
117 small_printf ("WARNING: get_full_path_of_dll can't get system directory win32 %E\n");
119 ret = check_access (buf, name);
122 /* 16 bits system directory */
123 if (!ret && (os_being_run == winNT))
125 /* we assume last dir was xxxxx\SYSTEM32, so we remove 32 */
128 ret = check_access (buf, name);
131 /* windows directory */
134 if (GetWindowsDirectory (buf, MAX_PATH) == 0)
135 small_printf ("WARNING: get_full_path_of_dll can't get Windows directory win32 %E\n");
137 ret = check_access (buf, name);
142 ret = check_path_access ("PATH=", name);
144 /* Now do a final conversion to win32 pathname. This step is necessary
145 to resolve symlinks etc so that win32 API finds the underlying file. */
148 path_conv real_filename (ret, PC_SYM_FOLLOW | PC_FULL);
149 if (real_filename.error)
153 strcpy (name, real_filename.get_win32 ());
161 dlopen (const char *name, int)
163 SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
169 /* handle for the current module */
170 ret = (void *) GetModuleHandle (NULL);
174 /* handle for the named library */
175 const char *fullpath = get_full_path_of_dll (name);
176 ret = (void *) LoadLibrary (fullpath);
180 set_dl_error ("dlopen");
181 debug_printf ("ret %p", ret);
183 ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
188 dlsym (void *handle, const char *name)
190 void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
192 set_dl_error ("dlsym");
193 debug_printf ("ret %p", ret);
198 dlclose (void *handle)
200 SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");
203 if (FreeLibrary ((HMODULE) handle))
206 set_dl_error ("dlclose");
208 ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");