Make Content-Length and Max-Forwards fields uints
[obnox/wireshark/wip.git] / file.c
diff --git a/file.c b/file.c
index 067e9ca5090118174fccd3a6dcbdd3995239034e..1ac3d070cb3b82e91a546526a241412b09978165 100644 (file)
--- a/file.c
+++ b/file.c
@@ -3,8 +3,8 @@
  *
  * $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
 
 #include <time.h>
 
-#ifdef HAVE_IO_H
-#include <io.h>
-#endif
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
-#ifdef NEED_SNPRINTF_H
-# include "snprintf.h"
-#endif
-
 #ifdef NEED_STRERROR_H
 #include "strerror.h"
 #endif
@@ -69,7 +57,8 @@
 #include "packet-range.h"
 #include "print.h"
 #include "file.h"
-#include "util.h"
+#include "fileset.h"
+#include "tempfile.h"
 #include "merge.h"
 #include "alert_box.h"
 #include "simple_dialog.h"
 #include <epan/conversation.h>
 #include <epan/epan_dissect.h>
 #include <epan/tap.h>
+#include "stat_menu.h"
 #include "tap_dfilter_dlg.h"
 #include <epan/dissectors/packet-data.h>
+#include <epan/timestamp.h>
+#include "file_util.h"
+
 
-/* Win32 needs the O_BINARY flag for open() */
-#ifndef O_BINARY
-#define O_BINARY       0
-#endif
 
 #ifdef HAVE_LIBPCAP
 gboolean auto_scroll_live;
 #endif
 
-static guint32 firstsec, firstusec;
-static guint32 prevsec, prevusec;
+static nstime_t first_ts;
+static nstime_t prev_ts;
 static guint32 cum_bytes = 0;
 
 static void cf_reset_state(capture_file *cf);
@@ -125,7 +114,7 @@ static gboolean find_packet(capture_file *cf,
 static void cf_open_failure_alert_box(const char *filename, int err,
                                      gchar *err_info, gboolean for_writing,
                                      int file_type);
-static char *file_rename_error_message(int err);
+static const char *file_rename_error_message(int err);
 static void cf_write_failure_alert_box(const char *filename, int err);
 static void cf_close_failure_alert_box(const char *filename, int err);
 static   gboolean copy_binary_file(const char *from_filename, const char *to_filename);
@@ -168,27 +157,62 @@ cf_callback_remove(cf_callback_t func _U_)
     cf_cb_user_data = NULL;
 }
 
+void
+cf_timestamp_auto_precision(capture_file *cf)
+{
+       int prec = timestamp_get_precision();
+
+
+       /* don't try to get the file's precision if none is opened */
+       if(cf->state == FILE_CLOSED) {
+               return;
+       }
+
+       /* if we are in auto mode, set precision of current file */
+       if(prec == TS_PREC_AUTO ||
+         prec == TS_PREC_AUTO_SEC ||
+         prec == TS_PREC_AUTO_DSEC ||
+         prec == TS_PREC_AUTO_CSEC ||
+         prec == TS_PREC_AUTO_MSEC ||
+         prec == TS_PREC_AUTO_USEC ||
+         prec == TS_PREC_AUTO_NSEC)
+       {
+               switch(wtap_file_tsprecision(cf->wth)) {
+               case(WTAP_FILE_TSPREC_SEC):
+                       timestamp_set_precision(TS_PREC_AUTO_SEC);
+                       break;
+               case(WTAP_FILE_TSPREC_DSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_DSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_CSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_CSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_MSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_MSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_USEC):
+                       timestamp_set_precision(TS_PREC_AUTO_USEC);
+                       break;
+               case(WTAP_FILE_TSPREC_NSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_NSEC);
+                       break;
+               default:
+                       g_assert_not_reached();
+               }
+       }
+}
+
 
 cf_status_t
 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 {
   wtap       *wth;
   gchar       *err_info;
-  int         fd;
-  struct stat cf_stat;
 
   wth = wtap_open_offline(fname, err, &err_info, TRUE);
   if (wth == NULL)
     goto fail;
 
-  /* Find the size of the file. */
-  fd = wtap_fd(wth);
-  if (fstat(fd, &cf_stat) < 0) {
-    *err = errno;
-    wtap_close(wth);
-    goto fail;
-  }
-
   /* The open succeeded.  Close whatever capture file we had open,
      and fill in the information for this file. */
   cf_reset_state(cf);
@@ -200,8 +224,7 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   cf->state = FILE_READ_IN_PROGRESS;
 
   cf->wth = wth;
-  cf->filed = fd;
-  cf->f_len = cf_stat.st_size;
+  cf->f_datalen = 0;
 
   /* Set the file name because we need it to set the follow stream filter.
      XXX - is that still true?  We need it for other reasons, though,
@@ -214,14 +237,12 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   /* If it's a temporary capture buffer file, mark it as not saved. */
   cf->user_saved = !is_tempfile;
 
-  cf->cd_t      = wtap_file_type(cf->wth);
+  cf->cd_t        = wtap_file_type(cf->wth);
   cf->count     = 0;
   cf->displayed_count = 0;
   cf->marked_count = 0;
   cf->drops_known = FALSE;
   cf->drops     = 0;
-  cf->esec      = 0;
-  cf->eusec     = 0;
   cf->snap      = wtap_snapshot_length(cf->wth);
   if (cf->snap == 0) {
     /* Snapshot length not known. */
@@ -229,8 +250,9 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
     cf->snap = WTAP_MAX_PACKET_SIZE;
   } else
     cf->has_snap = TRUE;
-  firstsec = 0, firstusec = 0;
-  prevsec = 0, prevusec = 0;
+  nstime_set_zero(&cf->elapsed_time);
+  nstime_set_zero(&first_ts);
+  nstime_set_zero(&prev_ts);
 
   cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
        sizeof(frame_data),
@@ -238,6 +260,11 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
        G_ALLOC_AND_FREE);
   g_assert(cf->plist_chunk);
 
+  /* change the time formats now, as we might have a new precision */
+  cf_change_time_formats(cf);
+
+  fileset_file_opened(fname);
+
   return CF_OK;
 
 fail:
@@ -245,6 +272,7 @@ fail:
   return CF_ERROR;
 }
 
+
 /*
  * Reset the state for the currently closed file, but don't do the
  * UI callbacks; this is for use in "cf_open()", where we don't
@@ -266,7 +294,7 @@ cf_reset_state(capture_file *cf)
   if (cf->filename != NULL) {
     /* If it's a temporary file, remove it. */
     if (cf->is_tempfile)
-      unlink(cf->filename);
+      eth_unlink(cf->filename);
     g_free(cf->filename);
     cf->filename = NULL;
   }
@@ -296,24 +324,33 @@ cf_reset_state(capture_file *cf)
   packet_list_clear();
   packet_list_thaw();
 
-  cf->f_len = 0;
+  cf->f_datalen = 0;
   cf->count = 0;
-  cf->esec  = 0;
-  cf->eusec = 0;
+  nstime_set_zero(&cf->elapsed_time);
 
   reset_tap_listeners();
 
   /* We have no file open. */
   cf->state = FILE_CLOSED;
