2006-09-18 15:43:50 -05:00
|
|
|
/*
|
|
|
|
* BinReloc - a library for creating relocatable executables
|
|
|
|
* Written by: Hongli Lai <h.lai@chello.nl>
|
|
|
|
* http://autopackage.org/
|
|
|
|
*
|
|
|
|
* This source code is public domain. You can relicense this code
|
|
|
|
* under whatever license you want.
|
|
|
|
*
|
|
|
|
* See http://autopackage.org/docs/binreloc/ for
|
|
|
|
* more information and how to use this.
|
|
|
|
*/
|
2015-09-29 14:08:48 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2006-09-18 15:43:50 -05:00
|
|
|
|
|
|
|
#ifndef __BINRELOC_C__
|
|
|
|
#define __BINRELOC_C__
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2006-09-18 15:43:50 -05:00
|
|
|
|
2015-05-22 21:50:25 -05:00
|
|
|
#include <platform.h>
|
|
|
|
#if PLATFORM(WINDOWS)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2006-09-18 15:43:50 -05:00
|
|
|
#ifdef ENABLE_BINRELOC
|
2010-02-17 23:31:54 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2009-07-13 15:39:44 -05:00
|
|
|
#ifdef MAC_INTEGRATION
|
2015-05-24 20:05:04 -05:00
|
|
|
#include <gtkmacintegration/gtkosxapplication.h>
|
2009-07-13 15:39:44 -05:00
|
|
|
#endif
|
2006-09-18 15:43:50 -05:00
|
|
|
#endif /* ENABLE_BINRELOC */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "binreloc.h"
|
2017-12-05 16:48:45 -06:00
|
|
|
#include "gnc-filepath-utils.h"
|
2006-09-19 04:03:36 -05:00
|
|
|
#include <glib.h>
|
2017-12-05 16:48:45 -06:00
|
|
|
#include "gncla-dir.h"
|
2006-09-18 15:43:50 -05:00
|
|
|
|
|
|
|
G_BEGIN_DECLS
|
|
|
|
|
|
|
|
/** @internal
|
|
|
|
* Find the canonical filename of the executable. Returns the filename
|
|
|
|
* (which must be freed) or NULL on error. If the parameter 'error' is
|
2010-09-01 10:30:11 -05:00
|
|
|
* not NULL, the error code will be stored there, if an error occurred.
|
2006-09-18 15:43:50 -05:00
|
|
|
*/
|
|
|
|
static char *
|
2010-03-20 19:40:24 -05:00
|
|
|
_br_find_exe (Gnc_GbrInitError *error)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
|
|
|
#ifndef ENABLE_BINRELOC
|
2010-02-17 23:31:54 -06:00
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_DISABLED;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
2006-09-18 15:58:01 -05:00
|
|
|
#else
|
2006-09-19 04:03:36 -05:00
|
|
|
#ifdef G_OS_WIN32
|
2010-02-17 23:31:54 -06:00
|
|
|
/* I *thought* this program code already included the
|
|
|
|
relocation code for windows. Unfortunately this is not
|
|
|
|
the case and we have to add this manually. This is only
|
|
|
|
one possibility; other ways of looking up the full path
|
2010-08-31 09:00:56 -05:00
|
|
|
of gnucash.exe probably exist.*/
|
2010-02-17 23:31:54 -06:00
|
|
|
gchar *prefix;
|
|
|
|
gchar *result;
|
|
|
|
|
|
|
|
/* From the glib docs: When passed NULL, this function looks
|
|
|
|
up installation the directory of the main executable of
|
|
|
|
the current process */
|
|
|
|
prefix = g_win32_get_package_installation_directory_of_module (NULL);
|
|
|
|
result = g_build_filename (prefix,
|
2017-12-05 16:48:45 -06:00
|
|
|
BINDIR, "gnucash.exe",
|
2010-02-17 23:31:54 -06:00
|
|
|
(char*)NULL);
|
|
|
|
g_free (prefix);
|
|
|
|
return result;
|
2010-03-20 19:40:24 -05:00
|
|
|
#elif defined MAC_INTEGRATION
|
2012-09-29 13:29:21 -05:00
|
|
|
gchar *path = gtkosx_application_get_executable_path();
|
2011-10-08 14:14:01 -05:00
|
|
|
g_print ("Application Path %s\n", path);
|
|
|
|
return path;
|
2006-09-18 15:43:50 -05:00
|
|
|
#else
|
2010-02-17 23:31:54 -06:00
|
|
|
char *path, *path2, *line, *result;
|
|
|
|
size_t buf_size;
|
|
|
|
ssize_t size;
|
|
|
|
struct stat stat_buf;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* Read from /proc/self/exe (symlink) */
|
|
|
|
if (sizeof (path) > SSIZE_MAX)
|
|
|
|
buf_size = SSIZE_MAX - 1;
|
|
|
|
else
|
|
|
|
buf_size = PATH_MAX - 1;
|
|
|
|
path = (char *) g_try_malloc (buf_size);
|
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
/* Cannot allocate memory. */
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_NOMEM;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
path2 = (char *) g_try_malloc (buf_size);
|
|
|
|
if (path2 == NULL)
|
|
|
|
{
|
|
|
|
/* Cannot allocate memory. */
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_NOMEM;
|
2010-02-17 23:31:54 -06:00
|
|
|
g_free (path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy (path2, "/proc/self/exe", buf_size - 1);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
size = readlink (path2, path, buf_size - 1);
|
|
|
|
if (size == -1)
|
|
|
|
{
|
|
|
|
/* Error. */
|
|
|
|
g_free (path2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readlink() success. */
|
|
|
|
path[size] = '\0';
|
|
|
|
|
|
|
|
/* Check whether the symlink's target is also a symlink.
|
|
|
|
* We want to get the final target. */
|
|
|
|
i = stat (path, &stat_buf);
|
|
|
|
if (i == -1)
|
|
|
|
{
|
|
|
|
/* Error. */
|
|
|
|
g_free (path2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stat() success. */
|
|
|
|
if (!S_ISLNK (stat_buf.st_mode))
|
|
|
|
{
|
|
|
|
/* path is not a symlink. Done. */
|
|
|
|
g_free (path2);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* path is a symlink. Continue loop and resolve this. */
|
|
|
|
strncpy (path, path2, buf_size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* readlink() or stat() failed; this can happen when the program is
|
|
|
|
* running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
|
|
|
|
|
|
|
|
buf_size = PATH_MAX + 128;
|
|
|
|
line = (char *) g_try_realloc (path, buf_size);
|
|
|
|
if (line == NULL)
|
|
|
|
{
|
|
|
|
/* Cannot allocate memory. */
|
|
|
|
g_free (path);
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_NOMEM;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen ("/proc/self/maps", "r");
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
g_free (line);
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_OPEN_MAPS;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The first entry should be the executable name. */
|
|
|
|
result = fgets (line, (int) buf_size, f);
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
fclose (f);
|
|
|
|
g_free (line);
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_READ_MAPS;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get rid of newline character. */
|
|
|
|
buf_size = strlen (line);
|
|
|
|
if (buf_size <= 0)
|
|
|
|
{
|
|
|
|
/* Huh? An empty string? */
|
|
|
|
fclose (f);
|
|
|
|
g_free (line);
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_INVALID_MAPS;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (line[buf_size - 1] == 10)
|
|
|
|
line[buf_size - 1] = 0;
|
|
|
|
|
|
|
|
/* Extract the filename; it is always an absolute path. */
|
|
|
|
path = strchr (line, '/');
|
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
if (strstr (line, " r-xp ") == NULL || path == NULL)
|
|
|
|
{
|
|
|
|
fclose (f);
|
|
|
|
g_free (line);
|
|
|
|
if (error)
|
2010-03-20 19:40:24 -05:00
|
|
|
*error = GNC_GBR_INIT_ERROR_INVALID_MAPS;
|
2010-02-17 23:31:54 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = g_strdup (path);
|
|
|
|
g_free (line);
|
|
|
|
fclose (f);
|
|
|
|
return path;
|
2006-09-18 15:58:01 -05:00
|
|
|
#endif /* G_OS_WINDOWS */
|
2006-09-18 15:43:50 -05:00
|
|
|
#endif /* ENABLE_BINRELOC */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-27 17:08:17 -05:00
|
|
|
static gchar *exe = NULL;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-27 17:08:17 -05:00
|
|
|
static void set_gerror (GError **error, Gnc_GbrInitError errcode);
|
2010-02-17 23:31:54 -06:00
|
|
|
|
|
|
|
|
2010-03-27 17:08:17 -05:00
|
|
|
void gnc_gbr_set_exe (const gchar* default_exe)
|
|
|
|
{
|
|
|
|
if (exe != NULL)
|
|
|
|
g_free(exe);
|
|
|
|
exe = NULL;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-27 17:08:17 -05:00
|
|
|
if (default_exe != NULL)
|
|
|
|
exe = g_strdup(default_exe);
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Initialize the BinReloc library (for applications).
|
|
|
|
*
|
|
|
|
* This function must be called before using any other BinReloc functions.
|
|
|
|
* It attempts to locate the application's canonical filename.
|
|
|
|
*
|
|
|
|
* @note If you want to use BinReloc for a library, then you should call
|
2010-03-20 19:40:24 -05:00
|
|
|
* gnc_gbr_init_lib() instead.
|
2006-09-18 15:43:50 -05:00
|
|
|
*
|
|
|
|
* @param error If BinReloc failed to initialize, then the error report will
|
|
|
|
* be stored in this variable. Set to NULL if you don't want an
|
2010-03-20 19:40:24 -05:00
|
|
|
* error report. See the #Gnc_GbrInitError for a list of error
|
2006-09-18 15:43:50 -05:00
|
|
|
* codes.
|
|
|
|
*
|
|
|
|
* @returns TRUE on success, FALSE if BinReloc failed to initialize.
|
|
|
|
*/
|
|
|
|
gboolean
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_init (GError **error)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2010-03-20 19:40:24 -05:00
|
|
|
Gnc_GbrInitError errcode = 0;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
|
|
|
/* Locate the application's filename. */
|
|
|
|
exe = _br_find_exe (&errcode);
|
|
|
|
if (exe != NULL)
|
|
|
|
/* Success! */
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Failed :-( */
|
|
|
|
set_gerror (error, errcode);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-03-20 19:40:24 -05:00
|
|
|
set_gerror (GError **error, Gnc_GbrInitError errcode)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2010-02-17 23:31:54 -06:00
|
|
|
gchar *error_message;
|
|
|
|
|
|
|
|
if (error == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (errcode)
|
|
|
|
{
|
2010-03-20 19:40:24 -05:00
|
|
|
case GNC_GBR_INIT_ERROR_NOMEM:
|
2010-02-17 23:31:54 -06:00
|
|
|
error_message = "Cannot allocate memory.";
|
|
|
|
break;
|
2010-03-20 19:40:24 -05:00
|
|
|
case GNC_GBR_INIT_ERROR_OPEN_MAPS:
|
2010-02-17 23:31:54 -06:00
|
|
|
error_message = "Unable to open /proc/self/maps for reading.";
|
|
|
|
break;
|
2010-03-20 19:40:24 -05:00
|
|
|
case GNC_GBR_INIT_ERROR_READ_MAPS:
|
2010-02-17 23:31:54 -06:00
|
|
|
error_message = "Unable to read from /proc/self/maps.";
|
|
|
|
break;
|
2010-03-20 19:40:24 -05:00
|
|
|
case GNC_GBR_INIT_ERROR_INVALID_MAPS:
|
2010-02-17 23:31:54 -06:00
|
|
|
error_message = "The file format of /proc/self/maps is invalid.";
|
|
|
|
break;
|
2010-03-20 19:40:24 -05:00
|
|
|
case GNC_GBR_INIT_ERROR_DISABLED:
|
2010-02-17 23:31:54 -06:00
|
|
|
error_message = "Binary relocation support is disabled.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_message = "Unknown error.";
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
g_set_error (error, g_quark_from_static_string ("GBinReloc"),
|
|
|
|
errcode, "%s", error_message);
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Find the canonical filename of the current application.
|
|
|
|
*
|
|
|
|
* @param default_exe A default filename which will be used as fallback.
|
|
|
|
* @returns A string containing the application's canonical filename,
|
|
|
|
* which must be freed when no longer necessary. If BinReloc is
|
|
|
|
* not initialized, or if the initialization function failed,
|
|
|
|
* then a copy of default_exe will be returned. If default_exe
|
|
|
|
* is NULL, then NULL will be returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_exe (const gchar *default_exe)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2010-02-17 23:31:54 -06:00
|
|
|
if (exe == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc is not initialized. */
|
|
|
|
if (default_exe != NULL)
|
|
|
|
return g_strdup (default_exe);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return g_strdup (exe);
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Locate the directory in which the current application is installed.
|
|
|
|
*
|
|
|
|
* The prefix is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* dirname(exename)
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_dir A default directory which will used as fallback.
|
|
|
|
* @return A string containing the directory, which must be freed when no
|
|
|
|
* longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_dir
|
|
|
|
* will be returned. If default_dir is NULL, then NULL will be
|
|
|
|
* returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_exe_dir (const gchar *default_dir)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2010-02-17 23:31:54 -06:00
|
|
|
if (exe == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_dir != NULL)
|
|
|
|
return g_strdup (default_dir);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_path_get_dirname (exe);
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Locate the prefix in which the current application is installed.
|
|
|
|
*
|
|
|
|
* The prefix is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* dirname(dirname(exename))
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_prefix A default prefix which will used as fallback.
|
|
|
|
* @return A string containing the prefix, which must be freed when no
|
|
|
|
* longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_prefix
|
|
|
|
* will be returned. If default_prefix is NULL, then NULL will be
|
|
|
|
* returned.
|
|
|
|
*/
|
2017-12-05 16:48:45 -06:00
|
|
|
|
|
|
|
static inline gchar *
|
|
|
|
get_mac_bundle_prefix()
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2009-07-13 15:39:44 -05:00
|
|
|
#if defined ENABLE_BINRELOC && defined MAC_INTEGRATION
|
2012-09-29 13:29:21 -05:00
|
|
|
gchar *id = gtkosx_application_get_bundle_id ();
|
|
|
|
gchar *path = gtkosx_application_get_resource_path ();
|
2017-12-05 16:48:45 -06:00
|
|
|
if (id == NULL)
|
2010-06-25 13:44:40 -05:00
|
|
|
{
|
|
|
|
gchar *dirname = g_path_get_dirname (path);
|
|
|
|
g_free (path);
|
|
|
|
g_free (id);
|
|
|
|
return dirname;
|
2010-02-17 23:31:54 -06:00
|
|
|
}
|
2010-05-21 17:45:34 -05:00
|
|
|
g_free (id);
|
|
|
|
return path;
|
2017-12-05 16:48:45 -06:00
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gchar*
|
|
|
|
get_builddir_prefix()
|
|
|
|
{
|
|
|
|
if (g_getenv ("GNC_UNINSTALLED"))
|
|
|
|
return g_strdup (g_getenv ("GNC_BUILDDIR"));
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-09-18 15:43:50 -05:00
|
|
|
|
2017-12-05 16:48:45 -06:00
|
|
|
gchar *
|
|
|
|
gnc_gbr_find_prefix (const gchar *default_prefix)
|
|
|
|
{
|
|
|
|
gchar *dir1, *dir2;
|
|
|
|
if ((dir2 = get_builddir_prefix()) || (dir2 = get_mac_bundle_prefix()))
|
|
|
|
return dir2;
|
2010-02-17 23:31:54 -06:00
|
|
|
if (exe == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_prefix != NULL)
|
|
|
|
return g_strdup (default_prefix);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dir1 = g_path_get_dirname (exe);
|
|
|
|
dir2 = g_path_get_dirname (dir1);
|
|
|
|
g_free (dir1);
|
|
|
|
return dir2;
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Locate the application's binary folder.
|
|
|
|
*
|
|
|
|
* The path is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* prefix + "/bin"
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_bin_dir A default path which will used as fallback.
|
|
|
|
* @return A string containing the bin folder's path, which must be freed when
|
|
|
|
* no longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_bin_dir will
|
|
|
|
* be returned. If default_bin_dir is NULL, then NULL will be returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_bin_dir (const gchar *default_bin_dir)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2017-12-05 16:48:45 -06:00
|
|
|
gchar *prefix, *dir, *bindir;
|
2010-03-20 19:40:24 -05:00
|
|
|
prefix = gnc_gbr_find_prefix (NULL);
|
2010-02-17 23:31:54 -06:00
|
|
|
if (prefix == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_bin_dir != NULL)
|
|
|
|
return g_strdup (default_bin_dir);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-05 16:48:45 -06:00
|
|
|
bindir = gnc_file_path_relative_part(PREFIX, BINDIR);
|
|
|
|
dir = g_build_filename (prefix, bindir, NULL);
|
|
|
|
g_free (bindir);
|
2010-02-17 23:31:54 -06:00
|
|
|
g_free (prefix);
|
|
|
|
return dir;
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Locate the application's data folder.
|
|
|
|
*
|
|
|
|
* The path is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* prefix + "/share"
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_data_dir A default path which will used as fallback.
|
|
|
|
* @return A string containing the data folder's path, which must be freed when
|
|
|
|
* no longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_data_dir
|
|
|
|
* will be returned. If default_data_dir is NULL, then NULL will be
|
|
|
|
* returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_data_dir (const gchar *default_data_dir)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2017-12-05 16:48:45 -06:00
|
|
|
gchar *prefix, *dir, *datadir;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-20 19:40:24 -05:00
|
|
|
prefix = gnc_gbr_find_prefix (NULL);
|
2010-02-17 23:31:54 -06:00
|
|
|
if (prefix == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_data_dir != NULL)
|
|
|
|
return g_strdup (default_data_dir);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:48:45 -06:00
|
|
|
datadir = gnc_file_path_relative_part(PREFIX, DATADIR);
|
|
|
|
dir = g_build_filename (prefix, datadir, NULL);
|
|
|
|
g_free (datadir);
|
2010-02-17 23:31:54 -06:00
|
|
|
g_free (prefix);
|
|
|
|
return dir;
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Locate the application's library folder.
|
|
|
|
*
|
|
|
|
* The path is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* prefix + "/lib"
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_lib_dir A default path which will used as fallback.
|
|
|
|
* @return A string containing the library folder's path, which must be freed when
|
|
|
|
* no longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_lib_dir will be returned.
|
|
|
|
* If default_lib_dir is NULL, then NULL will be returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_lib_dir (const gchar *default_lib_dir)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2017-12-05 16:48:45 -06:00
|
|
|
gchar *prefix, *dir, *libdir;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-20 19:40:24 -05:00
|
|
|
prefix = gnc_gbr_find_prefix (NULL);
|
2010-02-17 23:31:54 -06:00
|
|
|
if (prefix == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_lib_dir != NULL)
|
|
|
|
return g_strdup (default_lib_dir);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:48:45 -06:00
|
|
|
libdir = gnc_file_path_relative_part(PREFIX, LIBDIR);
|
|
|
|
dir = g_build_filename (prefix, libdir, NULL);
|
|
|
|
g_free (libdir);
|
2010-02-17 23:31:54 -06:00
|
|
|
g_free (prefix);
|
|
|
|
return dir;
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Locate the application's configuration files folder.
|
|
|
|
*
|
|
|
|
* The path is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* prefix + "/etc"
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_etc_dir A default path which will used as fallback.
|
|
|
|
* @return A string containing the etc folder's path, which must be freed when
|
|
|
|
* no longer necessary. If BinReloc is not initialized, or if the initialization
|
|
|
|
* function failed, then a copy of default_etc_dir will be returned.
|
|
|
|
* If default_etc_dir is NULL, then NULL will be returned.
|
|
|
|
*/
|
|
|
|
gchar *
|
2010-03-20 19:40:24 -05:00
|
|
|
gnc_gbr_find_etc_dir (const gchar *default_etc_dir)
|
2006-09-18 15:43:50 -05:00
|
|
|
{
|
2017-12-05 16:48:45 -06:00
|
|
|
gchar *prefix, *dir, *sysconfdir;
|
2010-02-17 23:31:54 -06:00
|
|
|
|
2010-03-20 19:40:24 -05:00
|
|
|
prefix = gnc_gbr_find_prefix (NULL);
|
2010-02-17 23:31:54 -06:00
|
|
|
if (prefix == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_etc_dir != NULL)
|
|
|
|
return g_strdup (default_etc_dir);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:48:45 -06:00
|
|
|
sysconfdir = gnc_file_path_relative_part(PREFIX, SYSCONFDIR);
|
|
|
|
dir = g_build_filename (prefix, sysconfdir, NULL);
|
|
|
|
g_free (sysconfdir);
|
2010-02-17 23:31:54 -06:00
|
|
|
g_free (prefix);
|
|
|
|
return dir;
|
2006-09-18 15:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
G_END_DECLS
|
|
|
|
|
|
|
|
#endif /* __BINRELOC_C__ */
|