gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r24884 - gnunet-gtk/src/lib


From: gnunet
Subject: [GNUnet-SVN] r24884 - gnunet-gtk/src/lib
Date: Sat, 10 Nov 2012 14:42:01 +0100

Author: grothoff
Date: 2012-11-10 14:42:01 +0100 (Sat, 10 Nov 2012)
New Revision: 24884

Modified:
   gnunet-gtk/src/lib/os_installation.c
Log:
unify os_installation implementation with the one from GNUnet-main

Modified: gnunet-gtk/src/lib/os_installation.c
===================================================================
--- gnunet-gtk/src/lib/os_installation.c        2012-11-10 13:41:43 UTC (rev 
24883)
+++ gnunet-gtk/src/lib/os_installation.c        2012-11-10 13:42:01 UTC (rev 
24884)
@@ -40,6 +40,8 @@
 #if LINUX
 /**
  * Try to determine path by reading /proc/PID/exe
+ *
+ * @return NULL on error
  */
 static char *
 get_path_from_proc_maps ()
@@ -51,8 +53,7 @@
   char *lgu;
 
   GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ());
-  f = fopen (fn, "r");
-  if (f == NULL)
+  if (NULL == (f = FOPEN (fn, "r")))
     return NULL;
   while (NULL != fgets (line, sizeof (line), f))
   {
@@ -69,8 +70,11 @@
   return NULL;
 }
 
+
 /**
  * Try to determine path by reading /proc/PID/exe
+ *
+ * @return NULL on error
  */
 static char *
 get_path_from_proc_exe ()
@@ -90,6 +94,11 @@
   lnk[size] = '\0';
   while ((lnk[size] != '/') && (size > 0))
     size--;
+ /* test for being in lib/gnunet/libexec/ */
+  if ( (size > strlen ("/gnunet/libexec/")) &&
+       (0 == strcmp ("/gnunet/libexec/",
+                    &lnk[size - strlen ("/gnunet/libexec/")])) )
+    size -= strlen ("gnunet/libexec/");
   if ((size < 4) || (lnk[size - 4] != '/'))
   {
     /* not installed in "/bin/" -- binary path probably useless */
@@ -100,28 +109,108 @@
 }
 #endif
 
+
 #if WINDOWS
 /**
  * Try to determine path with win32-specific function
+ *
+ * @return NULL on error
  */
 static char *
 get_path_from_module_filename ()
 {
-  char path[4097];
-  char *idx;
+  size_t pathlen = 512;
+  DWORD real_pathlen;
+  wchar_t *idx;
+  wchar_t *modulepath = NULL;
+  char *upath;
+  uint8_t *u8_string;
+  size_t u8_string_length;
 
-  GetModuleFileName (NULL, path, sizeof (path) - 1);
-  idx = path + strlen (path);
-  while ((idx > path) && (*idx != '\\') && (*idx != '/'))
+  /* This braindead function won't tell us how much space it needs, so
+   * we start at 1024 and double the space up if it doesn't fit, until
+   * it fits, or we exceed the threshold.
+   */
+  do
+  {
+    pathlen = pathlen * 2;
+    modulepath = GNUNET_realloc (modulepath, pathlen * sizeof (wchar_t));
+    SetLastError (0);
+    real_pathlen = GetModuleFileNameW (dll_instance, modulepath, pathlen * 
sizeof (wchar_t));
+  } while (real_pathlen >= pathlen && pathlen < 16*1024);
+  if (real_pathlen >= pathlen)
+    GNUNET_abort ();
+  /* To be safe */
+  modulepath[real_pathlen] = '\0';
+
+  idx = modulepath + real_pathlen;
+  while ((idx > modulepath) && (*idx != L'\\') && (*idx != L'/'))
     idx--;
-  *idx = '\0';
-  return GNUNET_strdup (path);
+  *idx = L'\0';
+
+  /* Now modulepath holds full path to the directory where libgnunetutil is.
+   * This directory should look like <GNUNET_PREFIX>/bin or <GNUNET_PREFIX>.
+   */
+  if (wcschr (modulepath, L'/') || wcschr (modulepath, L'\\'))
+  {
+    /* At least one directory component (i.e. we're not in a root directory) */
+    wchar_t *dirname = idx;
+    while ((dirname > modulepath) && (*dirname != L'\\') && (*dirname != L'/'))
+      dirname--;
+    *dirname = L'\0';
+    if (dirname > modulepath)
+    {
+      dirname++;
+      /* Now modulepath holds full path to the parent directory of the 
directory
+       * where libgnunetutil is.
+       * dirname holds the name of the directory where libgnunetutil is.
+       */
+      if (wcsicmp (dirname, L"bin") == 0)
+      {
+        /* pass */
+      }
+      else
+      {
+        /* Roll back our changes to modulepath */
+        dirname--;
+        *dirname = L'/';
+      }
+    }
+  }
+
+  /* modulepath is GNUNET_PREFIX */
+  u8_string = u16_to_u8 (modulepath, wcslen (modulepath), NULL, 
&u8_string_length);
+  if (NULL == u8_string)
+    GNUNET_abort ();
+
+  upath = GNUNET_malloc (u8_string_length + 1);
+  memcpy (upath, u8_string, u8_string_length);
+  upath[u8_string_length] = '\0';
+
+  free (u8_string);
+  GNUNET_free (modulepath);
+
+  return upath;
 }
 #endif
 
 #if DARWIN