+
+  fileset_file_closed();
 }
 
 /* Reset everything to a pristine state */
 void
 cf_close(capture_file *cf)
 {
-  cf_reset_state(cf);
+  /* do GUI things even if file is already closed,
+   * e.g. to cleanup things if a capture couldn't be started */
+  cf_callback_invoke(cf_cb_file_closing, cf);
+
+  /* close things, if not already closed before */
+  if(cf->state != FILE_CLOSED) {
 
-  cleanup_dissection();
+         cf_reset_state(cf);
+
+         cleanup_dissection();
+  }
 
   cf_callback_invoke(cf_cb_file_closed, cf);
 }
@@ -324,26 +361,18 @@ cf_read(capture_file *cf)
   int         err;
   gchar       *err_info;
   const gchar *name_ptr;
-  gchar       *load_msg, *load_fmt = "%s";
-  char        *errmsg;
+  const char  *errmsg;
   char         errmsg_errno[1024+1];
   gchar        err_str[2048+1];
   long         data_offset;
   progdlg_t   *progbar = NULL;
   gboolean     stop_flag;
-  /*
-   * XXX - should be "off_t", but Wiretap would need more work to handle
-   * the full size of "off_t" on platforms where it's more than a "long"
-   * as well.
-   */
-  long         file_pos;
-  float        prog_val;
-  int          fd;
-  struct stat  cf_stat;
+  gint64       size, file_pos;
+  float        progbar_val;
   GTimeVal     start_time;
   gchar        status_str[100];
-  int          progbar_nextstep;
-  int          progbar_quantum;
+  gint64       progbar_nextstep;
+  gint64       progbar_quantum;
 
   cum_bytes=0;
 
@@ -353,13 +382,20 @@ cf_read(capture_file *cf)
   cf_callback_invoke(cf_cb_file_read_start, cf);
 
   name_ptr = get_basename(cf->filename);
-  load_msg = g_strdup_printf(load_fmt, name_ptr);
+
+  /* Find the size of the file. */
+  size = wtap_file_size(cf->wth, NULL);
 
   /* Update the progress bar when it gets to this value. */
   progbar_nextstep = 0;
   /* When we reach the value that triggers a progress bar update,
      bump that value by this amount. */
-  progbar_quantum = cf->f_len/N_PROGBAR_UPDATES;
+  if (size >= 0)
+    progbar_quantum = size/N_PROGBAR_UPDATES;
+  else
+    progbar_quantum = 0;
+  /* Progress so far. */
+  progbar_val = 0.0;
 
   packet_list_freeze();
 
@@ -367,61 +403,62 @@ cf_read(capture_file *cf)
   g_get_current_time(&start_time);
 
   while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
-    /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
-       when we update it, we have to run the GTK+ main loop to get it
-       to repaint what's pending, and doing so may involve an "ioctl()"
-       to see if there's any pending input from an X server, and doing
-       that for every packet can be costly, especially on a big file. */
-    if (data_offset >= progbar_nextstep) {
-        file_pos = lseek(cf->filed, 0, SEEK_CUR);
-        prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
-        if (prog_val > 1.0) {
-          /* The file probably grew while we were reading it.
-             Update "cf->f_len", and try again. */
-          fd = wtap_fd(cf->wth);
-          if (fstat(fd, &cf_stat) >= 0) {
-            cf->f_len = cf_stat.st_size;
-            prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
+    if (size >= 0) {
+      /* Create the progress bar if necessary.
+         We check on every iteration of the loop, so that it takes no
+         longer than the standard time to create it (otherwise, for a
+         large file, we might take considerably longer than that standard
+         time in order to get to the next progress bar step). */
+      if (progbar == NULL) {
+        progbar = delayed_create_progress_dlg("Loading", name_ptr,
+          TRUE, &stop_flag, &start_time, progbar_val);
+      }
+
+      /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
+         when we update it, we have to run the GTK+ main loop to get it
+         to repaint what's pending, and doing so may involve an "ioctl()"
+         to see if there's any pending input from an X server, and doing
+         that for every packet can be costly, especially on a big file. */
+      if (data_offset >= progbar_nextstep) {
+          file_pos = wtap_read_so_far(cf->wth, NULL);
+          progbar_val = (gfloat) file_pos / (gfloat) size;
+          if (progbar_val > 1.0) {
+            /* The file probably grew while we were reading it.
+               Update file size, and try again. */
+            size = wtap_file_size(cf->wth, NULL);
+            if (size >= 0)
+              progbar_val = (gfloat) file_pos / (gfloat) size;
+            /* If it's still > 1, either "wtap_file_size()" failed (in which
+               case there's not much we can do about it), or the file
+               *shrank* (in which case there's not much we can do about
+               it); just clip the progress value at 1.0. */
+            if (progbar_val > 1.0)
+              progbar_val = 1.0;
           }
-          /* If it's still > 1, either the "fstat()" failed (in which
-             case there's not much we can do about it), or the file
-             *shrank* (in which case there's not much we can do about
-             it); just clip the progress value at 1.0. */
-          if (prog_val > 1.0)
-            prog_val = 1.0;
-        }
-        if (progbar == NULL) {
-          /* Create the progress bar if necessary */
-          progbar = delayed_create_progress_dlg("Loading", load_msg,
-            &stop_flag, &start_time, prog_val);
-          if (progbar != NULL)
-            g_free(load_msg);
-        }
-        if (progbar != NULL) {
-          g_snprintf(status_str, sizeof(status_str),
-                     "%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
-          update_progress_dlg(progbar, prog_val, status_str);
-        }
-        progbar_nextstep += progbar_quantum;
+          if (progbar != NULL) {
+            g_snprintf(status_str, sizeof(status_str),
+                       "%" PRId64 "KB of %" PRId64 "KB",
+                       file_pos / 1024, size / 1024);
+            update_progress_dlg(progbar, progbar_val, status_str);
+          }
+         progbar_nextstep += progbar_quantum;
+      }
     }
 
     if (stop_flag) {
-      /* Well, the user decided to abort the read.  Destroy the progress
-         bar, close the capture file, and return CF_READ_ABORTED so our caller
-        can do whatever is appropriate when that happens. */
-      destroy_progress_dlg(progbar);
-      cf->state = FILE_READ_ABORTED;   /* so that we're allowed to close it */
-      packet_list_thaw();              /* undo our freeze */
-      cf_close(cf);
-      return CF_READ_ABORTED;
+      /* Well, the user decided to abort the read. He/She will be warned and
+         it might be enough for him/her to work with the already loaded
+         packets.
+         This is especially true for very large capture files, where you don't
+         want to wait loading the whole file (which may last minutes or even
+         hours even on fast machines) just to see that it was the wrong file. */
+      break;
     }
     read_packet(cf, data_offset);
   }
 
   /* We're done reading the file; destroy the progress bar if it was created. */
-  if (progbar == NULL)
-    g_free(load_msg);
-  else
+  if (progbar != NULL)
     destroy_progress_dlg(progbar);
 
   /* We're done reading sequentially through the file. */
@@ -450,6 +487,18 @@ cf_read(capture_file *cf)
   if (cf->first_displayed != NULL)
     packet_list_select_row(0);
 
+  if(stop_flag) {
+    simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
+          "%sFile loading was cancelled!%s\n"
+          "\n"
+                 "The remaining packets in the file were discarded.\n"
+          "\n"
+          "As a lot of packets from the original file will be missing,\n"
+                 "remember to be careful when saving the current content to a file.\n",
+          simple_dialog_primary_start(), simple_dialog_primary_end());
+    return CF_READ_ERROR;
+  }
+
   if (err != 0) {
     /* Put up a message box noting that the read failed somewhere along
        the line.  Don't throw out the stuff we managed to read, though,
@@ -457,8 +506,8 @@ cf_read(capture_file *cf)
     switch (err) {
 
     case WTAP_ERR_UNSUPPORTED_ENCAP:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
-               "The capture file has a packet with a network type that Ethereal doesn't support.\n(%s)",
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The capture file has a packet with a network type that Wireshark doesn't support.\n(%s)",
                err_info);
       g_free(err_info);
       errmsg = errmsg_errno;
@@ -475,7 +524,7 @@ cf_read(capture_file *cf)
       break;
 
     case WTAP_ERR_BAD_RECORD:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                "The capture file appears to be damaged or corrupt.\n(%s)",
                err_info);
       g_free(err_info);
@@ -483,13 +532,13 @@ cf_read(capture_file *cf)
       break;
 
     default:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
               "An error occurred while reading the"
               " capture file: %s.", wtap_strerror(err));
       errmsg = errmsg_errno;
       break;
     }
-    snprintf(err_str, sizeof err_str, errmsg);
+    g_snprintf(err_str, sizeof err_str, errmsg);
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
     return CF_READ_ERROR;
   } else
@@ -503,9 +552,6 @@ cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *er
   cf_status_t cf_status;
 
   cf_status = cf_open(cf, fname, is_tempfile, err);
-  if (cf_status == CF_OK) {
-    cf_callback_invoke(cf_cb_live_capture_started, cf);
-  }
   return cf_status;
 }
 
@@ -519,9 +565,11 @@ cf_continue_tail(capture_file *cf, int to_read, int *err)
 
   packet_list_freeze();
 
+  /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
+
   while (to_read != 0 && (wtap_read(cf->wth, err, &err_info, &data_offset))) {
     if (cf->state == FILE_READ_ABORTED) {
-      /* Well, the user decided to exit Ethereal.  Break out of the
+      /* Well, the user decided to exit Wireshark.  Break out of the
          loop, and let the code below (which is called even if there
         aren't any packets left to read) exit. */
       break;
@@ -530,15 +578,18 @@ cf_continue_tail(capture_file *cf, int to_read, int *err)
     to_read--;
   }
 
-  packet_list_thaw();
+  /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
+         cf->count, cf->state, *err);*/
 
   /* XXX - this cheats and looks inside the packet list to find the final
      row number. */
   if (auto_scroll_live && cf->plist_end != NULL)
     packet_list_moveto_end();
 
