From Irene Ruengeler: Whitespace changes.
[obnox/wireshark/wip.git] / mergecap.c
index 70b4b58ff76d67cb1271ebd8a8257dff550437c7..db3873ef869889cacfcec6f998b3967219956baf 100644 (file)
@@ -2,7 +2,25 @@
  *
  * $Id$
  *
- * Written by Scott Renfro <scott@renfro.org> based on
+ * 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 modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Mergecap written by Scott Renfro <scott@renfro.org> based on
  * editcap by Richard Sharpe and Guy Harris
  *
  */
@@ -27,9 +45,7 @@
 #include <string.h>
 #include "wtap.h"
 
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#else
+#ifndef HAVE_GETOPT
 #include "wsutil/wsgetopt.h"
 #endif
 
@@ -101,14 +117,14 @@ usage(void)
   fprintf(stderr, "Merge two or more capture files into one.\n");
   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
   fprintf(stderr, "\n");
-  fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> ...\n");
+  fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
   fprintf(stderr, "\n");
   fprintf(stderr, "Output:\n");
   fprintf(stderr, "  -a                concatenate rather than merge files.\n");
   fprintf(stderr, "                    default is to merge based on frame timestamps.\n");
   fprintf(stderr, "  -s <snaplen>      truncate packets to <snaplen> bytes of data.\n");
   fprintf(stderr, "  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.\n");
-  fprintf(stderr, "  -F <capture type> set the output file type; default is libpcap.\n");
+  fprintf(stderr, "  -F <capture type> set the output file type; default is pcapng.\n");
   fprintf(stderr, "                    an empty \"-F\" option will list the file types.\n");
   fprintf(stderr, "  -T <encap type>   set the output file encapsulation type;\n");
   fprintf(stderr, "                    default is the same as the first input file.\n");
@@ -119,28 +135,65 @@ usage(void)
   fprintf(stderr, "  -v                verbose output.\n");
 }
 
-static void list_capture_types(void) {
-    int i;
+struct string_elem {
+    const char *sstr;   /* The short string */
+    const char *lstr;   /* The long string */
+};
+
+static gint
+string_compare(gconstpointer a, gconstpointer b)
+{
+    return strcmp(((const struct string_elem *)a)->sstr,
+        ((const struct string_elem *)b)->sstr);
+}
+
+static void
+string_elem_print(gpointer data, gpointer not_used _U_)
+{
+    fprintf(stderr, "    %s - %s\n",
+        ((struct string_elem *)data)->sstr,
+        ((struct string_elem *)data)->lstr);
+}
 
-    fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
-    for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
-      if (wtap_dump_can_open(i))
-        fprintf(stderr, "    %s - %s\n",
-          wtap_file_type_short_string(i), wtap_file_type_string(i));
+static void
+list_capture_types(void) {
+  int i;
+  struct string_elem *captypes;
+  GSList *list = NULL;
+
+  captypes = g_malloc(sizeof(struct string_elem) * WTAP_NUM_FILE_TYPES);
+
+  fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
+  for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
+    if (wtap_dump_can_open(i)) {
+      captypes[i].sstr = wtap_file_type_short_string(i);
+      captypes[i].lstr = wtap_file_type_string(i);
+      list = g_slist_insert_sorted(list, &captypes[i], string_compare);
     }
+  }
+  g_slist_foreach(list, string_elem_print, NULL);
+  g_slist_free(list);
+  g_free(captypes);
 }
 
-static void list_encap_types(void) {
+static void
+list_encap_types(void) {
     int i;
-    const char *string;
+    struct string_elem *encaps;
+    GSList *list = NULL;
 
-    fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
+    encaps = g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
+    fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
-        string = wtap_encap_short_string(i);
-        if (string != NULL)
-          fprintf(stderr, "    %s - %s\n",
-            string, wtap_encap_string(i));
+        encaps[i].sstr = wtap_encap_short_string(i);
+        if (encaps[i].sstr != NULL) {
+            encaps[i].lstr = wtap_encap_string(i);
+            list = g_slist_insert_sorted(list, &encaps[i], string_compare);
+        }
     }
+    g_slist_foreach(list, string_elem_print, NULL);
+    g_slist_free(list);
+    g_free(encaps);
 }
 
 int
