[Automatic update for 2017-01-29]
[metze/wireshark/wip.git] / mergecap.c
index e0e4048ccc500e74c63e0988597784e9b8538127..fd2c2838c3153e2f8209ee8d072c450b02ed8ba4 100644 (file)
 #include <getopt.h>
 #endif
 
-#ifdef HAVE_LIBZ
-#include <zlib.h>      /* to get the libz version number */
-#endif
-
 #include <string.h>
 
 #include <wiretap/wtap.h>
@@ -53,8 +49,7 @@
 #include <wsutil/file_util.h>
 #include <wsutil/privileges.h>
 #include <wsutil/strnatcmp.h>
-#include <wsutil/ws_diag_control.h>
-#include <wsutil/ws_version_info.h>
+#include <ws_version_info.h>
 
 #ifdef HAVE_PLUGINS
 #include <wsutil/plugins.h>
@@ -63,7 +58,6 @@
 #include <wsutil/report_err.h>
 
 #include <wiretap/merge.h>
-#include <wiretap/pcap-encap.h>
 
 #ifdef _WIN32
 #include <wsutil/unicode-utils.h>
@@ -135,14 +129,14 @@ string_elem_print(gpointer data, gpointer not_used _U_)
 
 #ifdef HAVE_PLUGINS
 /*
- *  Don't report failures to load plugins because most (non-wiretap) plugins
- *  *should* fail to load (because we're not linked against libwireshark and
- *  dissector plugins need libwireshark).
+ * General errors are reported with an console message in mergecap.
  */
 static void
-failure_message(const char *msg_format _U_, va_list ap _U_)
+failure_message(const char *msg_format, va_list ap)
 {
-  return;
+  fprintf(stderr, "mergecap: ");
+  vfprintf(stderr, msg_format, ap);
+  fprintf(stderr, "\n");
 }
 #endif
 
@@ -177,33 +171,6 @@ list_idb_merge_modes(void) {
   }
 }
 
