Get the directory that contains this shared library at run-time */
| 226 | |
| 227 | /* Get the directory that contains this shared library at run-time */ |
| 228 | char *gmt_runtime_libdir (char *result) { |
| 229 | #ifdef HAVE_DLADDR |
| 230 | char *p; |
| 231 | Dl_info info; |
| 232 | |
| 233 | if ( dladdr (gmt_runtime_libdir, &info) && info.dli_fname[0] == '/') { |
| 234 | /* Resolve symlinks */ |
| 235 | if (realpath (info.dli_fname, result) == NULL) |
| 236 | return NULL; |
| 237 | /* Truncate absolute path to dirname */ |
| 238 | if ( (p = strrchr (result, '/')) && p != result ) |
| 239 | *p = '\0'; |
| 240 | #ifdef DEBUG_RUNPATH |
| 241 | fprintf (stderr, "library is in '%s' (from dladdr).\n", result); |
| 242 | #endif |
| 243 | return result; |
| 244 | } |
| 245 | #endif /* HAVE_DLADDR */ |
| 246 | |
| 247 | #ifdef WIN32 |
| 248 | /* Get the directory that contains this DLL at run-time on Windows */ |
| 249 | MEMORY_BASIC_INFORMATION mbi; |
| 250 | HMODULE mod; |
| 251 | TCHAR path[PATH_MAX+1]; |
| 252 | char *p; |
| 253 | static int dummy; |
| 254 | |
| 255 | /* Get the directory that contains this DLL at run-time on Windows */ |
| 256 | VirtualQuery (&dummy, &mbi, sizeof(mbi)); |
| 257 | mod = (HMODULE) mbi.AllocationBase; |
| 258 | |
| 259 | /* Get absolute path of this dll */ |
| 260 | if ( GetModuleFileName (mod, path, PATH_MAX) == PATH_MAX ) |
| 261 | /* Path to long */ |
| 262 | return NULL; |
| 263 | |
| 264 | /* Convert to cstring */ |
| 265 | #ifdef _UNICODE |
| 266 | /* TCHAR is wchar_t* */ |
| 267 | wcstombs (result, path, PATH_MAX); |
| 268 | #else |
| 269 | /* TCHAR is char * */ |
| 270 | strncpy (result, path, PATH_MAX); |
| 271 | #endif |
| 272 | |
| 273 | /* Replace backslashes */ |
| 274 | gmt_strrepc (result, '\\', '/'); |
| 275 | |
| 276 | /* Truncate full path to dirname */ |
| 277 | if (((p = strrchr (result, '/')) != NULL) && p != result) |
| 278 | *p = '\0'; |
| 279 | |
| 280 | #ifdef DEBUG_RUNPATH |
| 281 | fprintf (stderr, "DLL is in '%s' (from GetModuleFileName).\n", result); |
| 282 | #endif |
| 283 | |
| 284 | return result; |
| 285 | #endif /* WIN32 */ |
no test coverage detected