2
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3
* http://www.gnu.org/licenses/gpl-3.0.html
6
* $Id: prefix.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
7
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/src/prefix.cpp $
11
* BinReloc - a library for creating relocatable executables
12
* Written by: Mike Hearn <mike@theoretic.com>
13
* Hongli Lai <h.lai@chello.nl>
14
* http://autopackage.org/
16
* This source code is public domain. You can relicense this code
17
* under whatever license you want.
19
* NOTE: if you're using C++ and are getting "undefined reference
20
* to br_*", try renaming prefix.c to prefix.cpp
23
/* WARNING, BEFORE YOU MODIFY PREFIX.C:
25
* If you make changes to any of the functions in prefix.c, you MUST
26
* change the BR_NAMESPACE macro (in prefix.h).
27
* This way you can avoid symbol table conflicts with other libraries
28
* that also happen to use BinReloc.
31
* #define BR_NAMESPACE(funcName) foobar_ ## funcName
32
* --> expands br_locate to foobar_br_locate
43
/* Change 1 to 0 if you don't want pthread support */
45
#endif /* BR_PTHREADS */
55
#endif /* __cplusplus */
59
#define NULL ((void *) 0)
62
#define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
64
#define br_return_val_if_fail(expr,val) if (!(expr)) return val
68
#ifdef ENABLE_BINRELOC
69
#include <sys/types.h>
71
#include <sys/param.h>
77
* symbol: A symbol that belongs to the app/library you want to locate.
78
* Returns: A newly allocated string containing the full path of the
79
* app/library that func belongs to, or NULL on error. This
80
* string should be freed when not when no longer needed.
82
* Finds out to which application or library symbol belongs, then locate
83
* the full path of that application or library.
84
* Note that symbol cannot be a pointer to a function. That will not work.
91
* int main (int argc, char *argv[]) {
92
* printf ("Full path of this app: %s\n", br_locate (&argc));
97
* --> libfoo.c starts here
100
* void libfoo_start () {
101
* --> "" is a symbol that belongs to libfoo (because it's called
102
* --> from libfoo_start()); that's why this works.
103
* printf ("libfoo is located in: %s\n", br_locate (""));
107
br_locate (void *symbol)
113
br_return_val_if_fail (symbol != NULL, (char*)NULL);
115
f = fopen ("/proc/self/maps", "r");
121
unsigned long start, end;
123
if (!fgets (line, sizeof (line), f))
125
if (!strstr (line, " r-xp ") || !strchr (line, '/'))
128
sscanf (line, "%lx-%lx ", &start, &end);
129
if (symbol >= (void *) start && symbol < (void *) end)
134
/* Extract the filename; it is always an absolute path */
135
path = strchr (line, '/');
137
/* Get rid of the newline */
138
tmp = strrchr (path, '\n');
141
/* Get rid of "(deleted)" */
143
if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
145
tmp = path + len - 10;
150
return strdup (path);
161
* symbol: A symbol that belongs to the app/library you want to locate.
162
* Returns: A prefix. This string should be freed when no longer needed.
164
* Locates the full path of the app/library that symbol belongs to, and return
165
* the prefix of that path, or NULL on error.
166
* Note that symbol cannot be a pointer to a function. That will not work.
169
* --> This application is located in /usr/bin/foo
170
* br_locate_prefix (&argc); --> returns: "/usr"
173
br_locate_prefix (void *symbol)
177
br_return_val_if_fail (symbol != NULL, (char*)NULL);
179
path = br_locate (symbol);
180
if (!path) return (char*)NULL;
182
prefix = br_extract_prefix (path);
190
* symbol: A symbol that belongs to the app/library you want to locate.
191
* path: The path that you want to prepend the prefix to.
192
* Returns: The new path, or NULL on error. This string should be freed when no
195
* Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
196
* Note that symbol cannot be a pointer to a function. That will not work.
199
* --> The application is /usr/bin/foo
200
* br_prepend_prefix (&argc, "/share/foo/data.png"); --> Returns "/usr/share/foo/data.png"
203
br_prepend_prefix (void *symbol, char *path)
207
br_return_val_if_fail (symbol != NULL, (char*)NULL);
208
br_return_val_if_fail (path != NULL, (char*)NULL);
210
tmp = br_locate_prefix (symbol);
211
if (!tmp) return (char*)NULL;
213
if (strcmp (tmp, "/") == 0)
214
newpath = strdup (path);
216
newpath = br_strcat (tmp, path);
218
/* Get rid of compiler warning ("br_prepend_prefix never used") */
219
if (0) br_prepend_prefix (NULL, (char*)NULL);
225
#endif /* ENABLE_BINRELOC */
228
/* Pthread stuff for thread safetiness */
233
static pthread_key_t br_thread_key;
235
static pthread_once_t br_thread_key_once = {0, (pthread_mutex_t)NULL};//PTHREAD_ONCE_INIT;
237
static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
241
br_thread_local_store_fini ()
245
specific = (char *) pthread_getspecific (br_thread_key);
249
pthread_setspecific (br_thread_key, NULL);
251
pthread_key_delete (br_thread_key);
257
br_str_free (void *str)
265
br_thread_local_store_init ()
267
if (pthread_key_create (&br_thread_key, br_str_free) == 0)
268
atexit (br_thread_local_store_fini);
271
#else /* BR_PTHREADS */
273
static char *br_last_value = (char *) NULL;
276
br_free_last_value ()
279
free (br_last_value);
282
#endif /* BR_PTHREADS */
286
* br_thread_local_store:
287
* str: A dynamically allocated string.
288
* Returns: str. This return value must not be freed.
290
* Store str in a thread-local variable and return str. The next
291
* you run this function, that variable is freed too.
292
* This function is created so you don't have to worry about freeing
297
* foo = thread_local_store (strdup ("hello")); --> foo == "hello"
298
* foo = thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
301
br_thread_local_store (char *str)
306
pthread_once (&br_thread_key_once, br_thread_local_store_init);
308
specific = (char *) pthread_getspecific (br_thread_key);
309
br_str_free (specific);
310
pthread_setspecific (br_thread_key, str);
312
#else /* BR_PTHREADS */
313
static int initialized = 0;
317
atexit (br_free_last_value);
322
free (br_last_value);
324
#endif /* BR_PTHREADS */
326
return (const char *) str;
333
* str2: Another string.
334
* Returns: A newly-allocated string. This string should be freed when no longer needed.
336
* Concatenate str1 and str2 to a newly allocated string.
339
br_strcat (const char *str1, const char *str2)
344
if (!str1) str1 = "";
345
if (!str2) str2 = "";
347
len1 = strlen (str1);
348
len2 = strlen (str2);
350
result = (char *) malloc (len1 + len2 + 1);
351
memcpy (result, str1, len1);
352
memcpy (result + len1, str2, len2);
353
result[len1 + len2] = '\0';
359
/* Emulates glibc's strndup() */
361
br_strndup (char *str, size_t size)
363
char *result = (char *) NULL;
366
br_return_val_if_fail (str != (char *) NULL, (char *) NULL);
369
if (!len) return strdup ("");
370
if (size > len) size = len;
372
result = (char *) calloc (sizeof (char), len + 1);
373
memcpy (result, str, size);
381
* Returns: A directory name. This string should be freed when no longer needed.
383
* Extracts the directory component of path. Similar to g_dirname() or the dirname
384
* commandline application.
387
* br_extract_dir ("/usr/local/foobar"); --> Returns: "/usr/local"
390
br_extract_dir (const char *path)
394
br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
396
end = strrchr (path, '/');
397
if (!end) return strdup (".");
399
while (end > path && *end == '/')
401
result = br_strndup ((char *) path, end - path + 1);
413
* path: The full path of an executable or library.
414
* Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
416
* Extracts the prefix from path. This function assumes that your executable
417
* or library is installed in an LSB-compatible directory structure.
420
* br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr"
421
* br_extract_prefix ("/usr/local/lib/libfoo.so"); --> Returns "/usr/local"
422
* br_extract_prefix ("/usr/local/libfoo.so"); --> Returns "/usr"
425
br_extract_prefix (const char *path)
427
char *end, *tmp, *result;
429
br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
431
if (!*path) return strdup ("/");
432
end = strrchr (path, '/');
433
if (!end) return strdup (path);
435
tmp = br_strndup ((char *) path, end - path);
441
end = strrchr (tmp, '/');
442
if (!end) return tmp;
444
result = br_strndup (tmp, end - tmp);
450
result = strdup ("/");
459
#endif /* __cplusplus */
461
#endif /* _PREFIX_C */