-static void
-get_mergecap_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_mergecap_runtime_info(GString *str _U_)
-{
-  /* zlib */
-#if defined(HAVE_LIBZ) && !defined(_WIN32)
-  g_string_append_printf(str, ", with libz %s", zlibVersion());
-#endif
-}
-
-
 static gboolean
 merge_callback(merge_event event, int num,
                const merge_in_file_t in_files[], const guint in_file_count,
@@ -275,6 +242,7 @@ main(int argc, char *argv[])
 {
   GString            *comp_info_str;
   GString            *runtime_info_str;
+  char               *init_progfile_dir_error;
   int                 opt;
   static const struct option long_options[] = {
       {"help", no_argument, NULL, 'h'},
@@ -284,13 +252,12 @@ main(int argc, char *argv[])
   gboolean            do_append          = FALSE;
   gboolean            verbose            = FALSE;
   int                 in_file_count      = 0;
-  guint               snaplen            = 0;
+  guint32             snaplen            = 0;
 #ifdef PCAP_NG_DEFAULT
   int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */
 #else
   int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */
 #endif
-  int                 out_fd;
   int                 err                = 0;
   gchar              *err_info           = NULL;
   int                 err_fileno;
@@ -300,10 +267,6 @@ main(int argc, char *argv[])
   gboolean            use_stdout         = FALSE;
   merge_progress_callback_t cb;
 
-#ifdef HAVE_PLUGINS
-  char  *init_progfile_dir_error;
-#endif
-
   cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
 
 #ifdef _WIN32
@@ -312,10 +275,10 @@ main(int argc, char *argv[])
 #endif /* _WIN32 */
 
   /* Get the compile-time version information string */
-  comp_info_str = get_compiled_version_info(NULL, get_mergecap_compiled_info);
+  comp_info_str = get_compiled_version_info(NULL, NULL);
 
   /* Get the run-time version information string */
-  runtime_info_str = get_runtime_version_info(get_mergecap_runtime_info);
+  runtime_info_str = get_runtime_version_info(NULL);
 
   /* Add it to the information to be reported on a crash. */
   ws_add_crash_info("Mergecap (Wireshark) %s\n"
@@ -324,31 +287,41 @@ main(int argc, char *argv[])
        "\n"
        "%s",
     get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
+  g_string_free(comp_info_str, TRUE);
+  g_string_free(runtime_info_str, TRUE);
 
   /*
    * Get credential information for later use.
    */
   init_process_policies();
-  init_open_routines();
 
-#ifdef HAVE_PLUGINS
-  /* Register wiretap plugins */
-  if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
-    g_warning("mergecap: init_progfile_dir(): %s", init_progfile_dir_error);
+  /*
+   * Attempt to get the pathname of the directory containing the
+   * executable file.
+   */
+  init_progfile_dir_error = init_progfile_dir(argv[0], main);
+  if (init_progfile_dir_error != NULL) {
+    fprintf(stderr,
+            "mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
+            init_progfile_dir_error);
     g_free(init_progfile_dir_error);
-  } else {
-    /* Register all the plugin types we have. */
-    wtap_register_plugin_types(); /* Types known to libwiretap */
+  }
 
-    init_report_err(failure_message,NULL,NULL,NULL);
+  wtap_init();
 
-    /* Scan for plugins.  This does *not* call their registration routines;
-       that's done later. */
-    scan_plugins();
+#ifdef HAVE_PLUGINS
+  init_report_err(failure_message,NULL,NULL,NULL);
 
-    /* Register all libwiretap plugin modules. */
-    register_all_wiretap_modules();
-  }
+  /* Scan for plugins.  This does *not* call their registration routines;
+     that's done later.
+
+     Don't report failures to load plugins because most (non-wiretap)
+     plugins *should* fail to load (because we're not linked against
+     libwireshark and dissector plugins need libwireshark).*/
+  scan_plugins(DONT_REPORT_LOAD_FAILURE);
+
+  /* Register all libwiretap plugin modules. */
+  register_all_wiretap_modules();
 #endif
 
   /* Process the options first */
@@ -389,7 +362,7 @@ main(int argc, char *argv[])
       break;
 
     case 's':
-      snaplen = get_positive_int(optarg, "snapshot length");
+      snaplen = get_nonzero_guint32(optarg, "snapshot length");
       break;
 
     case 'v':
@@ -397,6 +370,8 @@ main(int argc, char *argv[])
       break;
 
     case 'V':
+      comp_info_str = get_compiled_version_info(NULL, NULL);
+      runtime_info_str = get_runtime_version_info(NULL);
       show_version("Mergecap (Wireshark)", comp_info_str, runtime_info_str);
       g_string_free(comp_info_str, TRUE);
       g_string_free(runtime_info_str, TRUE);
@@ -453,25 +428,21 @@ main(int argc, char *argv[])
 
   /* open the outfile */
   if (strcmp(out_filename, "-") == 0) {
-    /* use stdout as the outfile */
+    /* merge the files to the standard output */
     use_stdout = TRUE;
-    out_fd = 1 /*stdout*/;
+    status = merge_files_to_stdout(file_type,
+                                   (const char *const *) &argv[optind],
+                                   in_file_count, do_append, mode, snaplen,
+                                   "mergecap", verbose ? &cb : NULL,
+                                   &err, &err_info, &err_fileno);
   } else {
-    /* open the outfile */
-    out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
-    if (out_fd == -1) {
-      fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
-              out_filename, g_strerror(errno));
-      exit(1);
-    }
+    /* merge the files to the outfile */
+    status = merge_files(out_filename, file_type,
+                         (const char *const *) &argv[optind], in_file_count,
+                         do_append, mode, snaplen, "mergecap", verbose ? &cb : NULL,
+                         &err, &err_info, &err_fileno);
   }
 
-  /* merge the files */
-  status = merge_files(out_fd, out_filename, file_type,
-                       (const char *const *) &argv[optind], in_file_count,
-                       do_append, mode, snaplen, "mergecap", verbose ? &cb : NULL,
-                       &err, &err_info, &err_fileno);
-
   switch (status) {
     case MERGE_OK:
       break;
@@ -487,10 +458,13 @@ main(int argc, char *argv[])
       break;
 
     case MERGE_ERR_CANT_OPEN_OUTFILE:
-      fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
-                  wtap_strerror(err));
-      if (!use_stdout)
-        ws_close(out_fd);
+      if (use_stdout) {
+        fprintf(stderr, "mergecap: Can't set up the standard output: %s\n",
+                    wtap_strerror(err));
+      } else {
+        fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
+                    wtap_strerror(err));
+      }
       break;
 
     case MERGE_ERR_CANT_READ_INFILE:      /* fall through */