freebsd needs to define AF_INET6, seems to need sys/socket.h
[obnox/wireshark/wip.git] / filters.c
index 5aaaeb04073de103e89cbfeb2cdb5d7d231e6f9e..cdecc516913692d56ac32a54b86285d90ef7f073 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,22 +1,22 @@
 /* filters.c
  * Code for reading and writing the filters file.
  *
- * $Id: filters.c,v 1.14 2002/01/21 07:36:31 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
@@ -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;
+    }
     }
   }
 
@@ -247,7 +267,7 @@ read_filter_list(filter_list_type_t list, char **pref_path_return,
                line);
       continue;
     }
-         
+
     /* Skip over separating white space, if any. */
     while ((c = getc(ff)) != EOF && isspace(c)) {
       if (c == '\n')
@@ -354,7 +374,7 @@ GList *
 get_filter_list_first(filter_list_type_t list)
 {
   GList      **flp;
-  
+
   flp = get_filter_list(list);
   return g_list_first(*flp);
 }
@@ -364,11 +384,12 @@ get_filter_list_first(filter_list_type_t list)
  * Returns a pointer to the newly-added entry.
  */
 GList *
-add_to_filter_list(filter_list_type_t list, char *name, char *expression)
+add_to_filter_list(filter_list_type_t list, const char *name,
+    const char *expression)
 {
   GList      **flp;
   filter_def *filt;
-  
+
   flp = get_filter_list(list);
   filt = (filter_def *) g_malloc(sizeof(filter_def));
   filt->name = g_strdup(name);
@@ -385,7 +406,7 @@ remove_from_filter_list(filter_list_type_t list, GList *fl_entry)
 {
   GList      **flp;
   filter_def *filt;
-  
+
   flp = get_filter_list(list);
   filt = (filter_def *) fl_entry->data;
   g_free(filt->name);
@@ -406,13 +427,14 @@ 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;
   FILE       *ff;
   guchar     *p, c;
-  
+
   *pref_path_return = NULL;    /* assume no error */
 
   switch (list) {
@@ -437,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);
@@ -469,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;
     }
@@ -478,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;
   }