replace *a lot* of file related calls by their GLib counterparts. This is necessary...
[obnox/wireshark/wip.git] / filters.c
index aa58bcad4cd63725e19b71b9c5da9cf302b66994..cdecc516913692d56ac32a54b86285d90ef7f073 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,7 +1,7 @@
 /* filters.c
  * Code for reading and writing the filters file.
  *
- * $Id: filters.c,v 1.16 2003/12/04 00:45:37 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -40,6 +40,7 @@
 #include <epan/filesystem.h>
 
 #include "filters.h"
+#include "file_util.h"
 
 /*
  * Old filter file name.
@@ -81,7 +82,8 @@ void
 read_filter_list(filter_list_type_t list, char **pref_path_return,
     int *errno_return)
 {
-  char       *ff_path, *ff_name;
+  const char *ff_name;
+  char       *ff_path;
   FILE       *ff;
   GList      **flp;
   GList      *fl_ent;
@@ -111,11 +113,11 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
     return;
   }
 
-  /* To do: generalize this */
+  /* try to open personal "cfilters"/"dfilters" file */
   ff_path = get_persconffile_path(ff_name, FALSE);
-  if ((ff = fopen(ff_path, "r")) == NULL) {
+  if ((ff = eth_fopen(ff_path, "r")) == NULL) {
     /*
-     * Did that fail because we the file didn't exist?
+     * Did that fail because the file didn't exist?
      */
     if (errno != ENOENT) {
       /*
@@ -127,7 +129,7 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
     }
 
     /*
-     * Yes.  See if there's a "filters" file; if so, read it.
+     * Yes.  See if there's an "old style" personal "filters" file; if so, read it.
      * This means that a user will start out with their capture and
      * display filter lists being identical; each list may contain
      * filters that don't belong in that list.  The user can edit
@@ -136,7 +138,24 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
      */
     g_free(ff_path);
     ff_path = get_persconffile_path(FILTER_FILE_NAME, FALSE);
-    if ((ff = fopen(ff_path, "r")) == NULL) {
+    if ((ff = eth_fopen(ff_path, "r")) == NULL) {
+    /*
+     * Did that fail because the file didn't exist?
+     */
+      if (errno != ENOENT) {
+      /*
+       * No.  Just give up.
+       */
+       *pref_path_return = ff_path;
+       *errno_return = errno;
+    return;
+      }
+
+    /*
+     * Try to open the global "cfilters/dfilters" file */
+    ff_path = get_datafile_path(ff_name);
+    if ((ff = eth_fopen(ff_path, "r")) == NULL) {
+
       /*
        * Well, that didn't work, either.  Just give up.
        * Return an error if the file existed but we couldn't open it.
@@ -144,8 +163,9 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
       if (errno != ENOENT) {
        *pref_path_return = ff_path;
        *errno_return = errno;
-      }
-      return;
+    }
+    return;
+    }
     }
   }
 
@@ -407,7 +427,8 @@ void
 save_filter_list(filter_list_type_t list, char **pref_path_return,
     int *errno_return)
 {
-  gchar      *ff_path, *ff_path_new, *ff_name;
+  const gchar *ff_name;
+  gchar      *ff_path, *ff_path_new;
   GList      *fl;
   GList      *flp;
   filter_def *filt;
@@ -438,10 +459,9 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
   /* Write to "XXX.new", and rename if that succeeds.
      That means we don't trash the file if we fail to write it out
      completely. */
-  ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
-  sprintf(ff_path_new, "%s.new", ff_path);
+  ff_path_new = g_strdup_printf("%s.new", ff_path);
 
-  if ((ff = fopen(ff_path_new, "w")) == NULL) {
+  if ((ff = eth_fopen(ff_path_new, "w")) == NULL) {
     *pref_path_return = ff_path;
     *errno_return = errno;
     g_free(ff_path_new);
@@ -470,7 +490,7 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
       *pref_path_return = ff_path;
       *errno_return = errno;
       fclose(ff);
-      unlink(ff_path_new);
+      eth_unlink(ff_path_new);
       g_free(ff_path_new);
       return;
     }
@@ -479,32 +499,32 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
   if (fclose(ff) == EOF) {
     *pref_path_return = ff_path;
     *errno_return = errno;
-    unlink(ff_path_new);
+    eth_unlink(ff_path_new);
     g_free(ff_path_new);
     return;
   }
 
-#ifdef WIN32
+#ifdef _WIN32
   /* ANSI C doesn't say whether "rename()" removes the target if it
      exists; the Win32 call to rename files doesn't do so, which I
      infer is the reason why the MSVC++ "rename()" doesn't do so.
      We must therefore remove the target file first, on Windows. */
-  if (remove(ff_path) < 0 && errno != ENOENT) {
+  if (eth_remove(ff_path) < 0 && errno != ENOENT) {
     /* It failed for some reason other than "it's not there"; if
        it's not there, we don't need to remove it, so we just
        drive on. */
     *pref_path_return = ff_path;
     *errno_return = errno;
-    unlink(ff_path_new);
+    eth_unlink(ff_path_new);
     g_free(ff_path_new);
     return;
   }
 #endif
 
-  if (rename(ff_path_new, ff_path) < 0) {
+  if (eth_rename(ff_path_new, ff_path) < 0) {
     *pref_path_return = ff_path;
     *errno_return = errno;
-    unlink(ff_path_new);
+    eth_unlink(ff_path_new);
     g_free(ff_path_new);
     return;
   }