epl: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / reordercap.c
index 812825c6d04609f0f641a9ce3f4f6bd41b4315d1..ec7d4ce8efc6304cb2ce224a0111722e5761e80e 100644 (file)
 #include <wsutil/plugins.h>
 #endif
 
-#include <wsutil/report_err.h>
+#include <wsutil/report_message.h>
+
+#include "ui/failure_message.h"
+
+#define INVALID_OPTION 1
+#define OPEN_ERROR 2
+#define OUTPUT_FILE_ERROR 1
 
 /* Show command-line usage */
 static void
@@ -88,7 +94,8 @@ typedef struct FrameRecord_t {
 
 static void
 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
-            struct wtap_pkthdr *phdr, Buffer *buf, const char *infile)
+            struct wtap_pkthdr *phdr, Buffer *buf, const char *infile,
+            const char *outfile)
 {
     int    err;
     gchar  *err_info;
@@ -102,12 +109,9 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
         if (err != 0) {
             /* Print a message noting that the read failed somewhere along the line. */
             fprintf(stderr,
-                    "reordercap: An error occurred while re-reading \"%s\": %s.\n",
-                    infile, wtap_strerror(err));
-            if (err_info != NULL) {
-                fprintf(stderr, "(%s)\n", err_info);
-                g_free(err_info);
-            }
+                    "reordercap: An error occurred while re-reading \"%s\".\n",
+                    infile);
+            cfile_read_failure_message("reordercap", infile, err, err_info);
             exit(1);
         }
     }
@@ -119,12 +123,9 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
 
     /* Dump frame to outfile */
     if (!wtap_dump(pdh, phdr, ws_buffer_start_ptr(buf), &err, &err_info)) {
-        fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
-                wtap_strerror(err));
-        if (err_info != NULL) {
-            fprintf(stderr, "(%s)\n", err_info);
-            g_free(err_info);
-        }
+        cfile_write_failure_message("reordercap", infile, outfile, err,
+                                    err_info, frame->num,
+                                    wtap_file_type_subtype(wth));
         exit(1);
     }
 }
@@ -148,10 +149,11 @@ frames_compare(gconstpointer a, gconstpointer b)
 
 #ifdef HAVE_PLUGINS
 /*
- * General errors are reported with an console message in reordercap.
+ * General errors and warnings are reported with an console message
+ * in reordercap.
  */
 static void
-failure_message(const char *msg_format, va_list ap)
+failure_warning_message(const char *msg_format, va_list ap)
 {
     fprintf(stderr, "reordercap: ");
     vfprintf(stderr, msg_format, ap);
@@ -167,6 +169,7 @@ main(int argc, char *argv[])
 {
     GString *comp_info_str;
     GString *runtime_info_str;
+    char *init_progfile_dir_error;
     wtap *wth = NULL;
     wtap_dumper *pdh = NULL;
     struct wtap_pkthdr dump_phdr;
@@ -181,6 +184,7 @@ main(int argc, char *argv[])
     GArray                      *shb_hdrs = NULL;
     wtapng_iface_descriptions_t *idb_inf = NULL;
     GArray                      *nrb_hdrs = NULL;
+    int                          ret = EXIT_SUCCESS;
 
     GPtrArray *frames;
     FrameRecord_t *prevFrame = NULL;
@@ -195,10 +199,6 @@ main(int argc, char *argv[])
     char *infile;
     const char *outfile;
 
-#ifdef HAVE_PLUGINS
-    char  *init_progfile_dir_error;
-#endif
-
     /* Get the compile-time version information string */
     comp_info_str = get_compiled_version_info(NULL, NULL);
 
@@ -215,34 +215,40 @@ main(int argc, char *argv[])
     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();
+    /*
+     * Get credential information for later use.
+     */
+    init_process_policies();
+
+    /*
+     * 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,
+                "reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
+                init_progfile_dir_error);
+        g_free(init_progfile_dir_error);
+    }
+
+    wtap_init();
 
 #ifdef HAVE_PLUGINS
     /* Register wiretap plugins */
-    if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
-        g_warning("reordercap: init_progfile_dir(): %s", 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);
+    init_report_message(failure_warning_message, failure_warning_message,
+                        NULL, NULL, NULL);
 
-        /* Scan for plugins.  This does *not* call their registration routines;
-           that's done later.
+    /* 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);
+       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();
-    }
+    /* Register all libwiretap plugin modules. */
+    register_all_wiretap_modules();
 #endif
 
     /* Process the options first */
@@ -257,17 +263,18 @@ main(int argc, char *argv[])
                        "See https://www.wireshark.org for more information.\n",
                        get_ws_vcs_version_info());
                 print_usage(stdout);
-                exit(0);
+                goto clean_exit;
             case 'v':
                 comp_info_str = get_compiled_version_info(NULL, NULL);
                 runtime_info_str = get_runtime_version_info(NULL);
                 show_version("Reordercap (Wireshark)", comp_info_str, runtime_info_str);
                 g_string_free(comp_info_str, TRUE);
                 g_string_free(runtime_info_str, TRUE);
-                exit(0);
+                goto clean_exit;
             case '?':
                 print_usage(stderr);
-                exit(1);
+                ret = INVALID_OPTION;
+                goto clean_exit;
         }
     }
 
