From Ivan Lawrow: Added IEEE 802.15.4-2003 AES-CCM security modes
[obnox/wireshark/wip.git] / disabled_protos.c
index 11b45de9f6855ed9252aeeb63e6ac95135c889b2..fc6bfc2bc25c270fd9ed345fd8441ebbf25b84f8 100644 (file)
@@ -1,10 +1,10 @@
 /* disabled_protos.c
  * Code for reading and writing the disabled protocols file.
  *
- * $Id: disabled_protos.c,v 1.4 2004/01/03 18:40:07 sharpe Exp $
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
@@ -41,6 +41,7 @@
 #include <epan/proto.h>
 
 #include "disabled_protos.h"
+#include <wsutil/file_util.h>
 
 #define GLOBAL_PROTOCOLS_FILE_NAME     "disabled_protos"
 #define PROTOCOLS_FILE_NAME            "disabled_protos"
@@ -53,6 +54,25 @@ static GList *disabled_protos = NULL;
 
 #define INIT_BUF_SIZE  128
 
+static void
+discard_existing_list (GList **flp)
+{
+  GList      *fl_ent;
+  protocol_def *prot;
+
+  if (*flp != NULL) {
+    fl_ent = g_list_first(*flp);
+    while (fl_ent != NULL) {
+      prot = (protocol_def *) fl_ent->data;
+      g_free(prot->name);
+      g_free(prot);
+      fl_ent = fl_ent->next;
+    }
+    g_list_free(*flp);
+    *flp = NULL;
+  }
+}
+
 /*
  * Read in a list of disabled protocols.
  *
@@ -79,9 +99,12 @@ read_disabled_protos_list(char **gpath_return, int *gopen_errno_return,
   /* Construct the pathname of the global disabled protocols file. */
   gff_path = get_datafile_path(GLOBAL_PROTOCOLS_FILE_NAME);
 
+  /* If we already have a list of protocols, discard it. */
+  discard_existing_list (&global_disabled_protos);
+
   /* Read the global disabled protocols file, if it exists. */
   *gpath_return = NULL;
-  if ((ff = fopen(gff_path, "r")) != NULL) {
+  if ((ff = ws_fopen(gff_path, "r")) != NULL) {
     /* We succeeded in opening it; read it. */
     err = read_disabled_protos_list_file(gff_path, ff,
                                         &global_disabled_protos);
@@ -102,15 +125,19 @@ read_disabled_protos_list(char **gpath_return, int *gopen_errno_return,
       *gopen_errno_return = errno;
       *gread_errno_return = 0;
       *gpath_return = gff_path;
-    }
+    } else
+      g_free(gff_path);
   }
 
   /* Construct the pathname of the user's disabled protocols file. */
-  ff_path = get_persconffile_path(PROTOCOLS_FILE_NAME, FALSE);
+  ff_path = get_persconffile_path(PROTOCOLS_FILE_NAME, TRUE, FALSE);
 
-  /* Read the global disabled protocols file, if it exists. */
+  /* If we already have a list of protocols, discard it. */
+  discard_existing_list (&disabled_protos);
+
+  /* Read the user's disabled protocols file, if it exists. */
   *path_return = NULL;
-  if ((ff = fopen(ff_path, "r")) != NULL) {
+  if ((ff = ws_fopen(ff_path, "r")) != NULL) {
     /* We succeeded in opening it; read it. */
     err = read_disabled_protos_list_file(ff_path, ff, &disabled_protos);
     if (err != 0) {
@@ -130,7 +157,8 @@ read_disabled_protos_list(char **gpath_return, int *gopen_errno_return,
       *open_errno_return = errno;
       *read_errno_return = 0;
       *path_return = ff_path;
-    }
+    } else
+      g_free(ff_path);
   }
 }
 
@@ -138,7 +166,6 @@ static int
 read_disabled_protos_list_file(const char *ff_path, FILE *ff,
                               GList **flp)
 {
-  GList      *fl_ent;
   protocol_def *prot;
   int         c;
   char       *prot_name;
@@ -146,22 +173,10 @@ read_disabled_protos_list_file(const char *ff_path, FILE *ff,
   int         prot_name_index;
   int         line = 1;
 
-  /* If we already have a list of protocols, discard it. */
-  if (*flp != NULL) {
-    fl_ent = g_list_first(*flp);
-    while (fl_ent != NULL) {
-      prot = (protocol_def *) fl_ent->data;
-      g_free(prot->name);
-      g_free(prot);
-      fl_ent = fl_ent->next;
-    }
-    g_list_free(*flp);
-    *flp = NULL;
-  }
 
   /* Allocate the protocol name buffer. */
   prot_name_len = INIT_BUF_SIZE;
-  prot_name = g_malloc(prot_name_len + 1);
+  prot_name = (char *)g_malloc(prot_name_len + 1);
 
   for (line = 1; ; line++) {
     /* Lines in a disabled protocol file contain the "filter name" of
@@ -197,7 +212,7 @@ read_disabled_protos_list_file(const char *ff_path, FILE *ff,
       if (prot_name_index >= prot_name_len) {
        /* protocol name buffer isn't long enough; double its length. */
        prot_name_len *= 2;
-       prot_name = g_realloc(prot_name, prot_name_len + 1);
+       prot_name = (char *)g_realloc(prot_name, prot_name_len + 1);
       }
       prot_name[prot_name_index] = c;
       prot_name_index++;
@@ -235,7 +250,7 @@ read_disabled_protos_list_file(const char *ff_path, FILE *ff,
     if (prot_name_index >= prot_name_len) {
       /* protocol name buffer isn't long enough; double its length. */
       prot_name_len *= 2;
-      prot_name = g_realloc(prot_name, prot_name_len + 1);
+      prot_name = (char *)g_realloc(prot_name, prot_name_len + 1);
     }
     prot_name[prot_name_index] = '\0';
 
@@ -315,25 +330,22 @@ skip:
 void
 save_disabled_protos_list(char **pref_path_return, int *errno_return)
 {
-  gchar      *ff_path, *ff_path_new, *ff_name;
-  FILE       *ff;
-  gint        i;
-  protocol_t *protocol;
-  void       *cookie;
+  gchar       *ff_path, *ff_path_new;
+  FILE        *ff;
+  gint         i;
+  protocol_t  *protocol;
+  void        *cookie;
 
   *pref_path_return = NULL;    /* assume no error */
 
-  ff_name = PROTOCOLS_FILE_NAME;
-
-  ff_path = get_persconffile_path(ff_name, TRUE);
+  ff_path = get_persconffile_path(PROTOCOLS_FILE_NAME, TRUE, TRUE);
 
   /* 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 = ws_fopen(ff_path_new, "w")) == NULL) {
     *pref_path_return = ff_path;
     *errno_return = errno;
     g_free(ff_path_new);
@@ -361,32 +373,32 @@ save_disabled_protos_list(char **pref_path_return, int *errno_return)
   if (fclose(ff) == EOF) {
     *pref_path_return = ff_path;
     *errno_return = errno;
-    unlink(ff_path_new);
+    ws_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 (ws_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);
+    ws_unlink(ff_path_new);
     g_free(ff_path_new);
     return;
   }
 #endif
 
-  if (rename(ff_path_new, ff_path) < 0) {
+  if (ws_rename(ff_path_new, ff_path) < 0) {
     *pref_path_return = ff_path;
     *errno_return = errno;
-    unlink(ff_path_new);
+    ws_unlink(ff_path_new);
     g_free(ff_path_new);
     return;
   }