@@ -159,12 +212,11 @@ main(int argc, char *argv[])
 #endif
   int          frame_type = -2;
   int          out_fd;
-  merge_in_file_t   *in_files      = NULL;
+  merge_in_file_t   *in_files      = NULL, *in_file;
   int          i;
-  wtap        *wth;
   struct wtap_pkthdr *phdr, snap_phdr;
   wtap_dumper *pdh;
-  int          open_err, read_err, write_err, close_err;
+  int          open_err, read_err=0, write_err, close_err;
   gchar       *err_info;
   int          err_fileno;
   char        *out_filename = NULL;
@@ -261,7 +313,7 @@ main(int argc, char *argv[])
 
     case WTAP_ERR_UNSUPPORTED:
     case WTAP_ERR_UNSUPPORTED_ENCAP:
-    case WTAP_ERR_BAD_RECORD:
+    case WTAP_ERR_BAD_FILE:
       fprintf(stderr, "(%s)\n", err_info);
       g_free(err_info);
       break;
@@ -348,14 +400,19 @@ main(int argc, char *argv[])
   count = 1;
   for (;;) {
     if (do_append)
-      wth = merge_append_read_packet(in_file_count, in_files, &read_err,
-                                     &err_info);
+      in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
+                                         &err_info);
     else
-      wth = merge_read_packet(in_file_count, in_files, &read_err,
-                              &err_info);
-    if (wth == NULL) {
-      if (read_err != 0)
-        got_read_error = TRUE;
+      in_file = merge_read_packet(in_file_count, in_files, &read_err,
+                                  &err_info);
+    if (in_file == NULL) {
+      /* EOF */
+      break;
+    }
+
+    if (read_err != 0) {
+      /* I/O error reading from in_file */
+      got_read_error = TRUE;
       break;
     }
 
@@ -364,15 +421,15 @@ main(int argc, char *argv[])
 
     /* We simply write it, perhaps after truncating it; we could do other
      * things, like modify it. */
-    phdr = wtap_phdr(wth);
+    phdr = wtap_phdr(in_file->wth);
     if (snaplen != 0 && phdr->caplen > snaplen) {
       snap_phdr = *phdr;
       snap_phdr.caplen = snaplen;
       phdr = &snap_phdr;
     }
 
-    if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
-         wtap_buf_ptr(wth), &write_err)) {
+    if (!wtap_dump(pdh, phdr, wtap_pseudoheader(in_file->wth),
+         wtap_buf_ptr(in_file->wth), &write_err)) {
       got_write_error = TRUE;
       break;
     }
@@ -397,7 +454,7 @@ main(int argc, char *argv[])
 
         case WTAP_ERR_UNSUPPORTED:
         case WTAP_ERR_UNSUPPORTED_ENCAP:
-        case WTAP_ERR_BAD_RECORD:
+        case WTAP_ERR_BAD_FILE:
           fprintf(stderr, "(%s)\n", err_info);
           g_free(err_info);
           break;
@@ -407,8 +464,22 @@ main(int argc, char *argv[])
   }
 
   if (got_write_error) {
-    fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
-            wtap_strerror(write_err));
+    switch (write_err) {
+
+    case WTAP_ERR_UNSUPPORTED_ENCAP:
+      /*
+       * This is a problem with the particular frame we're writing;
+       * note that, and give the frame number.
+       */
+      fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
+              in_file->packet_num, in_file->filename);
+      break;
+
+    default:
+      fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
+              wtap_strerror(write_err));
+      break;
+    }
   }
 
   g_free(in_files);