capinfos: Print "n/a" for rates and duration if only one packet in capture.\;
[obnox/wireshark/wip.git] / capinfos.c
index 321a19172c4c84796b9404921bfc00b51e352bfe..4f0a56dc0981ea4af5091d2fc93df9d83c8facfe 100644 (file)
@@ -49,6 +49,7 @@
 #include <epan/plugins.h>
 #include <epan/report_err.h>
 #include "wtap.h"
+#include <wsutil/privileges.h>
 
 #ifdef NEED_GETOPT_H
 #include "getopt.h"
@@ -66,6 +67,7 @@ static gboolean cap_end_time = FALSE;
 static gboolean cap_data_rate_byte = FALSE;
 static gboolean cap_data_rate_bit = FALSE;
 static gboolean cap_packet_size = FALSE;
+static gboolean cap_packet_rate = FALSE;
 
 
 typedef struct _capture_info {
@@ -94,6 +96,13 @@ secs_nsecs(const struct wtap_nstime * nstime)
   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
 }
 
+static void print_value(gchar *text_p1, gint width, gchar *text_p2, double value) {
+  if (value > 0.0)
+    printf("%s%.*f%s\n", text_p1, width, value, text_p2);
+  else
+    printf("%sn/a\n", text_p1);
+}
+
 static void
 print_stats(capture_info *cf_info)
 {
@@ -107,17 +116,18 @@ print_stats(capture_info *cf_info)
   start_time_t = (time_t)cf_info->start_time;
   stop_time_t = (time_t)cf_info->stop_time;
 
-  if (cap_file_type) printf("File type: %s\n", file_type_string);
-  if (cap_file_encap) printf("File encapsulation: %s\n", file_encap_string);
-  if (cap_packet_count) printf("Number of packets: %u \n", cf_info->packet_count);
-  if (cap_file_size) printf("File size: %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
-  if (cap_data_size) printf("Data size: %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
-  if (cap_duration) printf("Capture duration: %f seconds\n", cf_info->duration);
-  if (cap_start_time) printf("Start time: %s", (cf_info->packet_count>0) ? ctime (&start_time_t) : "n/a\n");
-  if (cap_end_time) printf("End time: %s",     (cf_info->packet_count>0) ? ctime (&stop_time_t)  : "n/a\n");
-  if (cap_data_rate_byte) printf("Data rate: %.2f bytes/s\n", cf_info->data_rate);
-  if (cap_data_rate_bit) printf("Data rate: %.2f bits/s\n", cf_info->data_rate*8);
-  if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
+  if (cap_file_type)      printf     ("File type:           %s\n", file_type_string);
+  if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
+  if (cap_packet_count)   printf     ("Number of packets:   %u\n", cf_info->packet_count);
+  if (cap_file_size)      printf     ("File size:           %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
+  if (cap_data_size)      printf     ("Data size:           %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
+  if (cap_duration)       print_value("Capture duration:    ", 0, " seconds",   cf_info->duration); 
+  if (cap_start_time)     printf     ("Start time:          %s", (cf_info->packet_count>0) ? ctime (&start_time_t) : "n/a\n");
+  if (cap_end_time)       printf     ("End time:            %s", (cf_info->packet_count>0) ? ctime (&stop_time_t)  : "n/a\n");
+  if (cap_data_rate_byte) print_value("Data byte rate:      ", 2, " bytes/s",   cf_info->data_rate);
+  if (cap_data_rate_bit)  print_value("Data bit rate:       ", 2, " bits/s",    cf_info->data_rate*8);
+  if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",      cf_info->packet_size);
+  if (cap_packet_rate)    print_value("Average packet rate: ", 2, " packets/s", cf_info->packet_rate);
 }
 
 static int
@@ -131,7 +141,7 @@ process_cap_file(wtap *wth, const char *filename)
   guint32              packet = 0;
   gint64               bytes = 0;
   const struct wtap_pkthdr *phdr;
-  capture_info  cf_info;
+  capture_info         cf_info;
   double               start_time = 0;
   double               stop_time = 0;
   double               cur_time = 0;
@@ -164,6 +174,7 @@ process_cap_file(wtap *wth, const char *filename)
     case WTAP_ERR_UNSUPPORTED_ENCAP:
     case WTAP_ERR_BAD_RECORD:
       fprintf(stderr, "(%s)\n", err_info);
+      g_free(err_info);
       break;
     }
     return 1;
@@ -197,22 +208,26 @@ process_cap_file(wtap *wth, const char *filename)
   /* Number of packet bytes */
   cf_info.packet_bytes = bytes;
 
+  cf_info.data_rate   = 0.0;
+  cf_info.packet_rate = 0.0;
+  cf_info.packet_size = 0.0;
+
   if (packet > 0) {
-    cf_info.data_rate   = (double)bytes / (stop_time-start_time);  /* Data rate per second */
+    if (cf_info.duration > 0.0) { 
+      cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
+      cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
+    }
     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
   }
-  else {
-    cf_info.data_rate   = 0.0;
-    cf_info.packet_size = 0.0;
-  }
 
-  printf("File name: %s\n", filename);
+  printf("File name:           %s\n", filename);
   print_stats(&cf_info);
 
   return 0;
 }
 
-static void usage(gboolean is_error)
+static void
+usage(gboolean is_error)
 {
   FILE *output;
 
@@ -252,6 +267,7 @@ static void usage(gboolean is_error)
   fprintf(output, "  -y display average data rate (in bytes/s)\n");
   fprintf(output, "  -i display average data rate (in bits/s)\n");
   fprintf(output, "  -z display average packet size (in bytes)\n");
+  fprintf(output, "  -x display average packet rate (in packets/s)\n");
   fprintf(output, "\n");
   fprintf(output, "Miscellaneous:\n");
   fprintf(output, "  -h display this help and exit\n");
@@ -259,44 +275,53 @@ static void usage(gboolean is_error)
   fprintf(output, "If no options are given, default is to display all infos\n");
 }
 
+#ifdef HAVE_PLUGINS
 /*
- * Errors are reported with a console message.
+ *  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).
  */
 static void
-failure_message(const char *msg_format, va_list ap)
+failure_message(const char *msg_format _U_, va_list ap _U_)
 {
-       fprintf(stderr, "capinfos: ");
-       vfprintf(stderr, msg_format, ap);
-       fprintf(stderr, "\n");
+       return;
 }
+#endif
 
 
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
 {
   wtap *wth;
   int err;
   gchar *err_info;
-  extern char *optarg;
   extern int optind;
   int opt;
   int status = 0;
 #ifdef HAVE_PLUGINS
   char* init_progfile_dir_error;
+#endif
+
+  /*
+   * Get credential information for later use.
+   */
+  get_credential_info();
 
+#ifdef HAVE_PLUGINS
   /* Register wiretap plugins */
 
-    if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
+    if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
                g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
                g_free(init_progfile_dir_error);
     } else {
-               init_report_err(failure_message,NULL,NULL);
+               init_report_err(failure_message,NULL,NULL,NULL);
                init_plugins();
     }
 #endif
 
   /* Process the options */
 
-  while ((opt = getopt(argc, argv, "tEcsduaeyizvh")) !=-1) {
+  while ((opt = getopt(argc, argv, "tEcsduaeyizvhx")) !=-1) {
 
     switch (opt) {
 
@@ -344,6 +369,10 @@ int main(int argc, char *argv[])
       cap_packet_size = TRUE;
       break;
 
+    case 'x':
+      cap_packet_rate = TRUE;
+      break;
+
     case 'h':
       usage(FALSE);
       exit(1);
@@ -371,6 +400,7 @@ int main(int argc, char *argv[])
     cap_data_rate_byte = TRUE;
     cap_data_rate_bit = TRUE;
     cap_packet_size = TRUE;
+    cap_packet_rate = TRUE;
   }
 
   if ((argc - optind) < 1) {