+  packet_list_thaw();
+
   if (cf->state == FILE_READ_ABORTED) {
-    /* Well, the user decided to exit Ethereal.  Return CF_READ_ABORTED
+    /* Well, the user decided to exit Wireshark.  Return CF_READ_ABORTED
        so that our caller can kill off the capture child process;
        this will cause an EOF on the pipe from the child, so
        "cf_finish_tail()" will be called, and it will clean up
@@ -546,7 +597,10 @@ cf_continue_tail(capture_file *cf, int to_read, int *err)
     return CF_READ_ABORTED;
   } else if (*err != 0) {
     /* We got an error reading the capture file.
-       XXX - pop up a dialog box? */
+       XXX - pop up a dialog box instead? */
+       g_warning("Error \"%s\" while reading: \"%s\"\n",
+               wtap_strerror(*err), cf->filename);
+
     return CF_READ_ERROR;
   } else
     return CF_READ_OK;
@@ -557,8 +611,11 @@ cf_finish_tail(capture_file *cf, int *err)
 {
   gchar *err_info;
   long data_offset;
-  int         fd;
-  struct stat cf_stat;
+
+  if(cf->wth == NULL) {
+    cf_close(cf);
+    return CF_READ_ERROR;
+  }
 
   packet_list_freeze();
 
@@ -572,6 +629,8 @@ cf_finish_tail(capture_file *cf, int *err)
     read_packet(cf, data_offset);
   }
 
+  packet_list_thaw();
+
   if (cf->state == FILE_READ_ABORTED) {
     /* Well, the user decided to abort the read.  We're only called
        when the child capture process closes the pipe to us (meaning
@@ -582,7 +641,6 @@ cf_finish_tail(capture_file *cf, int *err)
     return CF_READ_ABORTED;
   }
 
-  packet_list_thaw();
   if (auto_scroll_live && cf->plist_end != NULL)
     /* XXX - this cheats and looks inside the packet list to find the final
        row number. */
@@ -591,13 +649,6 @@ cf_finish_tail(capture_file *cf, int *err)
   /* We're done reading sequentially through the file. */
   cf->state = FILE_READ_DONE;
 
-  /* we have to update the f_len field */
-  /* Find the size of the file. */
-  fd = wtap_fd(cf->wth);
-  if (fstat(fd, &cf_stat) >= 0) {
-      cf->f_len = cf_stat.st_size;
-  }
-
   /* We're done reading sequentially through the file; close the
      sequential I/O side, to free up memory it requires. */
   wtap_sequential_close(cf->wth);
@@ -612,8 +663,6 @@ cf_finish_tail(capture_file *cf, int *err)
      WTAP_ENCAP_PER_PACKET). */
   cf->lnk_t = wtap_file_encap(cf->wth);
 
