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
32 #include "gnunet_common.h"
33 #include "gnunet_configuration_lib.h"
34 #include "gnunet_disk_lib.h"
35 #include "gnunet_os_lib.h"
37 #include <mach-o/ldsyms.h>
38 #include <mach-o/dyld.h>
43 * Try to determine path by reading /proc/PID/exe
46 get_path_from_proc_maps ()
61 while (NULL != fgets (line, sizeof(line), f))
63 if ((1 == sscanf (line,
64 "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
66 (NULL != (lgu = strstr (dir, "libgnunetutil"))))
70 return GNUNET_strdup (dir);
78 * Try to determine path by reading /proc/PID/exe
81 get_path_from_proc_exe ()
88 sizeof(fn), "/proc/%u/exe", getpid ());
89 size = readlink (fn, lnk, sizeof (lnk)-1);
92 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
95 GNUNET_assert (size < sizeof (lnk));
97 while ((lnk[size] != '/') && (size > 0))
99 if ((size < 4) || (lnk[size - 4] != '/'))
101 /* not installed in "/bin/" -- binary path probably useless */
105 return GNUNET_strdup (lnk);
111 * Try to determine path with win32-specific function
114 get_path_from_module_filename ()
119 GetModuleFileName (NULL, path, sizeof(path)-1);
120 idx = path + strlen (path);
121 while ((idx > path) && (*idx != '\\') && (*idx != '/'))
124 return GNUNET_strdup (path);
129 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
132 get_path_from_NSGetExecutablePath ()
134 static char zero = '\0';
137 MyNSGetExecutablePathProto func;
142 (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath");
147 /* get the path len, including the trailing \0 */
151 path = GNUNET_malloc (len);
152 ret = func (path, &len);
159 while ((path[len] != '/') && (len > 0))
166 get_path_from_dyld_image ()
174 c = _dyld_image_count ();
175 for (i = 0; i < c; i++)
177 if (_dyld_get_image_header (i) == &_mh_dylib_header)
179 path = _dyld_get_image_name (i);
180 if (path != NULL && strlen (path) > 0)
184 while ((s > p) && (*s != '/'))
197 get_path_from_PATH ()
208 path = GNUNET_strdup (p); /* because we write on it */
209 buf = GNUNET_malloc (strlen (path) + 20);
212 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
215 sprintf (buf, "%s/%s", pos, "gnunet-arm");
216 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
218 pos = GNUNET_strdup (pos);
225 sprintf (buf, "%s/%s", pos, "gnunet-arm");
226 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
228 pos = GNUNET_strdup (pos);
239 get_path_from_GNUNET_PREFIX ()
243 p = getenv ("GNUNET_PREFIX");
245 return GNUNET_strdup (p);
250 * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
253 * @return a pointer to the executable path, or NULL on error
256 os_get_gnunet_path ()
260 ret = get_path_from_GNUNET_PREFIX ();
264 ret = get_path_from_proc_maps ();
267 ret = get_path_from_proc_exe ();
272 ret = get_path_from_module_filename ();
277 ret = get_path_from_dyld_image ();
280 ret = get_path_from_NSGetExecutablePath ();
284 ret = get_path_from_PATH ();
287 /* other attempts here */
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 ("Could not determine installation path for %s. Set `%s' environment variable.\n"),
297 * @brief get the path to current app's bin/
300 * @return a pointer to the executable path, or NULL on error
309 ret = get_path_from_proc_exe ();
314 ret = get_path_from_module_filename ();
319 ret = get_path_from_NSGetExecutablePath ();
323 /* other attempts here */
330 * @brief get the path to a specific GNUnet installation directory or,
331 * with GNUNET_IPK_SELF_PREFIX, the current running apps installation directory
333 * @return a pointer to the dir path (to be freed by the caller)
336 GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
340 char *execpath = NULL;
344 /* if wanted, try to get the current app's bin/ */
345 if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
346 execpath = os_get_exec_path ();
348 /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
349 * guess for the current app */
350 if (execpath == NULL)
351 execpath = os_get_gnunet_path ();
353 if (execpath == NULL)
356 n = strlen (execpath);
359 /* should never happen, but better safe than sorry */
360 GNUNET_free (execpath);
363 /* remove filename itself */
364 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
365 execpath[--n] = '\0';
369 ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
370 (0 == strcasecmp (&execpath[n - 5], "lib64"))))
372 if (dirkind != GNUNET_OS_IPK_LIBDIR)
374 /* strip '/lib32' or '/lib64' */
375 execpath[n - 5] = '\0';
382 ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
383 (0 == strcasecmp (&execpath[n - 3], "lib"))))
385 /* strip '/bin' or '/lib' */
386 execpath[n - 3] = '\0';
389 /* in case this was a directory named foo-bin, remove "foo-" */
390 while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
391 execpath[--n] = '\0';
394 case GNUNET_OS_IPK_PREFIX:
395 case GNUNET_OS_IPK_SELF_PREFIX:
396 dirname = DIR_SEPARATOR_STR;
398 case GNUNET_OS_IPK_BINDIR:
399 dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR;
401 case GNUNET_OS_IPK_LIBDIR:
404 DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet"
407 dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
409 case GNUNET_OS_IPK_DATADIR:
411 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet"
414 case GNUNET_OS_IPK_LOCALEDIR:
416 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
419 case GNUNET_OS_IPK_ICONDIR:
421 DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
424 GNUNET_free (execpath);
427 tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
428 sprintf (tmp, "%s%s", execpath, dirname);
429 GNUNET_free (execpath);
433 /* end of os_installation.c */