changeset 173:a9e4454dee97

Implemented searching $HOME/.mozilla for profiles.ini on Linux.
author Sascha Wilde <wilde@intevation.de>
date Tue, 25 Mar 2014 16:20:04 +0100
parents 7b9545ad76f6
children 6eb9149b65e1
files cinst/mozilla.c common/errorcodes.h
diffstat 2 files changed, 94 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/cinst/mozilla.c	Tue Mar 25 15:20:06 2014 +0100
+++ b/cinst/mozilla.c	Tue Mar 25 16:20:04 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
@@ -146,20 +149,92 @@
   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;
+  DIR *mozdir;
+  struct dirent *mozdirent;
+
+  if (LINUX)
+    {
+      /* Search in $HOME/.mozilla */
+      char *home;
+
+      if ((home = getenv("HOME")) == NULL)
+        {
+          DEBUGFPRINT("DEBUG: FATAL!  No HOME in environment.\n")
+            exit(ERR_MOZ_HOMELESS);
+        }
+
+      snprintf(path, LINEBUFLEN, "%s/%s", home, "/.mozilla");
+      if ((mozdir = opendir(path)) != NULL)
+        {
+          while ((mozdirent = readdir(mozdir)) != NULL)
+            {
+              if (mozdirent->d_type == DT_DIR)
+                {
+                  snprintf(path, LINEBUFLEN, "%s/%s/%s/%s",
+                           home,
+                           "/.mozilla",
+                           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/.mozilla\n", home)
+            exit(WARN_MOZ_NO_PROFILES);
+        }
+    }
+  else
+    {
+      /* Windows */
+      fprintf(stderr, "Windows not yet supported");
+      abort();
+    }
+  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);
 }
--- a/common/errorcodes.h	Tue Mar 25 15:20:06 2014 +0100
+++ b/common/errorcodes.h	Tue Mar 25 16:20:04 2014 +0100
@@ -27,9 +27,14 @@
  * Warnings might be ORed together ...
  */
 
+/* Error: could not determine current users HOME */
+#define ERR_MOZ_HOMELESS 0x0081
+
 /* Warning: Failed to read profile.ini */
 #define WARN_MOZ_FAILED_TO_OPEN_INI 0x0091
 /* Warning: Some profile paths from profile.ini don't exist */
 #define WARN_MOZ_PROFILE_DOES_NOT_EXIST 0x0092
+/* Warning: no profiles found */
+#define WARN_MOZ_NO_PROFILES 0x0094
 
 #endif

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