-  cf_callback_invoke(cf_cb_live_capture_finished, cf);
-
   if (*err != 0) {
     /* We got an error reading the capture file.
        XXX - pop up a dialog box? */
@@ -647,11 +696,18 @@ cf_get_display_name(capture_file *cf)
 
 /* XXX - use a macro instead? */
 int
-cf_packet_count(capture_file *cf)
+cf_get_packet_count(capture_file *cf)
 {
     return cf->count;
 }
 
+/* XXX - use a macro instead? */
+void
+cf_set_packet_count(capture_file *cf, int packet_count)
+{
+    cf->count = packet_count;
+}
+
 /* XXX - use a macro instead? */
 gboolean
 cf_is_tempfile(capture_file *cf)
@@ -709,43 +765,37 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
   /* If we don't have the time stamp of the first packet in the
      capture, it's because this is the first packet.  Save the time
      stamp of this packet as the time stamp of the first packet. */
-  if (!firstsec && !firstusec) {
-    firstsec  = fdata->abs_secs;
-    firstusec = fdata->abs_usecs;
+  if (nstime_is_zero(&first_ts)) {
+    first_ts  = fdata->abs_ts;
   }
   /* if this frames is marked as a reference time frame, reset
      firstsec and firstusec to this frame */
   if(fdata->flags.ref_time){
-    firstsec  = fdata->abs_secs;
-    firstusec = fdata->abs_usecs;
+    first_ts = fdata->abs_ts;
   }
 
   /* If we don't have the time stamp of the previous displayed packet,
      it's because this is the first displayed packet.  Save the time
      stamp of this packet as the time stamp of the previous displayed
      packet. */
-  if (!prevsec && !prevusec) {
-    prevsec  = fdata->abs_secs;
-    prevusec = fdata->abs_usecs;
+  if (nstime_is_zero(&prev_ts)) {
+    prev_ts = fdata->abs_ts;
   }
 
   /* Get the time elapsed between the first packet and this packet. */
-  compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
-     fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
+  nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
 
   /* If it's greater than the current elapsed time, set the elapsed time
      to it (we check for "greater than" so as not to be confused by
      time moving backwards). */
-  if ((gint32)cf->esec < fdata->rel_secs
-  || ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
-    cf->esec = fdata->rel_secs;
-    cf->eusec = fdata->rel_usecs;
+  if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
+  || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
+    cf->elapsed_time = fdata->rel_ts;
   }
 
   /* Get the time elapsed between the previous displayed packet and
      this packet. */
-  compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
-       fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
+  nstime_delta(&fdata->del_ts, &fdata->abs_ts, &prev_ts);
 
   /* If either
 
@@ -786,7 +836,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
   } else
     fdata->flags.passed_dfilter = 1;
 
-  if( (fdata->flags.passed_dfilter) 
+  if( (fdata->flags.passed_dfilter)
    || (edt->pi.fd->flags.ref_time) ){
     /* This frame either passed the display filter list or is marked as
        a time reference frame.  All time reference frames are displayed
@@ -823,7 +873,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
 
     row = packet_list_append(cf->cinfo.col_data, fdata);
 
-    /* colorize packet: if packet is marked, use preferences, 
+    /* colorize packet: if packet is marked, use preferences,
        otherwise try to apply color filters */
       if (fdata->flags.marked) {
           fdata->color_filter = NULL;
@@ -834,8 +884,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
 
     /* Set the time of the previous displayed frame to the time of this
        frame. */
-    prevsec = fdata->abs_secs;
-    prevusec = fdata->abs_usecs;
+    prev_ts = fdata->abs_ts;
 
     cf->displayed_count++;
   } else {
@@ -861,6 +910,7 @@ read_packet(capture_file *cf, long offset)
   /* Allocate the next list entry, and add it to the list. */
   fdata = g_mem_chunk_alloc(cf->plist_chunk);
 
+  fdata->num = 0;
   fdata->next = NULL;
   fdata->prev = NULL;
   fdata->pfd  = NULL;
@@ -868,12 +918,13 @@ read_packet(capture_file *cf, long offset)
   fdata->cap_len  = phdr->caplen;
   fdata->file_off = offset;
   fdata->lnk_t = phdr->pkt_encap;
-  fdata->abs_secs  = phdr->ts.tv_sec;
-  fdata->abs_usecs = phdr->ts.tv_usec;
   fdata->flags.encoding = CHAR_ASCII;
   fdata->flags.visited = 0;
   fdata->flags.marked = 0;
   fdata->flags.ref_time = 0;
+  fdata->color_filter = NULL;
+
+  fdata->abs_ts  = *((nstime_t *) &phdr->ts);
 
   passed = TRUE;
   if (cf->rfcode) {
@@ -893,6 +944,7 @@ read_packet(capture_file *cf, long offset)
     cf->plist_end = fdata;
 
     cf->count++;
+    cf->f_datalen = offset + phdr->caplen;
     fdata->num = cf->count;
     add_packet_to_packet_list(fdata, cf, pseudo_header, buf, TRUE);
   } else {
@@ -920,26 +972,21 @@ cf_merge_files(char **out_filenamep, int in_file_count,
   wtap_dumper      *pdh;
   int               open_err, read_err, write_err, close_err;
   gchar            *err_info;
-  int               err_fileno; 
+  int               err_fileno;
   int               i;
   char              errmsg_errno[1024+1];
   gchar             err_str[2048+1];
-  char             *errmsg;
+  const char       *errmsg;
   gboolean          got_read_error = FALSE, got_write_error = FALSE;
   long              data_offset;
   progdlg_t        *progbar = NULL;
   gboolean          stop_flag;
-  /*
-   * XXX - should be "off_t", but Wiretap would need more work to handle
-   * the full size of "off_t" on platforms where it's more than a "long"
-   * as well.
-   */
-  long        f_len, file_pos;
-  float       prog_val;
-  GTimeVal    start_time;
-  gchar       status_str[100];
-  int         progbar_nextstep;
-  int         progbar_quantum;
+  gint64            f_len, file_pos;
+  float             progbar_val;
+  GTimeVal          start_time;
+  gchar             status_str[100];
+  gint64            progbar_nextstep;
+  gint64            progbar_quantum;
 
   /* open the input files */
   if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
@@ -952,7 +999,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
 
   if (*out_filenamep != NULL) {
     out_filename = *out_filenamep;
-    out_fd = open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
+    out_fd = eth_open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
     if (out_fd == -1)
       open_err = errno;
   } else {
@@ -972,9 +1019,10 @@ cf_merge_files(char **out_filenamep, int in_file_count,
 
   pdh = wtap_dump_fdopen(out_fd, file_type,
       merge_select_frame_type(in_file_count, in_files),
-      merge_max_snapshot_length(in_file_count, in_files), &open_err);
+      merge_max_snapshot_length(in_file_count, in_files),
+         FALSE /* compressed */, &open_err);
   if (pdh == NULL) {
-    close(out_fd);
+    eth_close(out_fd);
     merge_close_in_files(in_file_count, in_files);
     free(in_files);
     cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
@@ -992,6 +1040,8 @@ cf_merge_files(char **out_filenamep, int in_file_count,
   /* When we reach the value that triggers a progress bar update,
      bump that value by this amount. */
   progbar_quantum = f_len/N_PROGBAR_UPDATES;
+  /* Progress so far. */
+  progbar_val = 0.0;
 
   stop_flag = FALSE;
   g_get_current_time(&start_time);
@@ -1015,31 +1065,47 @@ cf_merge_files(char **out_filenamep, int in_file_count,
     for (i = 0; i < in_file_count; i++)
       data_offset += in_files[i].data_offset;
 
+    /* Create the progress bar if necessary.
+       We check on every iteration of the loop, so that it takes no
+       longer than the standard time to create it (otherwise, for a
+       large file, we might take considerably longer than that standard
+       time in order to get to the next progress bar step). */
+    if (progbar == NULL) {
+      progbar = delayed_create_progress_dlg("Merging", "files",
+        FALSE, &stop_flag, &start_time, progbar_val);
+    }
+
+    /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
+       when we update it, we have to run the GTK+ main loop to get it
+       to repaint what's pending, and doing so may involve an "ioctl()"
+       to see if there's any pending input from an X server, and doing
+       that for every packet can be costly, especially on a big file. */
     if (data_offset >= progbar_nextstep) {
         /* Get the sum of the seek positions in all of the files. */
         file_pos = 0;
         for (i = 0; i < in_file_count; i++)
-          file_pos += lseek(wtap_fd(in_files[i].wth), 0, SEEK_CUR);
-        prog_val = (gfloat) file_pos / (gfloat) f_len;
-        if (prog_val > 1.0) {
+          file_pos += wtap_read_so_far(in_files[i].wth, NULL);
+        progbar_val = (gfloat) file_pos / (gfloat) f_len;
+        if (progbar_val > 1.0) {
           /* Some file probably grew while we were reading it.
              That "shouldn't happen", so we'll just clip the progress
              value at 1.0. */
-          prog_val = 1.0;
-        }
-        if (progbar == NULL) {
-          /* Create the progress bar if necessary */
-          progbar = delayed_create_progress_dlg("Merging", "files",
-            &stop_flag, &start_time, prog_val);
+          progbar_val = 1.0;
         }
         if (progbar != NULL) {
           g_snprintf(status_str, sizeof(status_str),
-                     "%luKB of %luKB", file_pos / 1024, f_len / 1024);
-          update_progress_dlg(progbar, prog_val, status_str);
+                     "%" PRId64 "KB of %" PRId64 "KB",
+                     file_pos / 1024, f_len / 1024);
+          update_progress_dlg(progbar, progbar_val, status_str);
         }
         progbar_nextstep += progbar_quantum;
     }
 
+    if (stop_flag) {
+      /* Well, the user decided to abort the merge. */
+      break;
+    }
+
     if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
          wtap_buf_ptr(wth), &write_err)) {
       got_write_error = TRUE;
@@ -1069,8 +1135,8 @@ cf_merge_files(char **out_filenamep, int in_file_count,
        switch (read_err) {
 
        case WTAP_ERR_UNSUPPORTED_ENCAP:
-         snprintf(errmsg_errno, sizeof(errmsg_errno),
-                  "The capture file %%s has a packet with a network type that Ethereal doesn't support.\n(%s)",
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                  "The capture file %%s has a packet with a network type that Wireshark doesn't support.\n(%s)",
                   err_info);
          g_free(err_info);
          errmsg = errmsg_errno;
@@ -1087,7 +1153,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
          break;
 
        case WTAP_ERR_BAD_RECORD:
-         snprintf(errmsg_errno, sizeof(errmsg_errno),
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                   "The capture file %%s appears to be damaged or corrupt.\n(%s)",
                   err_info);
          g_free(err_info);
@@ -1095,13 +1161,13 @@ cf_merge_files(char **out_filenamep, int in_file_count,
          break;
 
        default:
-         snprintf(errmsg_errno, sizeof(errmsg_errno),
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                   "An error occurred while reading the"
                   " capture file %%s: %s.", wtap_strerror(read_err));
          errmsg = errmsg_errno;
          break;
        }
-       snprintf(err_str, sizeof err_str, errmsg, in_files[i].filename);
+       g_snprintf(err_str, sizeof err_str, errmsg, in_files[i].filename);
         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
       }
     }
@@ -1112,15 +1178,21 @@ cf_merge_files(char **out_filenamep, int in_file_count,
     cf_write_failure_alert_box(out_filename, write_err);
   }
 
-  return (!got_read_error && !got_write_error) ? CF_OK : CF_ERROR;
+  if (got_read_error || got_write_error || stop_flag) {
+    /* Callers aren't expected to treat an error or an explicit abort
+       differently - we put up error dialogs ourselves, so they don't
+       have to. */
+    return CF_ERROR;
+  } else
+    return CF_READ_OK;
 }
 
 cf_status_t
 cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
 {
-  dfilter_t *dfcode;
-  char      *filter_new = dftext ? dftext : "";
-  char      *filter_old = cf->dfilter ? cf->dfilter : "";
+  dfilter_t  *dfcode;
+  const char *filter_new = dftext ? dftext : "";
+  const char *filter_old = cf->dfilter ? cf->dfilter : "";
 
   /* if new filter equals old one, do nothing unless told to do so */
   if (!force && strcmp(filter_new, filter_old) == 0) {
@@ -1141,7 +1213,7 @@ cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
       gchar *safe_dftext = simple_dialog_format_message(dftext);
       gchar *safe_dfilter_error_msg = simple_dialog_format_message(
          dfilter_error_msg);
-      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, 
+      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
           "%s%s%s\n"
           "\n"
           "The following display filter isn't a valid display filter:\n%s\n"
@@ -1226,7 +1298,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   int         selected_row, prev_row, preceding_row, following_row;
   gboolean    selected_frame_seen;
   int         row;
-  float       prog_val;
+  float       progbar_val;
   GTimeVal    start_time;
   gchar       status_str[100];
   int         progbar_nextstep;
@@ -1271,10 +1343,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   /* Iterate through the list of frames.  Call a routine for each frame
      to check whether it should be displayed and, if so, add it to
      the display list. */
-  firstsec = 0;
-  firstusec = 0;
-  prevsec = 0;
-  prevusec = 0;
+  nstime_set_zero(&first_ts);
+  nstime_set_zero(&prev_ts);
 
   /* Update the progress bar when it gets to this value. */
   progbar_nextstep = 0;
@@ -1283,6 +1353,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   progbar_quantum = cf->count/N_PROGBAR_UPDATES;
   /* Count of packets at which we've looked. */
   count = 0;
+  /* Progress so far. */
+  progbar_val = 0.0;
 
   stop_flag = FALSE;
   g_get_current_time(&start_time);
@@ -1299,6 +1371,16 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   selected_frame_seen = FALSE;
 
   for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
+    /* Create the progress bar if necessary.
+       We check on every iteration of the loop, so that it takes no
+       longer than the standard time to create it (otherwise, for a
+       large file, we might take considerably longer than that standard
+       time in order to get to the next progress bar step). */
+    if (progbar == NULL)
+      progbar = delayed_create_progress_dlg(action, action_item, TRUE,
+                                            &stop_flag, &start_time,
+                                            progbar_val);
+
     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
        when we update it, we have to run the GTK+ main loop to get it
        to repaint what's pending, and doing so may involve an "ioctl()"
@@ -1309,17 +1391,12 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        * with count == 0, so let's assert that
        */
       g_assert(cf->count > 0);
-      prog_val = (gfloat) count / cf->count;
-
-      if (progbar == NULL)
-        /* Create the progress bar if necessary */
-        progbar = delayed_create_progress_dlg(action, action_item, &stop_flag,
-          &start_time, prog_val);
+      progbar_val = (gfloat) count / cf->count;
 
       if (progbar != NULL) {
         g_snprintf(status_str, sizeof(status_str),
                   "%4u of %u frames", count, cf->count);
-        update_progress_dlg(progbar, prog_val, status_str);
+        update_progress_dlg(progbar, progbar_val, status_str);
       }
 
       progbar_nextstep += progbar_quantum;
@@ -1399,7 +1476,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        these frames, as they won't have been seen by this sequential
        pass, but the only alternative I see is to keep scanning them
        even though the user requested that the scan stop, and that
-       would leave the user stuck with an Ethereal grinding on
+       would leave the user stuck with an Wireshark grinding on
        until it finishes.  Should we just stick them with that? */
     for (; fdata != NULL; fdata = fdata->next) {
       fdata->flags.visited = 0;
@@ -1443,19 +1520,6 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
            have to select the first displayed frame after the selected
            frame. */
         selected_row = following_row;
-      } else {
-        /* Choose the closer of the last displayed frame before the
-           selected frame and the first displayed frame after the
-           selected frame; in case of a tie, choose the first displayed
-           frame after the selected frame. */
-        if (following_frame->num - selected_frame->num <=
-            selected_frame->num - preceding_frame->num) {
-          selected_row = following_row;
-        } else {
-          /* The previous frame is closer to the selected frame than the
-             next frame. */
-          selected_row = preceding_row;
-        }
       }
     }
   }
@@ -1477,9 +1541,9 @@ typedef enum {
   PSP_FAILED
 } psp_return_t;
 
-psp_return_t
+static psp_return_t
 process_specified_packets(capture_file *cf, packet_range_t *range,
-    const char *string1, const char *string2,
+    const char *string1, const char *string2, gboolean terminate_is_stop,
     gboolean (*callback)(capture_file *, frame_data *,
                          union wtap_pseudo_header *, const guint8 *, void *),
     void *callback_args)
@@ -1508,6 +1572,8 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
   progbar_quantum = cf->count/N_PROGBAR_UPDATES;
   /* Count of packets at which we've looked. */
   progbar_count = 0;
+  /* Progress so far. */
+  progbar_val = 0.0;
 
   progbar_stop_flag = FALSE;
   g_get_current_time(&progbar_start_time);
@@ -1517,6 +1583,18 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
   /* Iterate through the list of packets, printing the packets that
      were selected by the current display filter.  */
   for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
+    /* Create the progress bar if necessary.
+       We check on every iteration of the loop, so that it takes no
+       longer than the standard time to create it (otherwise, for a
+       large file, we might take considerably longer than that standard
+       time in order to get to the next progress bar step). */
+    if (progbar == NULL)
+      progbar = delayed_create_progress_dlg(string1, string2,
+                                            terminate_is_stop,
+                                            &progbar_stop_flag,
+                                            &progbar_start_time,
+                                            progbar_val);
+
     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
        when we update it, we have to run the GTK+ main loop to get it
        to repaint what's pending, and doing so may involve an "ioctl()"
@@ -1529,13 +1607,6 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
       g_assert(cf->count > 0);
       progbar_val = (gfloat) progbar_count / cf->count;
 
-      if (progbar == NULL)
-        /* Create the progress bar if necessary */
-        progbar = delayed_create_progress_dlg(string1, string2,
-                                              &progbar_stop_flag,
-                                              &progbar_start_time,
-                                              progbar_val);
-
       if (progbar != NULL) {
         g_snprintf(progbar_status_str, sizeof(progbar_status_str),
                    "%4u of %u packets", progbar_count, cf->count);
@@ -1547,8 +1618,8 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
 
     if (progbar_stop_flag) {
       /* Well, the user decided to abort the operation.  Just stop,
-         and arrange to return TRUE to our caller, so they know it
-         was stopped explicitly. */
+         and arrange to return PSP_STOPPED to our caller, so they know
+         it was stopped explicitly. */
       ret = PSP_STOPPED;
       break;
     }
@@ -1593,8 +1664,9 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
 static gboolean
 retap_packet(capture_file *cf _U_, frame_data *fdata,
              union wtap_pseudo_header *pseudo_header, const guint8 *pd,
-             void *argsp _U_)
+             void *argsp)
 {
+  column_info *cinfo = argsp;
   epan_dissect_t *edt;
 
   /* If we have tap listeners, allocate a protocol tree root node, so that
@@ -1602,7 +1674,7 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
      be evaluated. */
   edt = epan_dissect_new(num_tap_filters != 0, FALSE);
   tap_queue_init(edt);
-  epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
+  epan_dissect_run(edt, pseudo_header, pd, fdata, cinfo);
   tap_push_tapped_queue(edt);
   epan_dissect_free(edt);
 
@@ -1610,7 +1682,7 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
 }
 
 cf_read_status_t
-cf_retap_packets(capture_file *cf)
+cf_retap_packets(capture_file *cf, gboolean do_columns)
 {
   packet_range_t range;
 
@@ -1622,8 +1694,8 @@ cf_retap_packets(capture_file *cf)
   packet_range_init(&range);
   packet_range_process_init(&range);
   switch (process_specified_packets(cf, &range, "Refiltering statistics on",
-                                    "all packets", retap_packet,
-                                    NULL)) {
+                                    "all packets", TRUE, retap_packet,
+                                    do_columns ? &cf->cinfo : NULL)) {
   case PSP_FINISHED:
     /* Completed successfully. */
     return CF_OK;
@@ -1673,7 +1745,7 @@ print_packet(capture_file *cf, frame_data *fdata,
   /* Create the protocol tree, and make it visible, if we're printing
      the dissection or the hex data.
      XXX - do we need it if we're just printing the hex data? */
-  proto_tree_needed = 
+  proto_tree_needed =
       args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex;
   edt = epan_dissect_new(proto_tree_needed, proto_tree_needed);
 
@@ -1699,7 +1771,7 @@ print_packet(capture_file *cf, frame_data *fdata,
    * We generate bookmarks, if the output format supports them.
    * The name is "__frameN__".
    */
-  sprintf(bookmark_name, "__frame%u__", fdata->num);
+  g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
 
   if (args->print_args->print_summary) {
     if (args->print_header_line) {
@@ -1750,7 +1822,7 @@ print_packet(capture_file *cf, frame_data *fdata,
      * Generate a bookmark, using "Frame N" as the title, as we're not
      * printing the summary line.
      */
-    sprintf(bookmark_title, "Frame %u", fdata->num);
+    g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
     if (!print_bookmark(args->print_args->stream, bookmark_name,
                         bookmark_title))
       goto fail;
@@ -1885,7 +1957,7 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Printing",
-                                  "selected packets", print_packet,
+                                  "selected packets", TRUE, print_packet,
                                   &callback_args);
 
   if (callback_args.header_line_buf != NULL)
@@ -1958,7 +2030,7 @@ cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
   FILE        *fh;
   psp_return_t ret;
 
-  fh = fopen(print_args->file, "w");
+  fh = eth_fopen(print_args->file, "w");
   if (fh == NULL)
     return CF_PRINT_OPEN_ERROR;        /* attempt to open destination failed */
 
@@ -1971,8 +2043,8 @@ cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
-                                  "selected packets", write_pdml_packet,
-                                  fh);
+                                  "selected packets", TRUE,
+                                  write_pdml_packet, fh);
 
   switch (ret) {
 
@@ -2029,7 +2101,7 @@ cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
   FILE        *fh;
   psp_return_t ret;
 
-  fh = fopen(print_args->file, "w");
+  fh = eth_fopen(print_args->file, "w");
   if (fh == NULL)
     return CF_PRINT_OPEN_ERROR;        /* attempt to open destination failed */
 
@@ -2042,8 +2114,8 @@ cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
-                                  "selected packets", write_psml_packet,
-                                  fh);
+                                  "selected packets", TRUE,
+                                  write_psml_packet, fh);
 
   switch (ret) {
 
@@ -2100,7 +2172,7 @@ cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
   FILE        *fh;
   psp_return_t ret;
 
-  fh = fopen(print_args->file, "w");
+  fh = eth_fopen(print_args->file, "w");
   if (fh == NULL)
     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
 
@@ -2113,8 +2185,8 @@ cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
-                                  "selected packets", write_csv_packet,
-                                  fh);
+                                  "selected packets", TRUE,
+                                  write_csv_packet, fh);
 
   switch (ret) {
 
@@ -2156,7 +2228,7 @@ cf_change_time_formats(capture_file *cf)
   int         count;
   int         row;
   int         i;
-  float       prog_val;
+  float       progbar_val;
   GTimeVal    start_time;
   gchar       status_str[100];
   int         progbar_nextstep;
@@ -2164,6 +2236,10 @@ cf_change_time_formats(capture_file *cf)
   int         first, last;
   gboolean    sorted_by_frame_column;
 
+
+  /* adjust timestamp precision if auto is selected */
+  cf_timestamp_auto_precision(cf);
+
   /* Are there any columns with time stamps in the "command-line-specified"
      format?
 
@@ -2190,6 +2266,8 @@ cf_change_time_formats(capture_file *cf)
   progbar_quantum = cf->count/N_PROGBAR_UPDATES;
   /* Count of packets at which we've looked. */
   count = 0;
+  /* Progress so far. */
+  progbar_val = 0.0;
 
   /*  If the rows are currently sorted by the frame column then we know
    *  the row number of each packet: it's the row number of the previously
@@ -2220,6 +2298,15 @@ cf_change_time_formats(capture_file *cf)
      any columns that show the time in the "command-line-specified"
      format and, if so, update that row. */
   for (fdata = cf->plist, row = -1; fdata != NULL; fdata = fdata->next) {
+    /* Create the progress bar if necessary.
+       We check on every iteration of the loop, so that it takes no
+       longer than the standard time to create it (otherwise, for a
+       large file, we might take considerably longer than that standard
+       time in order to get to the next progress bar step). */
+    if (progbar == NULL)
+      progbar = delayed_create_progress_dlg("Changing", "time display",
+        TRUE, &stop_flag, &start_time, progbar_val);
+
     /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
        when we update it, we have to run the GTK+ main loop to get it
        to repaint what's pending, and doing so may involve an "ioctl()"
@@ -2231,17 +2318,12 @@ cf_change_time_formats(capture_file *cf)
        */
       g_assert(cf->count > 0);
 
-      prog_val = (gfloat) count / cf->count;
-
-      if (progbar == NULL)
-        /* Create the progress bar if necessary */
-        progbar = delayed_create_progress_dlg("Changing", "time display", 
-          &stop_flag, &start_time, prog_val);
+      progbar_val = (gfloat) count / cf->count;
 
       if (progbar != NULL) {
         g_snprintf(status_str, sizeof(status_str),
                    "%4u of %u packets", count, cf->count);
-        update_progress_dlg(progbar, prog_val, status_str);
+        update_progress_dlg(progbar, progbar_val, status_str);
       }
 
       progbar_nextstep += progbar_quantum;
@@ -2374,7 +2456,7 @@ match_subtree_text(proto_node *node, gpointer data)
     label_ptr = label_str;
     proto_item_fill_label(fi, label_str);
   }
-    
+
   /* Does that label match? */
   label_len = strlen(label_ptr);
   for (i = 0; i < label_len; i++) {
@@ -2391,7 +2473,7 @@ match_subtree_text(proto_node *node, gpointer data)
     } else
       c_match = 0;
   }
-  
+
   /* Recurse into the subtree, if it exists */
   if (node->first_child != NULL)
     proto_tree_children_foreach(node, match_subtree_text, mdata);
@@ -2642,11 +2724,12 @@ find_packet(capture_file *cf,
   int         err;
   gchar      *err_info;
   int         row;
-  float       prog_val;
+  float       progbar_val;
   GTimeVal    start_time;
   gchar       status_str[100];
   int         progbar_nextstep;
   int         progbar_quantum;
+  char       *title;
 
   start_fd = cf->current_frame;
   if (start_fd != NULL)  {
@@ -2656,16 +2739,29 @@ find_packet(capture_file *cf,
     count = 0;
     fdata = start_fd;
 
+    /* Update the progress bar when it gets to this value. */
     progbar_nextstep = 0;
     /* When we reach the value that triggers a progress bar update,
        bump that value by this amount. */
     progbar_quantum = cf->count/N_PROGBAR_UPDATES;
+    /* Progress so far. */
+    progbar_val = 0.0;
 
     stop_flag = FALSE;
     g_get_current_time(&start_time);
 
     fdata = start_fd;
+    title = cf->sfilter?cf->sfilter:"";
     for (;;) {
+      /* Create the progress bar if necessary.
+         We check on every iteration of the loop, so that it takes no
+         longer than the standard time to create it (otherwise, for a
+         large file, we might take considerably longer than that standard
+         time in order to get to the next progress bar step). */
+      if (progbar == NULL)
+         progbar = delayed_create_progress_dlg("Searching", title,
+           FALSE, &stop_flag, &start_time, progbar_val);
+
       /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
          when we update it, we have to run the GTK+ main loop to get it
          to repaint what's pending, and doing so may involve an "ioctl()"
@@ -2677,17 +2773,12 @@ find_packet(capture_file *cf,
          */
         g_assert(cf->count > 0);
 
-        prog_val = (gfloat) count / cf->count;
-
-        /* Create the progress bar if necessary */
-        if (progbar == NULL)
-           progbar = delayed_create_progress_dlg("Searching", cf->sfilter, 
-             &stop_flag, &start_time, prog_val);
+        progbar_val = (gfloat) count / cf->count;
 
         if (progbar != NULL) {
           g_snprintf(status_str, sizeof(status_str),
                      "%4u of %u packets", count, cf->count);
-          update_progress_dlg(progbar, prog_val, status_str);
+          update_progress_dlg(progbar, progbar_val, status_str);
         }
 
         progbar_nextstep += progbar_quantum;
@@ -2812,14 +2903,14 @@ cf_goto_frame(capture_file *cf, guint fnumber)
   if (fdata == NULL) {
     /* we didn't find a packet with that packet number */
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                 "There is no packet with that packet number.");
+                 "There is no packet with the packet number %u.", fnumber);
     return FALSE;      /* we failed to go to that packet */
   }
   if (!fdata->flags.passed_dfilter) {
     /* that packet currently isn't displayed */
     /* XXX - add it to the set of displayed packets? */
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                 "That packet isn't currently being displayed.");
+                 "The packet number %u isn't currently being displayed.", fnumber);
     return FALSE;      /* we failed to go to that packet */
   }
 
@@ -2952,6 +3043,11 @@ cf_select_packet(capture_file *cf, int row)
          fdata = cf->first_displayed;
   }
 
+  /* If fdata _still_ isn't set simply give up. */
+  if (fdata == NULL) {
+    return;
+  }
+
   /* Get the data in that frame. */
   if (!wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
                       cf->pd, fdata->cap_len, &err, &err_info)) {
@@ -3052,8 +3148,7 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   int           err;
 
   /* init the wtap header for saving */
-  hdr.ts.tv_sec  = fdata->abs_secs;
-  hdr.ts.tv_usec = fdata->abs_usecs;
+  hdr.ts         = *(struct wtap_nstime *) &fdata->abs_ts;
   hdr.caplen     = fdata->cap_len;
   hdr.len        = fdata->pkt_len;
   hdr.pkt_encap  = fdata->lnk_t;
@@ -3067,34 +3162,19 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
 }
 
 cf_status_t
-cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format)
+cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
 {
   gchar        *from_filename;
   int           err;
   gboolean      do_copy;
   wtap_dumper  *pdh;
-  struct stat   infile, outfile;
   save_callback_args_t callback_args;
 
   cf_callback_invoke(cf_cb_file_safe_started, (gpointer) fname);
 
-  /*
-   * Check that the from file is not the same as to file
-   * We do it here so we catch all cases ...
-   * Unfortunately, the file requester gives us an absolute file
-   * name and the read file name may be relative (if supplied on
-   * the command line). From Joerg Mayer.
-   *
-   * This is a bit tricky on win32. The st_ino field is documented as:
-   * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
-   * but it *is* set to zero if stat() returns without an error,
-   * so this is working, but maybe not quite the way expected. ULFL
-   */
-   infile.st_ino = 1;   /* These prevent us from getting equality         */
-   outfile.st_ino = 2;  /* If one or other of the files is not accessible */
-   stat(cf->filename, &infile);
-   stat(fname, &outfile);
-   if (infile.st_ino == outfile.st_ino) {
+  /* don't write over an existing file. */
+  /* this should've been already checked by our caller, just to be sure... */
+  if (file_exists(fname)) {
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
       "%sCapture file: \"%s\" already exists!%s\n\n"
       "Please choose a different filename.",
@@ -3104,11 +3184,7 @@ cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_f
 
   packet_range_process_init(range);
 
-  /* Used to be :
-   * if (!save_filtered && !save_marked && !save_manual_range && 
-   *     !save_marked_range && !save_curr && save_format == cf->cd_t) {
-   */ 
-       
+
   if (packet_range_process_all(range) && save_format == cf->cd_t) {
     /* We're not filtering packets, and we're saving it in the format
        it's already in, so we can just move or copy the raw data. */
@@ -3118,7 +3194,7 @@ cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_f
          capture, so it doesn't need to stay around under that name;
         first, try renaming the capture buffer file to the new name. */
 #ifndef _WIN32
-      if (rename(cf->filename, fname) == 0) {
+      if (eth_rename(cf->filename, fname) == 0) {
        /* That succeeded - there's no need to copy the source file. */
        from_filename = NULL;
        do_copy = FALSE;
@@ -3161,7 +3237,8 @@ cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_f
     /* Either we're filtering packets, or we're saving in a different
        format; we can't do that by copying or moving the capture file,
        we have to do it by writing the packets out in Wiretap. */
-    pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap, &err);
+    pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap,
+               compressed, &err);
     if (pdh == NULL) {
       cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
       goto fail;
@@ -3182,9 +3259,8 @@ cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_f
        "range" since we initialized it. */
     callback_args.pdh = pdh;
     callback_args.fname = fname;
-    switch (process_specified_packets(cf, range, "Saving",
-                                      "selected packets", save_packet,
-                                      &callback_args)) {
+    switch (process_specified_packets(cf, range, "Saving", "selected packets",
+                                      TRUE, save_packet, &callback_args)) {
 
     case PSP_FINISHED:
       /* Completed successfully. */
@@ -3268,21 +3344,21 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_RANDOM_OPEN_PIPE:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" is a pipe or FIFO; Ethereal can't read pipe or FIFO files.",
+                   "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.",
                    filename);
       break;
 
     case WTAP_ERR_FILE_UNKNOWN_FORMAT:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" isn't a capture file in a format Ethereal understands.",
+                   "The file \"%s\" isn't a capture file in a format Wireshark understands.",
                    filename);
       break;
 
     case WTAP_ERR_UNSUPPORTED:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" isn't a capture file in a format Ethereal understands.\n"
+                   "The file \"%s\" isn't a capture file in a format Wireshark understands.\n"
                    "(%s)",
                    filename, err_info);
       g_free(err_info);
@@ -3299,16 +3375,16 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
       /* Seen only when opening a capture file for writing. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "Ethereal doesn't support writing capture files in that format.");
+                   "Wireshark doesn't support writing capture files in that format.");
       break;
 
     case WTAP_ERR_UNSUPPORTED_ENCAP:
       if (for_writing) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "Ethereal can't save this capture in that format.");
+                     "Wireshark can't save this capture in that format.");
       } else {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "The file \"%s\" is a capture for a network type that Ethereal doesn't support.\n"
+                     "The file \"%s\" is a capture for a network type that Wireshark doesn't support.\n"
                      "(%s)",
                      filename, err_info);
         g_free(err_info);
@@ -3318,10 +3394,10 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
       if (for_writing) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "Ethereal can't save this capture in that format.");
+                     "Wireshark can't save this capture in that format.");
       } else {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "The file \"%s\" is a capture for a network type that Ethereal doesn't support.",
+                     "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
                      filename);
       }
       break;
@@ -3360,6 +3436,11 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
                    filename);
       break;
 
+    case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
+      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+                   "Gzip compression not supported by this file type.");
+      break;
+
     default:
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "The file \"%s\" could not be %s: %s.",
@@ -3374,10 +3455,10 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
   }
 }
 
-static char *
+static const char *
 file_rename_error_message(int err)
 {
-  char *errmsg;
+  const char *errmsg;
   static char errmsg_errno[1024+1];
 
   switch (err) {
@@ -3391,7 +3472,7 @@ file_rename_error_message(int err)
     break;
 
   default:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                    "The file \"%%s\" could not be moved: %s.",
                                wtap_strerror(err));
     errmsg = errmsg_errno;
@@ -3408,19 +3489,19 @@ cf_read_error_message(int err, const gchar *err_info)
   switch (err) {
 
   case WTAP_ERR_UNSUPPORTED_ENCAP:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
-               "The file \"%%s\" has a packet with a network type that Ethereal doesn't support.\n(%s)",
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The file \"%%s\" has a packet with a network type that Wireshark doesn't support.\n(%s)",
                err_info);
       break;
 
   case WTAP_ERR_BAD_RECORD:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
             "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
             wtap_strerror(err), err_info);
     break;
 
   default:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
             "An error occurred while reading from the file \"%%s\": %s.",
             wtap_strerror(err));
     break;
@@ -3542,7 +3623,7 @@ copy_binary_file(const char *from_filename, const char *to_filename)
   guint8        pd[65536];
 
   /* Copy the raw bytes of the file. */
-  from_fd = open(from_filename, O_RDONLY | O_BINARY);
+  from_fd = eth_open(from_filename, O_RDONLY | O_BINARY, 0000 /* no creation so don't matter */);
   if (from_fd < 0) {
     open_failure_alert_box(from_filename, errno, FALSE);
     goto done;
@@ -3553,23 +3634,23 @@ copy_binary_file(const char *from_filename, const char *to_filename)
      may open the file in text mode, not binary mode, but we want
      to copy the raw bytes of the file, so we need the output file
      to be open in binary mode. */
-  to_fd = open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+  to_fd = eth_open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
   if (to_fd < 0) {
     open_failure_alert_box(to_filename, errno, TRUE);
-    close(from_fd);
+    eth_close(from_fd);
     goto done;
   }
 
-  while ((nread = read(from_fd, pd, sizeof pd)) > 0) {
-    nwritten = write(to_fd, pd, nread);
+  while ((nread = eth_read(from_fd, pd, sizeof pd)) > 0) {
+    nwritten = eth_write(to_fd, pd, nread);
     if (nwritten < nread) {
       if (nwritten < 0)
        err = errno;
       else
        err = WTAP_ERR_SHORT_WRITE;
       write_failure_alert_box(to_filename, err);
-      close(from_fd);
-      close(to_fd);
+      eth_close(from_fd);
+      eth_close(to_fd);
       goto done;
     }
   }
@@ -3578,12 +3659,12 @@ copy_binary_file(const char *from_filename, const char *to_filename)
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                  "An error occurred while reading from the file \"%s\": %s.",
                  from_filename, strerror(err));
-    close(from_fd);
-    close(to_fd);
+    eth_close(from_fd);
+    eth_close(to_fd);
     goto done;
   }
-  close(from_fd);
-  if (close(to_fd) < 0) {
+  eth_close(from_fd);
+  if (eth_close(to_fd) < 0) {
     write_failure_alert_box(to_filename, errno);
     goto done;
   }