prefix.cpp revision e358d623c19312d9b8f76433cfda624625b51ad8
/*
* BinReloc - a library for creating relocatable executables
* Written by: Mike Hearn <mike@theoretic.com>
* Hongli Lai <h.lai@chello.nl>
*
* This source code is public domain. You can relicense this code
* under whatever license you want.
*
* NOTE: if you're using C++ and are getting "undefined reference
* to br_*", try renaming prefix.c to prefix.cpp
*/
/* WARNING, BEFORE YOU MODIFY PREFIX.C:
*
* If you make changes to any of the functions in prefix.c, you MUST
* change the BR_NAMESPACE macro (in prefix.h).
* This way you can avoid symbol table conflicts with other libraries
* that also happen to use BinReloc.
*
* Example:
* #define BR_NAMESPACE(funcName) foobar_ ## funcName
* --> expands br_locate to foobar_br_locate
*/
#ifndef _PREFIX_C_
#define _PREFIX_C_
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <limits.h>
#include "prefix.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// Why is NULL being redefined?! This breaks compilation on Windows, as it disables the safe assignment of NULL to other pointer types.
#ifndef __WIN32__
# define NULL ((void *) 0)
#endif
#ifdef __GNUC__
#define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
#else
#endif /* __GNUC__ */
#ifdef ENABLE_BINRELOC
#include <unistd.h>
/**
* br_locate:
* Returns: A newly allocated string containing the full path of the
* string should be freed when not when no longer needed.
*
* Finds out to which application or library symbol belongs, then locate
* the full path of that application or library.
* Note that symbol cannot be a pointer to a function. That will not work.
*
* Example:
* --> main.c
* #include "prefix.h"
* #include "libfoo.h"
*
* int main (int argc, char *argv[]) {
* printf ("Full path of this app: %s\n", br_locate (&argc));
* libfoo_start ();
* return 0;
* }
*
* --> libfoo.c starts here
* #include "prefix.h"
*
* void libfoo_start () {
* --> "" is a symbol that belongs to libfoo (because it's called
* --> from libfoo_start()); that's why this works.
* printf ("libfoo is located in: %s\n", br_locate (""));
* }
*/
char *
{
char line[5000];
FILE *f;
char *path;
if (!f)
return NULL;
while (!feof (f))
{
continue;
continue;
{
char *tmp;
/* Extract the filename; it is always an absolute path */
/* Get rid of the newline */
/* Get rid of "(deleted)" */
{
*tmp = 0;
}
fclose(f);
}
}
fclose (f);
return NULL;
}
/**
* br_locate_prefix:
* Returns: A prefix. This string should be freed when no longer needed.
*
* the prefix of that path, or NULL on error.
* Note that symbol cannot be a pointer to a function. That will not work.
*
* Example:
* br_locate_prefix (&argc); --> returns: "/usr"
*/
char *
br_locate_prefix (void *symbol)
{
return prefix;
}
/**
* br_prepend_prefix:
* path: The path that you want to prepend the prefix to.
* Returns: The new path, or NULL on error. This string should be freed when no
* longer needed.
*
* Note that symbol cannot be a pointer to a function. That will not work.
*
* Example:
*/
char *
{
else
/* Get rid of compiler warning ("br_prepend_prefix never used") */
return newpath;
}
#endif /* ENABLE_BINRELOC */
/* Thread stuff for thread safetiness */
#if BR_THREADS
/*
We do not need local store init() or fini(), because
g_private_new (g_free) will take care of all of that
for us. Isn't GLib wonderful?
*/
#else /* !BR_THREADS */
static char *br_last_value = (char*)NULL;
static void
{
if (br_last_value)
}
#endif /* BR_THREADS */
/**
* br_thread_local_store:
* str: A dynamically allocated string.
* Returns: str. This return value must not be freed.
*
* Store str in a thread-local variable and return str. The next
* you run this function, that variable is freed too.
* This function is created so you don't have to worry about freeing
* strings.
*
* Example:
* char *foo;
* foo = thread_local_store (strdup ("hello")); --> foo == "hello"
* foo = thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
*/
const char *
br_thread_local_store (char *str)
{
#if BR_THREADS
if (!g_thread_supported ())
{
}
if (specific)
#else /* !BR_THREADS */
static int initialized = 0;
if (!initialized)
{
initialized = 1;
}
if (br_last_value)
br_last_value = str;
#endif /* BR_THREADS */
return (const char *) str;
}
/**
* br_strcat:
* str1: A string.
* str2: Another string.
* Returns: A newly-allocated string. This string should be freed when no longer needed.
*
* Concatenate str1 and str2 to a newly allocated string.
*/
char *
{
char *result;
return result;
}
/* Emulates glibc's strndup() */
static char *
{
return result;
}
/**
* br_extract_dir:
* path: A path.
* Returns: A directory name. This string should be freed when no longer needed.
*
* Extracts the directory component of path. Similar to g_path_get_dirname()
* or the dirname commandline application.
*
* Example:
*/
char *
br_extract_dir (const char *path)
{
const char *end;
char *result;
end--;
if (!*result)
{
return strdup ("/");
} else
return result;
}
/**
* br_extract_prefix:
* path: The full path of an executable or library.
* Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
*
* Extracts the prefix from path. This function assumes that your executable
* or library is installed in an LSB-compatible directory structure.
*
* Example:
* br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr"
*/
char *
br_extract_prefix (const char *path)
{
const char *end;
if (!*tmp)
{
return strdup ("/");
}
if (!*result)
{
}
return result;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#ifdef __WIN32__
/**
* Provide a similar mechanism for Win32. Enable a macro,
* WIN32_DATADIR, that can look up subpaths for inkscape resources
*/
#include <windows.h>
/**
* Return the directory of the .exe that is currently running
*/
{
return ret;
}
/**
* Return the relocatable version of the datadir,
* probably c:\inkscape
*/
{
if (INKSCAPE_DATADIR && *INKSCAPE_DATADIR &&
{
dir += "\\";
dir += INKSCAPE_DATADIR;
}
return dir;
}
{
{
dir += "\\";
}
return dir;
}
/**
* This is the visible utility function
*/
char *win32_relative_path(const char *childPath)
{
static char *returnPath = 0;
if (!childPath)
childPath = "";
if (returnPath)
return returnPath;
}
#endif /* __WIN32__ */
#endif /* _PREFIX_C */