From 5fc8d2d134c9c1734c5523bbeccf499ef1309ede Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Mon, 6 May 2002 20:15:49 +0000 Subject: [PATCH] gnc-backend-file.c: scandir() is not portable; re-write the backup-file deletion using readdir() instead gnc-gui-query.c: at startup, gnc_ui_get_toplevel() may return NULL, do deal accordingly and don't assume it will be non-NULL (still other cases where this can bite you) git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@6825 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/backend/file/gnc-backend-file.c | 36 +++++++++++++++++++++-------- src/gnome-utils/gnc-gui-query.c | 5 +++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/backend/file/gnc-backend-file.c b/src/backend/file/gnc-backend-file.c index 2c75e13dd6..70944799fa 100644 --- a/src/backend/file/gnc-backend-file.c +++ b/src/backend/file/gnc-backend-file.c @@ -5,7 +5,6 @@ *********************************************************************/ #define _GNU_SOURCE -#define __EXTENSIONS__ #include #include @@ -547,9 +546,10 @@ gnc_file_be_select_files (const struct dirent *d) static void gnc_file_be_remove_old_files(FileBackend *be) { - struct dirent **dent; + struct dirent *dent; + DIR *dir; struct stat lockstatbuf, statbuf; - int pathlen, n; + int pathlen; time_t now; if (stat (be->lockfile, &lockstatbuf) != 0) @@ -563,14 +563,31 @@ gnc_file_be_remove_old_files(FileBackend *be) * appropriate extensions. Pity you can't pass user data into * scandir... */ - n = scandir(be->dirname, &dent, gnc_file_be_select_files, alphasort); - if (n <= 0) + + /* + * Unfortunately scandir() is not portable, so re-write this + * function without it. Note that this version will be even a bit + * faster because it does not have to sort, malloc, or anything + * else that scandir did, and it only performs a single pass + * through the directory rather than one pass through the + * directory and then one pass over the 'matching' files. -- + * warlord@MIT.EDU 2002-05-06 + */ + + dir = opendir (be->dirname); + if (!dir) return; now = time(NULL); - while(n--) { - char *name = g_strconcat(be->dirname, "/", dent[n]->d_name, NULL); - int len = strlen(name) - 4; + while((dent = readdir(dir)) != NULL) { + char *name; + int len; + + if (gnc_file_be_select_files (dent) == 0) + continue; + + name = g_strconcat(be->dirname, "/", dent->d_name, NULL); + len = strlen(name) - 4; /* Is this file associated with the current data file */ if (strncmp(name, be->fullpath, pathlen) == 0) { @@ -598,9 +615,8 @@ gnc_file_be_remove_old_files(FileBackend *be) } } g_free(name); - free(dent[n]); } - free(dent); + closedir (dir); } diff --git a/src/gnome-utils/gnc-gui-query.c b/src/gnome-utils/gnc-gui-query.c index 2cc19929eb..d645d32c5b 100644 --- a/src/gnome-utils/gnc-gui-query.c +++ b/src/gnome-utils/gnc-gui-query.c @@ -416,10 +416,13 @@ gnc_error_dialog_common(GtkWindow *parent, const gchar *format, va_list args) void gnc_error_dialog(const gchar *format, ...) { + GtkWidget *parent; va_list args; + parent = gnc_ui_get_toplevel(); + va_start(args, format); - gnc_error_dialog_common(GTK_WINDOW(gnc_ui_get_toplevel()), format, args); + gnc_error_dialog_common(parent ? GTK_WINDOW(parent) : NULL, format, args); va_end(args); }