source: filezilla/trunk/fuentes/src/interface/prefix.cpp @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 9.7 KB
Line 
1/*
2 * BinReloc - a library for creating relocatable executables
3 * Written by: Mike Hearn <mike@theoretic.com>
4 *             Hongli Lai <h.lai@chello.nl>
5 * http://autopackage.org/
6 *
7 * This source code is public domain. You can relicense this code
8 * under whatever license you want.
9 *
10 * NOTE: if you're using C++ and are getting "undefined reference
11 * to br_*", try renaming prefix.c to prefix.cpp
12 */
13
14/* WARNING, BEFORE YOU MODIFY PREFIX.C:
15 *
16 * If you make changes to any of the functions in prefix.c, you MUST
17 * change the BR_NAMESPACE macro (in prefix.h).
18 * This way you can avoid symbol table conflicts with other libraries
19 * that also happen to use BinReloc.
20 *
21 * Example:
22 * #define BR_NAMESPACE(funcName) foobar_ ## funcName
23 * --> expands br_locate to foobar_br_locate
24 */
25
26#ifndef _PREFIX_C_
27#define _PREFIX_C_
28
29#ifdef HAVE_CONFIG_H
30        #include "config.h"
31#endif
32
33#ifdef ENABLE_BINRELOC
34
35#ifndef BR_PTHREADS
36        /* Change 1 to 0 if you don't want pthread support */
37        #define BR_PTHREADS 1
38#endif /* BR_PTHREADS */
39
40#include <stdlib.h>
41#include <stdio.h>
42#include <limits.h>
43#include <string.h>
44#include "prefix.h"
45
46#ifndef NULL
47#define NULL 0; //Changed from ((void *) 0) to avoid compile error
48#endif
49
50#ifdef __GNUC__
51        #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
52#else
53        #define br_return_val_if_fail(expr,val) if (!(expr)) return val
54#endif /* __GNUC__ */
55
56
57#include <sys/types.h>
58#include <sys/stat.h>
59#include <sys/param.h>
60#include <unistd.h>
61
62
63/**
64 * br_locate:
65 * symbol: A symbol that belongs to the app/library you want to locate.
66 * Returns: A newly allocated string containing the full path of the
67 *          app/library that func belongs to, or NULL on error. This
68 *          string should be freed when not when no longer needed.
69 *
70 * Finds out to which application or library symbol belongs, then locate
71 * the full path of that application or library.
72 * Note that symbol cannot be a pointer to a function. That will not work.
73 *
74 * Example:
75 * --> main.c
76 * #include "prefix.h"
77 * #include "libfoo.h"
78 *
79 * int main (int argc, char *argv[]) {
80 *      printf ("Full path of this app: %s\n", br_locate (&argc));
81 *      libfoo_start ();
82 *      return 0;
83 * }
84 *
85 * --> libfoo.c starts here
86 * #include "prefix.h"
87 *
88 * void libfoo_start () {
89 *      --> "" is a symbol that belongs to libfoo (because it's called
90 *      --> from libfoo_start()); that's why this works.
91 *      printf ("libfoo is located in: %s\n", br_locate (""));
92 * }
93 */
94char *
95br_locate (void *symbol)
96{
97        char line[5000];
98        FILE *f;
99        char *path;
100
101        br_return_val_if_fail (symbol != NULL, NULL);
102
103        f = fopen ("/proc/self/maps", "r");
104        if (!f)
105                return NULL;
106
107        while (!feof (f))
108        {
109                unsigned long start, end;
110
111                if (!fgets (line, sizeof (line), f))
112                        continue;
113                if (!strstr (line, " r-xp ") || !strchr (line, '/'))
114                        continue;
115
116                sscanf (line, "%lx-%lx ", &start, &end);
117                if (symbol >= (void *) start && symbol < (void *) end)
118                {
119                        char *tmp;
120                        size_t len;
121
122                        /* Extract the filename; it is always an absolute path */
123                        path = strchr (line, '/');
124
125                        /* Get rid of the newline */
126                        tmp = strrchr (path, '\n');
127                        if (tmp) *tmp = 0;
128
129                        /* Get rid of "(deleted)" */
130                        len = strlen (path);
131                        if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
132                        {
133                                tmp = path + len - 10;
134                                *tmp = 0;
135                        }
136
137                        fclose(f);
138                        return strdup (path);
139                }
140        }
141
142        fclose (f);
143        return NULL;
144}
145
146
147/**
148 * br_locate_prefix:
149 * symbol: A symbol that belongs to the app/library you want to locate.
150 * Returns: A prefix. This string should be freed when no longer needed.
151 *
152 * Locates the full path of the app/library that symbol belongs to, and return
153 * the prefix of that path, or NULL on error.
154 * Note that symbol cannot be a pointer to a function. That will not work.
155 *
156 * Example:
157 * --> This application is located in /usr/bin/foo
158 * br_locate_prefix (&argc);   --> returns: "/usr"
159 */
160char *
161br_locate_prefix (void *symbol)
162{
163        char *path, *prefix;
164
165        br_return_val_if_fail (symbol != NULL, NULL);
166
167        path = br_locate (symbol);
168        if (!path) return NULL;
169
170        prefix = br_extract_prefix (path);
171        free (path);
172        return prefix;
173}
174
175
176/**
177 * br_prepend_prefix:
178 * symbol: A symbol that belongs to the app/library you want to locate.
179 * path: The path that you want to prepend the prefix to.
180 * Returns: The new path, or NULL on error. This string should be freed when no
181 *          longer needed.
182 *
183 * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
184 * Note that symbol cannot be a pointer to a function. That will not work.
185 *
186 * Example:
187 * --> The application is /usr/bin/foo
188 * br_prepend_prefix (&argc, "/share/foo/data.png");   --> Returns "/usr/share/foo/data.png"
189 */
190char *
191br_prepend_prefix (void *symbol, const char *path)
192{
193        char *tmp, *newpath;
194
195        br_return_val_if_fail (symbol != NULL, NULL);
196        br_return_val_if_fail (path != NULL, NULL);
197
198        tmp = br_locate_prefix (symbol);
199        if (!tmp) return NULL;
200
201        if (strcmp (tmp, "/") == 0)
202                newpath = strdup (path);
203        else
204                newpath = br_strcat (tmp, path);
205
206        /* Get rid of compiler warning ("br_prepend_prefix never used") */
207        if (0) br_prepend_prefix (NULL, NULL);
208
209        free (tmp);
210        return newpath;
211}
212
213
214/* Pthread stuff for thread safetiness */
215#if BR_PTHREADS
216
217#include <pthread.h>
218
219static pthread_key_t br_thread_key;
220static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
221
222
223static void
224br_thread_local_store_fini ()
225{
226        char *specific;
227
228        specific = (char *) pthread_getspecific (br_thread_key);
229        if (specific)
230        {
231                free (specific);
232                pthread_setspecific (br_thread_key, NULL);
233        }
234        pthread_key_delete (br_thread_key);
235        br_thread_key = 0;
236}
237
238
239static void
240br_str_free (void *str)
241{
242        if (str)
243                free (str);
244}
245
246
247static void
248br_thread_local_store_init ()
249{
250        if (pthread_key_create (&br_thread_key, br_str_free) == 0)
251                atexit (br_thread_local_store_fini);
252}
253
254#else /* BR_PTHREADS */
255
256static char *br_last_value = (char *) NULL;
257
258static void
259br_free_last_value ()
260{
261        if (br_last_value)
262                free (br_last_value);
263}
264
265#endif /* BR_PTHREADS */
266
267
268/**
269 * br_thread_local_store:
270 * str: A dynamically allocated string.
271 * Returns: str. This return value must not be freed.
272 *
273 * Store str in a thread-local variable and return str. The next
274 * you run this function, that variable is freed too.
275 * This function is created so you don't have to worry about freeing
276 * strings.
277 *
278 * Example:
279 * char *foo;
280 * foo = thread_local_store (strdup ("hello")); --> foo == "hello"
281 * foo = thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
282 */
283const char *
284br_thread_local_store (char *str)
285{
286        #if BR_PTHREADS
287                char *specific;
288
289                pthread_once (&br_thread_key_once, br_thread_local_store_init);
290
291                specific = (char *) pthread_getspecific (br_thread_key);
292                br_str_free (specific);
293                pthread_setspecific (br_thread_key, str);
294
295        #else /* BR_PTHREADS */
296                static int initialized = 0;
297
298                if (!initialized)
299                {
300                        atexit (br_free_last_value);
301                        initialized = 1;
302                }
303
304                if (br_last_value)
305                        free (br_last_value);
306                br_last_value = str;
307        #endif /* BR_PTHREADS */
308
309        return (const char *) str;
310}
311
312
313/**
314 * br_strcat:
315 * str1: A string.
316 * str2: Another string.
317 * Returns: A newly-allocated string. This string should be freed when no longer needed.
318 *
319 * Concatenate str1 and str2 to a newly allocated string.
320 */
321char *
322br_strcat (const char *str1, const char *str2)
323{
324        char *result;
325        size_t len1, len2;
326
327        if (!str1) str1 = "";
328        if (!str2) str2 = "";
329
330        len1 = strlen (str1);
331        len2 = strlen (str2);
332
333        result = (char *) malloc (len1 + len2 + 1);
334        memcpy (result, str1, len1);
335        memcpy (result + len1, str2, len2);
336        result[len1 + len2] = '\0';
337
338        return result;
339}
340
341
342/* Emulates glibc's strndup() */
343static char *
344br_strndup (char *str, size_t size)
345{
346        char *result = (char *) NULL;
347        size_t len;
348
349        br_return_val_if_fail (str != (char *) NULL, (char *) NULL);
350
351        len = strlen (str);
352        if (!len) return strdup ("");
353        if (size > len) size = len;
354
355        result = (char *) calloc (sizeof (char), len + 1);
356        memcpy (result, str, size);
357        return result;
358}
359
360
361/**
362 * br_extract_dir:
363 * path: A path.
364 * Returns: A directory name. This string should be freed when no longer needed.
365 *
366 * Extracts the directory component of path. Similar to g_dirname() or the dirname
367 * commandline application.
368 *
369 * Example:
370 * br_extract_dir ("/usr/local/foobar");  --> Returns: "/usr/local"
371 */
372char *
373br_extract_dir (const char *path)
374{
375        const char *end;
376        char *result;
377
378        br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
379
380        end = strrchr (path, '/');
381        if (!end) return strdup (".");
382
383        while (end > path && *end == '/')
384                end--;
385        result = br_strndup ((char *) path, end - path + 1);
386        if (!*result)
387        {
388                free (result);
389                return strdup ("/");
390        } else
391                return result;
392}
393
394
395/**
396 * br_extract_prefix:
397 * path: The full path of an executable or library.
398 * Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
399 *
400 * Extracts the prefix from path. This function assumes that your executable
401 * or library is installed in an LSB-compatible directory structure.
402 *
403 * Example:
404 * br_extract_prefix ("/usr/bin/gnome-panel");       --> Returns "/usr"
405 * br_extract_prefix ("/usr/local/lib/libfoo.so");   --> Returns "/usr/local"
406 * br_extract_prefix ("/usr/local/libfoo.so");       --> Returns "/usr"
407 */
408char *
409br_extract_prefix (const char *path)
410{
411        const char *end;
412        char *tmp, *result;
413
414        br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
415
416        if (!*path) return strdup ("/");
417        end = strrchr (path, '/');
418        if (!end) return strdup (path);
419
420        tmp = br_strndup ((char *) path, end - path);
421        if (!*tmp)
422        {
423                free (tmp);
424                return strdup ("/");
425        }
426        end = strrchr (tmp, '/');
427        if (!end) return tmp;
428
429        result = br_strndup (tmp, end - tmp);
430        free (tmp);
431
432        if (!*result)
433        {
434                free (result);
435                result = strdup ("/");
436        }
437
438        return result;
439}
440
441#endif /* ENABLE_BINRELOC */
442
443#endif /* _PREFIX_C */
Note: See TracBrowser for help on using the repository browser.