Change a comment to be slightly easier to read
[metze/wireshark/wip.git] / reordercap.c
index b9c0763f8a35aae1bb89522d9ed89947ed6371b4..92469746821ffa5c787fef3338c6d51d6db5a790 100644 (file)
@@ -1,8 +1,6 @@
 /* Reorder the frames from an input dump file, and write to output dump file.
  * Martin Mathieson and Jakub Jawadzki
  *
- * $Id$
- *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
 #include <unistd.h>
 #endif
 
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+
+#ifdef HAVE_LIBZ
+#include <zlib.h>      /* to get the libz version number */
+#endif
+
 #include "wtap.h"
 
 #ifndef HAVE_GETOPT
 #include "wsutil/wsgetopt.h"
 #endif
 
-/* Show command-line usage */
-static void usage(gboolean is_error)
-{
-    FILE *output;
+#include <wsutil/strnatcmp.h>
+#include <wsutil/file_util.h>
+#include <wsutil/crash_info.h>
+#include <wsutil/copyright_info.h>
+#include <wsutil/os_version_info.h>
+#include <wsutil/ws_version_info.h>
 
-    if (!is_error) {
-        output = stdout;
-    }
-    else {
-        output = stderr;
-    }
+#include "version_info.h"
 
-    fprintf(output, "Reordercap %s"
-#ifdef GITVERSION
-                      " (" GITVERSION " from " GITBRANCH ")"
-#endif
-                      "\n", VERSION);
-    fprintf(output, "Reorder timestamps of input file frames into output file.\n");
-    fprintf(output, "See http://www.wireshark.org for more information.\n");
+static void
+show_version(GString *comp_info_str, GString *runtime_info_str)
+{
+    printf("Reordercap (Wireshark) %s\n"
+           "\n"
+           "%s"
+           "\n"
+           "%s"
+           "\n"
+           "%s",
+           get_ws_vcs_version_info(), get_copyright_info(),
+           comp_info_str->str, runtime_info_str->str);
+}
+
+/* Show command-line usage */
+static void
+print_usage(FILE *output)
+{
     fprintf(output, "\n");
     fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
     fprintf(output, "\n");
@@ -98,6 +112,8 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
     gchar  *err_info;
     struct wtap_pkthdr phdr;
 
+    memset(&phdr, 0, sizeof(struct wtap_pkthdr));
+
     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
                 frame->offset);
 
@@ -135,9 +151,9 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
 }
 
 /* Comparing timestamps between 2 frames.
-   -1 if (t1 < t2)
-   0  if (t1 == t2)
-    if (t1 > t2)
+   negative if (t1 < t2)
+   zero     if (t1 == t2)
+   positive if (t1 > t2)
 */
 static int
 frames_compare(gconstpointer a, gconstpointer b)
@@ -148,32 +164,43 @@ frames_compare(gconstpointer a, gconstpointer b)
     const nstime_t *time1 = &frame1->time;
     const nstime_t *time2 = &frame2->time;
 
-    if (time1->secs > time2->secs)
-        return 1;
-    if (time1->secs < time2->secs)
-        return -1;
-
-    /* time1->secs == time2->secs */
-    if (time1->nsecs > time2->nsecs)
-        return 1;
-    if (time1->nsecs < time2->nsecs)
-        return -1;
-
-    /* time1->nsecs == time2->nsecs */
+    return nstime_cmp(time1, time2);
+}
 
-    if (frame1->num > frame2->num)
-        return 1;
-    if (frame1->num < frame2->num)
-        return -1;
-    return 0;
+static void
+get_reordercap_compiled_info(GString *str)
+{
+    /* LIBZ */
+    g_string_append(str, ", ");
+#ifdef HAVE_LIBZ
+    g_string_append(str, "with libz ");
+#ifdef ZLIB_VERSION
+    g_string_append(str, ZLIB_VERSION);
+#else /* ZLIB_VERSION */
+    g_string_append(str, "(version unknown)");
+#endif /* ZLIB_VERSION */
+#else /* HAVE_LIBZ */
+    g_string_append(str, "without libz");
+#endif /* HAVE_LIBZ */
 }
 
+static void
+get_reordercap_runtime_info(GString *str)
+{
+    /* zlib */
+#if defined(HAVE_LIBZ) && !defined(_WIN32)
+    g_string_append_printf(str, ", with libz %s", zlibVersion());
+#endif
+}
 
 /********************************************************************/
 /* Main function.                                                   */
 /********************************************************************/
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
 {
+    GString *comp_info_str;
+    GString *runtime_info_str;
     wtap *wth = NULL;
     wtap_dumper *pdh = NULL;
     Buffer buf;
@@ -191,21 +218,51 @@ int main(int argc, char *argv[])
     FrameRecord_t *prevFrame = NULL;
 
     int opt;
+    static const struct option long_options[] = {
+        {(char *)"help", no_argument, NULL, 'h'},
+        {(char *)"version", no_argument, NULL, 'v'},
+        {0, 0, 0, 0 }
+    };
     int file_count;
     char *infile;
     char *outfile;
 
+    /* Assemble the compile-time version information string */
+    comp_info_str = g_string_new("Compiled ");
+    get_compiled_version_info(comp_info_str, NULL, get_reordercap_compiled_info);
+
+    /* Assemble the run-time version information string */
+    runtime_info_str = g_string_new("Running ");
+    get_runtime_version_info(runtime_info_str, get_reordercap_runtime_info);
+
+    /* Add it to the information to be reported on a crash. */
+    ws_add_crash_info("Reordercap (Wireshark) %s\n"
+         "\n"
+         "%s"
+         "\n"
+         "%s",
+      get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
+
     /* Process the options first */
-    while ((opt = getopt(argc, argv, "hn")) != -1) {
+    while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) {
         switch (opt) {
             case 'n':
                 write_output_regardless = FALSE;
                 break;
             case 'h':
-                usage(FALSE);
+                printf("Reordercap (Wireshark) %s\n"
+                       "Reorder timestamps of input file frames into output file.\n"
+                       "See http://www.wireshark.org for more information.\n",
+                       get_ws_vcs_version_info());
+                print_usage(stdout);
+                exit(0);
+            case 'v':
+                show_version(comp_info_str, runtime_info_str);
+                g_string_free(comp_info_str, TRUE);
+                g_string_free(runtime_info_str, TRUE);
                 exit(0);
             case '?':
-                usage(TRUE);
+                print_usage(stderr);
                 exit(1);
         }
     }
@@ -217,13 +274,13 @@ int main(int argc, char *argv[])
         outfile = argv[optind+1];
     }
     else {
-        usage(TRUE);
+        print_usage(stderr);
         exit(1);
     }
 
-    init_open_routines();
-
     /* Open infile */
+    /* TODO: if reordercap is ever changed to give the user a choice of which
+       open_routine reader to use, then the following needs to change. */
     wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
     if (wth == NULL) {
         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
@@ -267,7 +324,11 @@ int main(int argc, char *argv[])
         newFrameRecord = g_slice_new(FrameRecord_t);
         newFrameRecord->num = frames->len + 1;
         newFrameRecord->offset = data_offset;
-        newFrameRecord->time = phdr->ts;
+        if (phdr->presence_flags & WTAP_HAS_TS) {
+            newFrameRecord->time = phdr->ts;
+        } else {
+            nstime_set_unset(&newFrameRecord->time);
+        }
 
         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
            wrong_order_count++;