From Xiao Xiangquan:
[obnox/wireshark/wip.git] / mergecap.c
index 51e64464c88d5b0d5633829751c13455286c3a93..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
  *
  */
@@ -99,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");
@@ -117,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 */
+};
 
-    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 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);
+}
+
+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
@@ -161,7 +216,7 @@ main(int argc, char *argv[])
   int          i;
   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;
@@ -258,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;
@@ -351,8 +406,13 @@ main(int argc, char *argv[])
       in_file = merge_read_packet(in_file_count, in_files, &read_err,
                                   &err_info);
     if (in_file == NULL) {
-      if (read_err != 0)
-        got_read_error = TRUE;
+      /* EOF */
+      break;
+    }
+
+    if (read_err != 0) {
+      /* I/O error reading from in_file */
+      got_read_error = TRUE;
       break;
     }
 
@@ -394,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;