55
48
#define NULL ((void *) 0)
58
#define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
51
#define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
60
#define br_return_val_if_fail(expr,val) if (!(expr)) return val
53
#define br_return_val_if_fail(expr,val) if (!(expr)) return val
61
54
#endif /* __GNUC__ */
73
66
* symbol: A symbol that belongs to the app/library you want to locate.
74
67
* Returns: A newly allocated string containing the full path of the
75
* app/library that func belongs to, or NULL on error. This
76
* string should be freed when not when no longer needed.
68
* app/library that func belongs to, or NULL on error. This
69
* string should be freed when not when no longer needed.
78
71
* Finds out to which application or library symbol belongs, then locate
79
72
* the full path of that application or library.
85
78
* #include "libfoo.h"
87
80
* int main (int argc, char *argv[]) {
88
* printf ("Full path of this app: %s\n", br_locate (&argc));
81
* printf ("Full path of this app: %s\n", br_locate (&argc));
93
86
* --> libfoo.c starts here
94
87
* #include "prefix.h"
96
89
* void libfoo_start () {
97
* --> "" is a symbol that belongs to libfoo (because it's called
98
* --> from libfoo_start()); that's why this works.
99
* printf ("libfoo is located in: %s\n", br_locate (""));
90
* --> "" is a symbol that belongs to libfoo (because it's called
91
* --> from libfoo_start()); that's why this works.
92
* printf ("libfoo is located in: %s\n", br_locate (""));
103
96
br_locate (void *symbol)
109
br_return_val_if_fail (symbol != NULL, NULL);
111
f = fopen ("/proc/self/maps", "r");
117
unsigned long start, end;
119
if (!fgets (line, sizeof (line), f))
121
if (!strstr (line, " r-xp ") || !strchr (line, '/'))
124
sscanf (line, "%lx-%lx ", &start, &end);
125
if (symbol >= (void *) start && symbol < (void *) end)
130
/* Extract the filename; it is always an absolute path */
131
path = strchr (line, '/');
133
/* Get rid of the newline */
134
tmp = strrchr (path, '\n');
137
/* Get rid of "(deleted)" */
139
if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
141
tmp = path + len - 10;
146
return strdup (path);
102
br_return_val_if_fail (symbol != NULL, NULL);
104
f = fopen ("/proc/self/maps", "r");
110
unsigned long start, end;
112
if (!fgets (line, sizeof (line), f))
114
if (!strstr (line, " r-xp ") || !strchr (line, '/'))
117
sscanf (line, "%lx-%lx ", &start, &end);
118
if (symbol >= (void *) start && symbol < (void *) end)
123
/* Extract the filename; it is always an absolute path */
124
path = strchr (line, '/');
126
/* Get rid of the newline */
127
tmp = strrchr (path, '\n');
130
/* Get rid of "(deleted)" */
132
if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
134
tmp = path + len - 10;
139
return strdup (path);
169
162
br_locate_prefix (void *symbol)
173
br_return_val_if_fail (symbol != NULL, NULL);
175
path = br_locate (symbol);
176
if (!path) return NULL;
178
prefix = br_extract_prefix (path);
166
br_return_val_if_fail (symbol != NULL, NULL);
168
path = br_locate (symbol);
169
if (!path) return NULL;
171
prefix = br_extract_prefix (path);
199
192
br_prepend_prefix (void *symbol, char *path)
203
br_return_val_if_fail (symbol != NULL, NULL);
204
br_return_val_if_fail (path != NULL, NULL);
206
tmp = br_locate_prefix (symbol);
207
if (!tmp) return NULL;
209
if (strcmp (tmp, "/") == 0)
210
newpath = strdup (path);
212
newpath = br_strcat (tmp, path);
214
/* Get rid of compiler warning ("br_prepend_prefix never used") */
215
if (0) br_prepend_prefix (NULL, NULL);
196
br_return_val_if_fail (symbol != NULL, NULL);
197
br_return_val_if_fail (path != NULL, NULL);
199
tmp = br_locate_prefix (symbol);
200
if (!tmp) return NULL;
202
if (strcmp (tmp, "/") == 0)
203
newpath = strdup (path);
205
newpath = br_strcat (tmp, path);
207
/* Get rid of compiler warning ("br_prepend_prefix never used") */
208
if (0) br_prepend_prefix (NULL, NULL);
221
214
#endif /* ENABLE_BINRELOC */
265
258
br_thread_local_store (char *str)
268
261
if (!g_thread_supported ())
270
263
g_thread_init ((GThreadFunctions *)NULL);
271
264
br_thread_key = g_private_new (g_free);
274
char *specific = (char *) g_private_get (br_thread_key);
267
char *specific = (char *) g_private_get (br_thread_key);
277
270
g_private_set (br_thread_key, str);
279
#else /* !BR_THREADS */
280
static int initialized = 0;
284
atexit (br_free_last_value);
289
free (br_last_value);
291
#endif /* BR_THREADS */
293
return (const char *) str;
272
#else /* !BR_THREADS */
273
static int initialized = 0;
277
atexit (br_free_last_value);
282
free (br_last_value);
284
#endif /* BR_THREADS */
286
return (const char *) str;
306
299
br_strcat (const char *str1, const char *str2)
311
if (!str1) str1 = "";
312
if (!str2) str2 = "";
314
len1 = strlen (str1);
315
len2 = strlen (str2);
317
result = (char *) malloc (len1 + len2 + 1);
318
memcpy (result, str1, len1);
319
memcpy (result + len1, str2, len2);
320
result[len1 + len2] = '\0';
304
if (!str1) str1 = "";
305
if (!str2) str2 = "";
307
len1 = strlen (str1);
308
len2 = strlen (str2);
310
result = (char *) malloc (len1 + len2 + 1);
311
memcpy (result, str1, len1);
312
memcpy (result + len1, str2, len2);
313
result[len1 + len2] = '\0';
328
321
br_strndup (char *str, size_t size)
330
char *result = (char*)NULL;
333
br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
336
if (!len) return strdup ("");
337
if (size > len) size = len;
339
result = (char *) calloc (sizeof (char), len + 1);
340
memcpy (result, str, size);
323
char *result = (char*)NULL;
326
br_return_val_if_fail (str != (char*)NULL, (char*)NULL);
329
if (!len) return strdup ("");
330
if (size > len) size = len;
332
result = (char *) calloc (sizeof (char), len + 1);
333
memcpy (result, str, size);
357
350
br_extract_dir (const char *path)
362
br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
364
end = strrchr (path, '/');
365
if (!end) return strdup (".");
367
while (end > path && *end == '/')
369
result = br_strndup ((char *) path, end - path + 1);
355
br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
357
end = strrchr (path, '/');
358
if (!end) return strdup (".");
360
while (end > path && *end == '/')
362
result = br_strndup ((char *) path, end - path + 1);
393
386
br_extract_prefix (const char *path)
398
br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
400
if (!*path) return strdup ("/");
401
end = strrchr (path, '/');
402
if (!end) return strdup (path);
404
tmp = br_strndup ((char *) path, end - path);
410
end = strrchr (tmp, '/');
411
if (!end) return tmp;
413
result = br_strndup (tmp, end - tmp);
419
result = strdup ("/");
391
br_return_val_if_fail (path != (char*)NULL, (char*)NULL);
393
if (!*path) return strdup ("/");
394
end = strrchr (path, '/');
395
if (!end) return strdup (path);
397
tmp = br_strndup ((char *) path, end - path);
403
end = strrchr (tmp, '/');
404
if (!end) return tmp;
406
result = br_strndup (tmp, end - tmp);
412
result = strdup ("/");
443
436
* Return the directory of the .exe that is currently running
445
static Glib::ustring win32_getExePath()
438
Glib::ustring win32_getExePath()
447
char exeName[MAX_PATH+1];
448
GetModuleFileName(NULL, exeName, MAX_PATH);
449
char *slashPos = strrchr(exeName, '\\');
452
Glib::ustring s = exeName;
440
gunichar2 path[2048];
441
GetModuleFileNameW(0, (WCHAR*) path, 2048);
442
gchar *exe = g_utf16_to_utf8(path, -1, 0,0,0);
443
gchar *dir = g_path_get_dirname(exe);
444
Glib::ustring ret = dir;
461
455
static Glib::ustring win32_getDataDir()
463
457
Glib::ustring dir = win32_getExePath();
464
if (INKSCAPE_DATADIR && *INKSCAPE_DATADIR &&
465
strcmp(INKSCAPE_DATADIR, ".") != 0)
458
if (INKSCAPE_DATADIR && *INKSCAPE_DATADIR &&
459
strcmp(INKSCAPE_DATADIR, ".") != 0)
468
462
dir += INKSCAPE_DATADIR;