- fix 2699
[oweals/gnunet.git] / src / util / os_installation.c
index ef3eda2ff21d877ac354229e8e6dd43418c37523..d500580bfa36a20c9ea29c6217ebb374ea766026 100644 (file)
@@ -143,6 +143,7 @@ DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
   return TRUE;
 }
 
+
 /**
  * Try to determine path with win32-specific function
  *
@@ -395,6 +396,9 @@ os_get_gnunet_path ()
 #if LINUX
   if (NULL != (ret = get_path_from_proc_maps ()))
     return ret;
+  /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
+  if (NULL != (ret = get_path_from_PATH ("gnunet-arm")))
+    return ret;
   if (NULL != (ret = get_path_from_proc_exe ()))
     return ret;
 #endif
@@ -428,7 +432,7 @@ os_get_gnunet_path ()
 static char *
 os_get_exec_path ()
 {
-  char *ret;
+  char *ret = NULL;
 
 #if LINUX
   if (NULL != (ret = get_path_from_proc_exe ()))
@@ -443,7 +447,7 @@ os_get_exec_path ()
     return ret;
 #endif
   /* other attempts here */
-  return NULL;
+  return ret;
 }
 
 
@@ -486,26 +490,27 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
     execpath[--n] = '\0';
 
   isbasedir = 1;
-  if ((n > 5) &&
-      ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
-       (0 == strcasecmp (&execpath[n - 5], "lib64"))))
+  if ((n > 6) &&
+      ((0 == strcasecmp (&execpath[n - 6], "/lib32")) ||
+       (0 == strcasecmp (&execpath[n - 6], "/lib64"))))
   {
-    if (GNUNET_OS_IPK_LIBDIR != dirkind)
+    if ( (GNUNET_OS_IPK_LIBDIR != dirkind) &&
+        (GNUNET_OS_IPK_LIBEXECDIR != dirkind) )
     {
       /* strip '/lib32' or '/lib64' */
-      execpath[n - 5] = '\0';
-      n -= 5;
+      execpath[n - 6] = '\0';
+      n -= 6;
     }
     else
       isbasedir = 0;
   }
-  else if ((n > 3) &&
-           ((0 == strcasecmp (&execpath[n - 3], "bin")) ||
-            (0 == strcasecmp (&execpath[n - 3], "lib"))))
+  else if ((n > 4) &&
+           ((0 == strcasecmp (&execpath[n - 4], "/bin")) ||
+            (0 == strcasecmp (&execpath[n - 4], "/lib"))))
   {
     /* strip '/bin' or '/lib' */
-    execpath[n - 3] = '\0';
-    n -= 3;
+    execpath[n - 4] = '\0';
+    n -= 4;
   }
   /* in case this was a directory named foo-bin, remove "foo-" */
   while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
@@ -521,8 +526,30 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
     break;
   case GNUNET_OS_IPK_LIBDIR:
     if (isbasedir)
+    {
       dirname =
           DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
+      tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
+      sprintf (tmp, "%s%s", execpath, dirname);
+      if ( (GNUNET_YES !=
+           GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
+          (4 == sizeof (void *)) )
+      {
+       GNUNET_free (tmp);
+       dirname =
+         DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
+       tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
+       sprintf (tmp, "%s%s", execpath, dirname);
+      }
+      if ( (GNUNET_YES !=
+           GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
+          (8 == sizeof (void *)) )
+      {
+       dirname =
+         DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
+      }
+      GNUNET_free (tmp);
+    }
     else
       dirname = DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR;
     break;
@@ -544,9 +571,38 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
         "gnunet" DIR_SEPARATOR_STR;
     break;
   case GNUNET_OS_IPK_LIBEXECDIR:
-    dirname =
+    if (isbasedir)
+    {
+      dirname =
         DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
         "libexec" DIR_SEPARATOR_STR;
+      tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
+      sprintf (tmp, "%s%s", execpath, dirname);
+      if ( (GNUNET_YES !=
+           GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
+          (4 == sizeof (void *)) )
+      {
+       GNUNET_free (tmp);
+       dirname =
+         DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
+         "libexec" DIR_SEPARATOR_STR;
+       tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1);
+       sprintf (tmp, "%s%s", execpath, dirname);
+      }
+      if ( (GNUNET_YES !=
+           GNUNET_DISK_directory_test (tmp, GNUNET_YES)) &&
+          (8 == sizeof (void *)) )
+      {
+       dirname =
+         DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
+         "libexec" DIR_SEPARATOR_STR;
+      }
+      GNUNET_free (tmp);
+    }
+    else
+      dirname =
+        DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
+        "libexec" DIR_SEPARATOR_STR;
     break;
   default:
     GNUNET_free (execpath);
@@ -573,6 +629,8 @@ GNUNET_OS_get_libexec_binary_path (const char *progname)
   char *libexecdir;
   char *binary;
 
+  if (DIR_SEPARATOR == progname[0])
+    return GNUNET_strdup (progname);
   libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
   if (NULL == libexecdir)
     return GNUNET_strdup (progname);