+
+/**
+ * Signature of the '_NSGetExecutablePath" function.
+ *
+ * @param buf where to write the path
+ * @param number of bytes available in 'buf'
+ * @return 0 on success, otherwise desired number of bytes is stored in 
'bufsize'
+ */
 typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize);
 
+
+/**
+ * Try to obtain the path of our executable using '_NSGetExecutablePath'.
+ *
+ * @return NULL on error
+ */
 static char *
 get_path_from_NSGetExecutablePath ()
 {
@@ -132,9 +221,8 @@
   int ret;
 
   path = NULL;
-  func =
-      (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, 
"_NSGetExecutablePath");
-  if (!func)
+  if (NULL == (func =
+              (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, 
"_NSGetExecutablePath")))
     return NULL;
   path = &zero;
   len = 0;
@@ -156,37 +244,49 @@
   return path;
 }
 
+
+/**
+ * Try to obtain the path of our executable using '_dyld_image' API.
+ *
+ * @return NULL on error
+ */
 static char *
 get_path_from_dyld_image ()
 {
   const char *path;
-  char *p, *s;
-  int i;
+  char *p;
+  char *s;
+  unsigned int i;
   int c;
 
-  p = NULL;
   c = _dyld_image_count ();
   for (i = 0; i < c; i++)
   {
-    if (_dyld_get_image_header (i) == &_mh_dylib_header)
-    {
-      path = _dyld_get_image_name (i);
-      if (path != NULL && strlen (path) > 0)
-      {
-        p = strdup (path);
-        s = p + strlen (p);
-        while ((s > p) && (*s != '/'))
-          s--;
-        s++;
-        *s = '\0';
-      }
-      break;
-    }
+    if (_dyld_get_image_header (i) != &_mh_dylib_header)
+      continue;
+    path = _dyld_get_image_name (i);
+    if ( (NULL == path) || (0 == strlen (path)) )
+      continue;
+    p = GNUNET_strdup (path);
+    s = p + strlen (p);
+    while ((s > p) && ('/' != *s))
+      s--;
+    s++;
+    *s = '\0';
+    return p;
   }
-  return p;
+  return NULL;
 }
 #endif
 
+
+/**
+ * Return the actual path to a file found in the current
+ * PATH environment variable.
+ *
+ * @param binary the name of the file to find
+ * @return path to binary, NULL if not found
+ */
 static char *
 get_path_from_PATH ()
 {
@@ -229,6 +329,13 @@
   return NULL;
 }
 
+
+/**
+ * Try to obtain the installation path using the "GNUNET_PREFIX" environment
+ * variable.
+ *
+ * @return NULL on error (environment variable not set)
+ */
 static char *
 get_path_from_GNUNET_PREFIX ()
 {
@@ -240,6 +347,7 @@
   return NULL;
 }
 
+
 /*
  * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path
  * @author Milan
@@ -343,7 +451,7 @@
   if (execpath == NULL)
     execpath = os_get_gnunet_path ();
 
-  if (execpath == NULL)
+  if (NULL == execpath)
     return NULL;
 
   n = strlen (execpath);
@@ -362,7 +470,8 @@
       ((0 == strcasecmp (&execpath[n - 5], "lib32")) ||
        (0 == strcasecmp (&execpath[n - 5], "lib64"))))
   {
-    if (dirkind != GNUNET_OS_IPK_LIBDIR)
+    if ( (GNUNET_OS_IPK_LIBDIR != dirkind) &&
+        (GNUNET_OS_IPK_LIBEXECDIR != dirkind) )
     {
       /* strip '/lib32' or '/lib64' */
       execpath[n - 5] = '\0';
@@ -412,6 +521,16 @@
     dirname =
         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR;
     break;
+  case GNUNET_OS_IPK_LIBEXECDIR:
+    if (isbasedir)
+      dirname =
+        DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
+        "libexec" DIR_SEPARATOR_STR;
+    else
+      dirname =
+        DIR_SEPARATOR_STR "gnunet" DIR_SEPARATOR_STR \
+        "libexec" DIR_SEPARATOR_STR;
+    break;
   default:
     GNUNET_free (execpath);
     return NULL;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]