Follow up to rev 34073: Since "-b files:0" is no longer necessary to
[obnox/wireshark/wip.git] / mergecap.c
index 9136e64930b6a249810709ba2a08e8f283b336cb..05bb517666d8090f66ad0d802e1e2bae64c13562 100644 (file)
 #include <string.h>
 #include "wtap.h"
 
-#ifdef NEED_GETOPT_H
-#include "getopt.h"
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+#include "wsutil/wsgetopt.h"
 #endif
 
 #include "svnversion.h"
 #include "merge.h"
-
-#ifdef HAVE_IO_H
-# include <io.h>
-#endif
+#include "wsutil/file_util.h"
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
-/* Win32 needs the O_BINARY flag for open() */
-#ifndef O_BINARY
-#define O_BINARY       0
-#endif
-
 static int
 get_natural_int(const char *string, const char *name)
 {
-  long number;
+  int number;
   char *p;
 
-  number = strtol(string, &p, 10);
+  number = (int) strtol(string, &p, 10);
   if (p == string || *p != '\0') {
     fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
            name, string);
@@ -75,7 +69,7 @@ get_natural_int(const char *string, const char *name)
 static int
 get_positive_int(const char *string, const char *name)
 {
-  long number;
+  int number;
 
   number = get_natural_int(string, name);
 
@@ -94,39 +88,60 @@ get_positive_int(const char *string, const char *name)
 static void
 usage(void)
 {
-  int i;
-  const char *string;
-
-  printf("Usage: mergecap [-hva] [-s <snaplen>] [-T <encap type>]\n");
-  printf("          [-F <capture type>] -w <outfile> <infile> [...]\n\n");
-  printf("  where\t-h produces this help listing.\n");
-  printf("       \t-v verbose operation, default is silent\n");
-  printf("       \t-a files should be concatenated, not merged\n");
-  printf("       \t     Default merges based on frame timestamps\n");
-  printf("       \t-s <snaplen>: truncate packets to <snaplen> bytes of data\n");
-  printf("       \t-w <outfile>: sets output filename to <outfile>\n");
-  printf("       \t-T <encap type> encapsulation type to use:\n");
-  for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
-      string = wtap_encap_short_string(i);
-      if (string != NULL)
-        printf("       \t     %s - %s\n",
-          string, wtap_encap_string(i));
-  }
-  printf("       \t     default is the same as the first input file\n");
-  printf("       \t-F <capture type> capture file type to write:\n");
-  for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
-    if (wtap_dump_can_open(i))
-      printf("       \t     %s - %s\n",
-        wtap_file_type_short_string(i), wtap_file_type_string(i));
-  }
-  printf("       \t     default is libpcap\n");
+
+  fprintf(stderr, "Mergecap %s"
+#ifdef SVNVERSION
+         " (" SVNVERSION " from " SVNPATH ")"
+#endif
+         "\n", VERSION);
+  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, "\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, "                    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");
+  fprintf(stderr, "                    an empty \"-T\" option will list the encapsulation types.\n");
+  fprintf(stderr, "\n");
+  fprintf(stderr, "Miscellaneous:\n");
+  fprintf(stderr, "  -h                display this help and exit.\n");
+  fprintf(stderr, "  -v                verbose output.\n");
+}
+
+static void list_capture_types(void) {
+    int i;
+
+    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_encap_types(void) {
+    int i;
+    const char *string;
+
+    fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\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));
+    }
 }
 
 int
 main(int argc, char *argv[])
 {
-  extern char *optarg;
-  extern int   optind;
   int          opt;
   gboolean     do_append     = FALSE;
   gboolean     verbose       = FALSE;
@@ -164,6 +179,7 @@ main(int argc, char *argv[])
       if (frame_type < 0) {
        fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
            optarg);
+        list_encap_types();
        exit(1);
       }
       break;
@@ -173,6 +189,7 @@ main(int argc, char *argv[])
       if (file_type < 0) {
        fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
            optarg);
+        list_capture_types();
        exit(1);
       }
       break;
@@ -186,18 +203,22 @@ main(int argc, char *argv[])
       break;
 
     case 'h':
-      printf("mergecap version %s"
-#ifdef SVNVERSION
-         " (" SVNVERSION ")"
-#endif
-         "\n", VERSION);
       usage();
       exit(0);
       break;
 
     case '?':              /* Bad options if GNU getopt */
-      usage();
-      return 1;
+      switch(optopt) {
+      case'F':
+        list_capture_types();
+        break;
+      case'T':
+        list_encap_types();
+        break;
+      default:
+        usage();
+      }
+      exit(1);
       break;
 
     }
@@ -287,24 +308,24 @@ main(int argc, char *argv[])
   }
 
   /* open the outfile */
-  if (strncmp(out_filename, "-", 2) == 0) {  
+  if (strncmp(out_filename, "-", 2) == 0) {
     /* use stdout as the outfile */
     out_fd = 1 /*stdout*/;
   } else {
     /* open the outfile */
-    out_fd = open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+    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, strerror(errno));
       exit(1);
     }
-  }  
-    
+  }
+
   /* prepare the outfile */
   pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
   if (pdh == NULL) {
     merge_close_in_files(in_file_count, in_files);
-    free(in_files);
+    g_free(in_files);
     fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
             wtap_strerror(open_err));
     exit(1);
@@ -377,7 +398,7 @@ main(int argc, char *argv[])
             wtap_strerror(write_err));
   }
 
-  free(in_files);
+  g_free(in_files);
 
   return (!got_read_error && !got_write_error) ? 0 : 2;
 }