@@ -279,7 +286,8 @@ main(int argc, char *argv[])
     }
     else {
         print_usage(stderr);
-        exit(1);
+        ret = INVALID_OPTION;
+        goto clean_exit;
     }
 
     /* Open infile */
@@ -287,13 +295,9 @@ main(int argc, char *argv[])
        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,
-                wtap_strerror(err));
-        if (err_info != NULL) {
-            fprintf(stderr, "(%s)\n", err_info);
-            g_free(err_info);
-        }
-        exit(1);
+        cfile_open_failure_message("reordercap", infile, err, err_info);
+        ret = OPEN_ERROR;
+        goto clean_exit;
     }
     DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth));
 
@@ -304,21 +308,21 @@ main(int argc, char *argv[])
     /* Open outfile (same filetype/encap as input file) */
     if (strcmp(outfile, "-") == 0) {
       pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth),
-                                     65535, FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
-      outfile = "standard output";
+                                     wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
     } else {
       pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
-                              65535, FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
+                              wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
     }
     g_free(idb_inf);
     idb_inf = NULL;
 
     if (pdh == NULL) {
-        fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
-                outfile, wtap_strerror(err));
+        cfile_dump_open_failure_message("reordercap", outfile, err,
+                                        wtap_file_type_subtype(wth));
         wtap_block_array_free(shb_hdrs);
         wtap_block_array_free(nrb_hdrs);
-        exit(1);
+        ret = OUTPUT_FILE_ERROR;
+        goto clean_exit;
     }
 
     /* Allocate the array of frame pointers. */
@@ -348,13 +352,7 @@ main(int argc, char *argv[])
     }
     if (err != 0) {
       /* Print a message noting that the read failed somewhere along the line. */
-      fprintf(stderr,
-              "reordercap: An error occurred while reading \"%s\": %s.\n",
-              infile, wtap_strerror(err));
-      if (err_info != NULL) {
-          fprintf(stderr, "(%s)\n", err_info);
-          g_free(err_info);
-      }
+      cfile_read_failure_message("reordercap", infile, err, err_info);
     }
 
     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
@@ -372,7 +370,7 @@ main(int argc, char *argv[])
 
         /* Avoid writing if already sorted and configured to */
         if (write_output_regardless || (wrong_order_count > 0)) {
-            frame_write(frame, wth, pdh, &dump_phdr, &buf, infile);
+            frame_write(frame, wth, pdh, &dump_phdr, &buf, infile, outfile);
         }
         g_slice_free(FrameRecord_t, frame);
     }
@@ -380,7 +378,7 @@ main(int argc, char *argv[])
     ws_buffer_free(&buf);
 
     if (!write_output_regardless && (wrong_order_count == 0)) {
-        printf("Not writing output file because input file is already in order!\n");
+        printf("Not writing output file because input file is already in order.\n");
     }
 
     /* Free the whole array */
@@ -388,11 +386,11 @@ main(int argc, char *argv[])
 
     /* Close outfile */
     if (!wtap_dump_close(pdh, &err)) {
-        fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
-                wtap_strerror(err));
+        cfile_close_failure_message(outfile, err);
         wtap_block_array_free(shb_hdrs);
         wtap_block_array_free(nrb_hdrs);
-        exit(1);
+        ret = OUTPUT_FILE_ERROR;
+        goto clean_exit;
     }
     wtap_block_array_free(shb_hdrs);
     wtap_block_array_free(nrb_hdrs);
@@ -400,7 +398,13 @@ main(int argc, char *argv[])
     /* Finally, close infile and release resources. */
     wtap_close(wth);
 
-    return 0;
+clean_exit:
+    wtap_cleanup();
+    free_progdirs();
+#ifdef HAVE_PLUGINS
+    plugins_cleanup();
+#endif
+    return ret;
 }
 
 /*