diff cinst/mozilla.c @ 188:a3bde2aaabd9

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 26 Mar 2014 09:12:10 +0100
parents bea93c8651b7
children d4e97c9b199f
line wrap: on
line diff
--- a/cinst/mozilla.c	Wed Mar 26 09:10:46 2014 +0100
+++ b/cinst/mozilla.c	Wed Mar 26 09:12:10 2014 +0100
@@ -48,19 +48,22 @@
  * encoded.
  * */
 
+#include <dirent.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
 
+#include <debug.h>
 #include <errorcodes.h>
 #include <portpath.h>
 #include <strhelp.h>
 
 #ifndef _WIN32
-#define UNIX 1
+#define LINUX 1
 #else
-#define UNIX 0
+#define LINUX 0
 #endif
 
 #define LINEBUFLEN 1000
@@ -76,6 +79,47 @@
 int return_code = 0;
 
 /**
+ * @brief Return users home path, on windows including drive letter
+ * @returns a pointer to a string containing the home path, it should
+ * be freed by the caller.
+ */
+static char *
+get_home()
+{
+  char *home, *homevar, *fqhome;
+  char *homedrive = NULL;
+
+  size_t len;
+  if (LINUX)
+    homevar = "HOME";
+  else
+    homevar = "HOMEPATH";
+
+  if ((home = getenv(homevar)) == NULL)
+    {
+      DEBUGFPRINT("DEBUG: FATAL!  No HOME in environment.\n");
+      exit(ERR_MOZ_HOMELESS);
+    }
+
+  len = strlen(home);
+  if (!LINUX)
+    homedrive = getenv("HOMEDRIVE");
+
+  if (homedrive != NULL)
+    len += strlen(homedrive);
+
+  len++;                        /* Room for \0 */
+  fqhome = xmalloc(len);
+
+  if (homedrive == NULL)
+    snprintf(fqhome, len, "%s", home);
+  else
+    snprintf(fqhome, len, "%s%s", homedrive, home);
+
+  return fqhome;
+}
+
+/**
  * @brief Get a list of all mozilla profile directories
  *
  * Read the profiles.ini and extract all profile paths from that.
@@ -101,6 +145,8 @@
 
   if ((inifile = fopen(inifile_name, "r")) != NULL)
     {
+      DEBUGFPRINT("DEBUG: Searching for profile paths in: '%s'\n", inifile_name);
+
       inifile_dirname = port_dirname(inifile_name);
       while (fgets(line, LINEBUFLEN, inifile) != NULL)
         {
@@ -116,8 +162,8 @@
           /* If we are in a profile parse path related stuff */
           if (inprofile)
             {
-              value = line;
-              key = strsep(&value, "=");
+              key = strtok(line, "=");
+              value = strtok(NULL, "=");
               str_trim(&value);
               if (str_equal(key, "Path"))
                 {
@@ -127,39 +173,114 @@
                     strncpy(path, value, LINEBUFLEN);
                   if ((fqpath = port_realpath(path)) != NULL)
                     {
+                      DEBUGFPRINT("DEBUG: Found profile path: '%s'\n", fqpath);
                       strv_append(&dirs, fqpath, strlen(fqpath));
                       free (fqpath);
                     }
                   else
-                    return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST;
+                    {
+                      DEBUGFPRINT("DEBUG: WARN!  Non existent profile path: '%s'\n", path);
+                      return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST;
+                    }
                 }
               else if (str_equal(key, "IsRelative") &&
                        str_starts_with(value, "1"))
                 relative_path = true;
             }
         }
+      fclose(inifile);
     }
   else
     {
+      DEBUGFPRINT("DEBUG: WARN!  Could not open ini file: '%s'\n", inifile_name);
       return_code |= WARN_MOZ_FAILED_TO_OPEN_INI;
     }
   return dirs;
 }
 
+/**
+ * @brief Search for mozilla profiles.ini files
+ *
+ * Use well known paths and heuristics to find the current users
+ * profiles.ini files on GNU/Linux and Windows systems.
+ *
+ * @return NULL terminated array of strings containing the absolute
+ * path of the profiles.ini files.  The array needs to be freed by the
+ * caller.
+ */
+static char **
+get_profile_inis ()
+{
+  char **inis = NULL;
+  char path[LINEBUFLEN];
+  char *fqpath;
+  char *mozdirname;
+  DIR *mozdir;
+  struct dirent *mozdirent;
+  char *home = get_home();
+
+  if (LINUX)
+    {
+      mozdirname = ".mozilla";
+    }
+  else
+    {
+      mozdirname = "AppData/Roaming/Mozilla";
+    }
+
+  snprintf(path, LINEBUFLEN, "%s/%s", home, mozdirname);
+  if ((mozdir = opendir(path)) != NULL)
+    {
+      while ((mozdirent = readdir(mozdir)) != NULL)
+        {
+          snprintf(path, LINEBUFLEN, "%s/%s/%s",
+                   home,
+                   mozdirname,
+                   mozdirent->d_name);
+          if (port_isdir(path))
+            {
+              snprintf(path, LINEBUFLEN, "%s/%s/%s/%s",
+                       home,
+                       mozdirname,
+                       mozdirent->d_name,
+                       "profiles.ini");
+              DEBUGFPRINT("DEBUG: checking for %s...\n", path);
+              if ((fqpath = port_realpath(path)) != NULL)
+                {
+                  strv_append(&inis, fqpath, strlen(fqpath));
+                  DEBUGFPRINT("DEBUG: Found mozilla ini file: '%s'\n", fqpath);
+                  free(fqpath);
+                }
+            }
+        }
+      closedir(mozdir);
+    }
+  else
+    {
+      DEBUGFPRINT("DEBUG: Could not open %s/%s\n", home, mozdirname);
+      exit(WARN_MOZ_NO_PROFILES);
+    }
+  return inis;
+}
+
+
 int
-main (int argc, char *argv[])
+main ()
 {
   int x = 0;
-  if (argc == 2)
-    {
-      char **pdirs =
-        get_profile_dirs(argv[1]);
-      if (pdirs != NULL)
-        {
-          while (pdirs[x] != NULL)
-            puts(pdirs[x++]);
-          strv_free(pdirs);
-        }
-    }
+  int y = 0;
+  char **mozinis, **pdirs;
+  if ((mozinis = get_profile_inis()) != NULL)
+    while (mozinis[y] != NULL)
+      {
+        pdirs =
+          get_profile_dirs(mozinis[y++]);
+        if (pdirs != NULL)
+          {
+            while (pdirs[x] != NULL)
+              puts(pdirs[x++]);
+            strv_free(pdirs);
+          }
+      }
   exit(return_code);
 }

http://wald.intevation.org/projects/trustbridge/