2 This file is part of GNUnet.
3 (C) 2006 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/util/os_installation.c
23 * @brief get paths used by the program
30 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_common.h"
42 #include "gnunet_configuration_lib.h"
43 #include "gnunet_disk_lib.h"
44 #include "gnunet_os_lib.h"
46 #include <mach-o/ldsyms.h>
47 #include <mach-o/dyld.h>
52 * Try to determine path by reading /proc/PID/exe
55 get_path_from_proc_maps ()
70 while (NULL != fgets (line, sizeof(line), f))
72 if ((1 == sscanf (line,
73 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
75 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
79 return GNUNET_strdup (dir);
87 * Try to determine path by reading /proc/PID/exe
90 get_path_from_proc_exe ()
97 sizeof(fn), "/proc/%u/exe", getpid ());
98 size = readlink (fn, lnk, sizeof (lnk)-1);
101 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
104 GNUNET_assert (size < sizeof (lnk));
106 while ((lnk[size] != '/') && (size > 0))
108 if ((size < 4) || (lnk[size - 4] != '/'))
110 /* not installed in "/bin/" -- binary path probably useless */
114 return GNUNET_strdup (lnk);
120 * Try to determine path with win32-specific function
123 get_path_from_module_filename ()
128 GetModuleFileName (NULL, path, sizeof(path)-1);
129 idx = path + strlen (path);
130 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
133 return GNUNET_strdup (path);
138 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
141 get_path_from_NSGetExecutablePath ()
143 static char zero = '\0';
146 MyNSGetExecutablePathProto func;
151 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
156 /* get the path len, including the trailing \0 */
160 path = GNUNET_malloc (len);
161 ret = func (path, &len);
168 while ((path[len] != '/') && (len > 0))
175 get_path_from_dyld_image ()
183 c = _dyld_image_count ();
184 for (i = 0; i < c; i++)
186 if (_dyld_get_image_header (i) == &_mh_dylib_header)
188 path = _dyld_get_image_name (i);
189 if (path != NULL && strlen (path) > 0)
193 while ((s > p) && (*s != '/'))
206 get_path_from_PATH ()
217 path = GNUNET_strdup (p); /* because we write on it */
218 buf = GNUNET_malloc (strlen (path) + 20);
221 while (NULL != (end = strchr (pos, ':')))
224 sprintf (buf, "%s/%s", pos, "gnunet-arm");
225 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
227 pos = GNUNET_strdup (pos);
234 sprintf (buf, "%s/%s", pos, "gnunet-arm");
235 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
237 pos = GNUNET_strdup (pos);
248 get_path_from_GNUNET_PREFIX ()
252 p = getenv ("GNUNET_PREFIX");
254 return GNUNET_strdup (p);
259 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
262 * @return a pointer to the executable path, or NULL on error
265 os_get_gnunet_path ()
269 ret = get_path_from_GNUNET_PREFIX ();
273 ret = get_path_from_proc_maps ();
276 ret = get_path_from_proc_exe ();
281 ret = get_path_from_module_filename ();
286 ret = get_path_from_dyld_image ();
289 ret = get_path_from_NSGetExecutablePath ();
293 ret = get_path_from_PATH ();
296 /* other attempts here */
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
306 * @brief get the path to current app's bin/
309 * @return a pointer to the executable path, or NULL on error
318 ret = get_path_from_proc_exe ();
323 ret = get_path_from_module_filename ();
328 ret = get_path_from_NSGetExecutablePath ();
332 /* other attempts here */
339 * @brief get the path to a specific GNUnet installation directory or,
340 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
342 * @return a pointer to the dir path (to be freed by the caller)
345 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
349 char *execpath = NULL;
353 /* if wanted, try to get the current app's bin/ */
354 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
355 execpath = os_get_exec_path ();
357 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
358 * guess for the current app */
359 if (execpath == NULL)
360 execpath = os_get_gnunet_path ();
362 if (execpath == NULL)
365 n = strlen (execpath);
368 /* should never happen, but better safe than sorry */
369 GNUNET_free (execpath);
372 /* remove filename itself */
373 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
374 execpath[--n] = '\0';
378 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
379 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
381 if (dirkind != GNUNET_OS_IPK_LIBDIR)
383 /* strip '/lib32' or '/lib64' */
384 execpath[n - 5] = '\0';
391 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
392 (0 == strcasecmp (&execpath[n - 3], "lib"))))
394 /* strip '/bin' or '/lib' */
395 execpath[n - 3] = '\0';
398 /* in case this was a directory named foo-bin, remove "foo-" */
399 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
400 execpath[--n] = '\0';
403 case GNUNET_OS_IPK_PREFIX:
404 case GNUNET_OS_IPK_SELF_PREFIX:
405 dirname = DIR_SEPARATOR_STR;
407 case GNUNET_OS_IPK_BINDIR:
408 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
410 case GNUNET_OS_IPK_LIBDIR:
413 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
416 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
418 case GNUNET_OS_IPK_DATADIR:
420 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
423 case GNUNET_OS_IPK_LOCALEDIR:
425 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
429 GNUNET_free (execpath);
432 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
433 sprintf (tmp, "%s%s", execpath, dirname);
434 GNUNET_free (execpath);
438 #if 0 /* keep Emacsens' auto-indent happy */
444 /* end of os_installation.c */