Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / os_installation.c
index afb8c2474462f96d2ab14301e5c9bcf20dd5d409..cbbc614334bf38762fffdf4c0386d83d7673d746 100644 (file)
  * @brief get paths used by the program
  * @author Milan
  */
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
@@ -55,32 +46,31 @@ static char *
 get_path_from_proc_maps ()
 {
   char fn[64];
-  char *line;
-  char *dir;
+  char line[1024];
+  char dir[1024];
   FILE *f;
+  char *lgu;
 
-  GNUNET_snprintf (fn, 64, "/proc/%u/maps", getpid ());
-  line = GNUNET_malloc (1024);
-  dir = GNUNET_malloc (1024);
+  GNUNET_snprintf (fn,
+                  sizeof(fn), 
+                  "/proc/%u/maps", 
+                  getpid ());
   f = fopen (fn, "r");
-  if (f != NULL)
+  if (f == NULL)
+    return NULL;
+  while (NULL != fgets (line, sizeof(line), f))
     {
-      while (NULL != fgets (line, 1024, f))
-        {
-          if ((1 == sscanf (line,
-                            "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
-                            dir)) && (NULL != strstr (dir, "libgnunetutil")))
-            {
-              strstr (dir, "libgnunetutil")[0] = '\0';
-              fclose (f);
-              GNUNET_free (line);
-              return dir;
-            }
-        }
-      fclose (f);
+      if ((1 == sscanf (line,
+                       "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s",
+                       dir)) &&
+         (NULL != (lgu = strstr (dir, "libgnunetutil"))))
+       {
+         lgu[0] = '\0';
+         fclose (f);
+         return GNUNET_strdup (dir);
+       }
     }
-  GNUNET_free (dir);
-  GNUNET_free (line);
+  fclose (f);
   return NULL;
 }
 
@@ -91,29 +81,28 @@ static char *
 get_path_from_proc_exe ()
 {
   char fn[64];
-  char *lnk;
-  size_t size;
+  char lnk[1024];
+  ssize_t size;
 
-  GNUNET_snprintf (fn, 64, "/proc/%u/exe", getpid ());
-  lnk = GNUNET_malloc (1024);
-  size = readlink (fn, lnk, 1023);
-  if ((size == 0) || (size >= 1024))
+  GNUNET_snprintf (fn, 
+                  sizeof(fn), "/proc/%u/exe", getpid ());
+  size = readlink (fn, lnk, sizeof (lnk)-1);
+  if (size <= 0)
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn);
-      GNUNET_free (lnk);
       return NULL;
     }
+  GNUNET_assert (size < sizeof (lnk));
   lnk[size] = '\0';
   while ((lnk[size] != '/') && (size > 0))
     size--;
   if ((size < 4) || (lnk[size - 4] != '/'))
     {
       /* not installed in "/bin/" -- binary path probably useless */
-      GNUNET_free (lnk);
       return NULL;
     }
   lnk[size] = '\0';
-  return lnk;
+  return GNUNET_strdup (lnk);
 }
 #endif
 
@@ -124,16 +113,15 @@ get_path_from_proc_exe ()
 static char *
 get_path_from_module_filename ()
 {
-  char *path;
+  char path[4097];
   char *idx;
 
-  path = GNUNET_malloc (4097);
-  GetModuleFileName (NULL, path, 4096);
+  GetModuleFileName (NULL, path, sizeof(path)-1);
   idx = path + strlen (path);
   while ((idx > path) && (*idx != '\\') && (*idx != '/'))
     idx--;
   *idx = '\0';
-  return path;
+  return GNUNET_strdup (path);
 }
 #endif
 
@@ -221,10 +209,10 @@ get_path_from_PATH ()
   buf = GNUNET_malloc (strlen (path) + 20);
   pos = path;
 
-  while (NULL != (end = strchr (pos, ':')))
+  while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
     {
       *end = '\0';
-      sprintf (buf, "%s/%s", pos, "gnunetd");
+      sprintf (buf, "%s/%s", pos, "gnunet-arm");
       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
         {
           pos = GNUNET_strdup (pos);
@@ -234,7 +222,7 @@ get_path_from_PATH ()
         }
       pos = end + 1;
     }
-  sprintf (buf, "%s/%s", pos, "gnunetd");
+  sprintf (buf, "%s/%s", pos, "gnunet-arm");
   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
     {
       pos = GNUNET_strdup (pos);
@@ -298,8 +286,10 @@ os_get_gnunet_path ()
     return ret;
   /* other attempts here */
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-             _("Could not determine installation path for GNUnet.  Set `%s' environment variable.\n"),
-             "GNUNET_PREFIX");
+              _
+              ("Could not determine installation path for %s.  Set `%s' environment variable.\n"),
+             "GNUnet",
+              "GNUNET_PREFIX");
   return NULL;
 }
 
@@ -426,6 +416,10 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
         DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale"
         DIR_SEPARATOR_STR;
       break;
+    case GNUNET_OS_IPK_ICONDIR:
+      dirname =
+        DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR;
+      break;
     default:
       GNUNET_free (execpath);
       return NULL;
@@ -436,10 +430,4 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
   return tmp;
 }
 
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
 /* end of os_installation.c */