Bug fix: Non-portable functions
[geeqie.git] / src / main.c
index 6b19d98..2cef1c9 100644 (file)
@@ -58,6 +58,7 @@
 #include "histogram.h"
 #include "pixbuf_util.h"
 #include "glua.h"
+#include "whereami.h"
 
 #ifdef HAVE_CLUTTER
 #include <clutter-gtk/clutter-gtk.h>
@@ -269,7 +270,9 @@ gchar *gq_helpdir;
 gchar *gq_htmldir;
 gchar *gq_app_dir;
 gchar *gq_bin_dir;
+gchar *gq_executable_path;
 gchar *desktop_file_template;
+gchar *instance_identifier;
 
 /*
  *-----------------------------------------------------------------------------
@@ -1075,6 +1078,8 @@ static void exit_program_final(void)
        LayoutWindow *lw = NULL;
        GList *list;
        LayoutWindow *tmp_lw;
+       gchar *archive_dir;
+       GFile *archive_file;
 
         /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
        layout_editors_reload_finish();
@@ -1107,6 +1112,27 @@ static void exit_program_final(void)
                layout_free(lw);
                }
 
+       /* Delete any files/folders in /tmp that have been created by the open archive function */
+       archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
+       if (isdir(archive_dir))
+               {
+               archive_file = g_file_new_for_path(archive_dir);
+               rmdir_recursive(archive_file, NULL, NULL);
+               g_free(archive_dir);
+               g_object_unref(archive_file);
+               }
+
+       /* If there are still sub-dirs created by another instance, this will fail
+        * but that does not matter */
+       archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
+       if (isdir(archive_dir))
+               {
+               archive_file = g_file_new_for_path(archive_dir);
+               g_file_delete(archive_file, NULL, NULL);
+               g_free(archive_dir);
+               g_object_unref(archive_file);
+               }
+
        secure_close(command_line->ssi);
 
        gtk_main_quit();
@@ -1222,22 +1248,20 @@ static void setup_sigbus_handler(void)
  * They are now variables, all defined relative to one level above the
  * directory that the executable is run from.
  */
-static void create_application_paths()
+static void create_application_paths(gchar *argv[])
 {
-       gchar buf[1024];
        gchar *dirname;
        gchar *tmp;
+       gint length;
+       gchar *path;
 
-       memset(buf, 0, sizeof(buf));
-       if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) < 0)
-               {
-               /* There was an error. Perhaps the path does not exist
-                * or the buffer is not big enough. */
-               log_printf("Can't get path from /proc/self/exe");
-               exit(1);
-               }
+       length = wai_getExecutablePath(NULL, 0, NULL);
+       path = (gchar *)malloc(length + 1);
+       wai_getExecutablePath(path, length, NULL);
+       path[length] = '\0';
 
-       dirname = g_path_get_dirname(buf); // default is /usr/bin/
+       gq_executable_path = g_strdup(path);
+       dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/
        gq_prefix = g_path_get_dirname(dirname);
 
        gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
@@ -1250,6 +1274,7 @@ static void create_application_paths()
 
        g_free(tmp);
        g_free(dirname);
+       g_free(path);
 }
 
 gint main(gint argc, gchar *argv[])
@@ -1273,7 +1298,7 @@ gint main(gint argc, gchar *argv[])
        /* init execution time counter (debug only) */
        init_exec_time();
 
-       create_application_paths();
+       create_application_paths(argv);
 
        /* setup locale, i18n */
        setlocale(LC_ALL, "");
@@ -1347,6 +1372,9 @@ gint main(gint argc, gchar *argv[])
                options->disable_gpu = TRUE;
                }
 
+       /* Generate a unique identifier used by the open archive function */
+       instance_identifier = g_strdup_printf("%x", g_random_int());
+
        DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
        /* these functions don't depend on config file */
        mkdir_if_not_exists(get_rc_dir());