Add remark about including packet.h before tap.h
[obnox/wireshark/wip.git] / capinfos.c
index f4ccf307d4442bb318e1907267a90d84cf4bb739..da32dceb74b0067489678f2aa200cb3edc35293d 100644 (file)
  *
  * New capinfos features
  *
- * Continue processing additional files after 
- * a wiretap open failure.  The new -C option 
- * reverts to capinfos' original behavior which 
- * is to cancels any further file processing at 
+ * Continue processing additional files after
+ * a wiretap open failure.  The new -C option
+ * reverts to capinfos' original behavior which
+ * is to cancels any further file processing at
  * first file open failure.
  *
- * Change the behavior of how the default display 
- * of all infos is initiated.  This gets rid of a 
+ * Change the behavior of how the default display
+ * of all infos is initiated.  This gets rid of a
  * special post getopt() argument count test.
  *
  * Add new table output format (with related options)
- * This feature allows outputting the various infos 
- * into a tab delimited text file, or to a comma 
+ * This feature allows outputting the various infos
+ * into a tab delimited text file, or to a comma
  * separated variables file (*.csv) instead of the
  * original "long" format.
  */
 #include "wsgetopt.h"
 #endif
 
+#include "svnversion.h"
+
 /*
  * By default capinfos now continues processing
- * the next filename if and when wiretap detects 
- * a problem opening a file.  
+ * the next filename if and when wiretap detects
+ * a problem opening a file.
  * Use the '-C' option to revert back to original
- * capinfos behavior which is to abort any 
- * additional file processing at first open file 
+ * capinfos behavior which is to abort any
+ * additional file processing at first open file
  * failure.
  */
 
@@ -107,13 +109,13 @@ static gchar field_separator = '\t';        /* Use TAB as field separator by def
 static gchar quote_char = '\0';             /* Do NOT quote fields by default        */
 
 /*
- * capinfos has the ability to report on a number of 
- * various characteristics ("infos") for each input file.  
- * 
- * By default reporting of all info fields is enabled. 
+ * capinfos has the ability to report on a number of
+ * various characteristics ("infos") for each input file.
+ *
+ * By default reporting of all info fields is enabled.
  *
- * Optionally the reporting of any specific info field 
- * or combination of info fields can be enabled with 
+ * Optionally the reporting of any specific info field
+ * or combination of info fields can be enabled with
  * individual options.
  */
 
@@ -128,6 +130,7 @@ static gboolean cap_data_size = TRUE;       /* Report packet byte size    */
 static gboolean cap_duration = TRUE;        /* Report capture duration    */
 static gboolean cap_start_time = TRUE;      /* Report capture start time  */
 static gboolean cap_end_time = TRUE;        /* Report capture end time    */
+static gboolean time_as_secs = FALSE;       /* Report time values as raw seconds */
 
 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
 static gboolean cap_data_rate_bit = TRUE;   /* Report data rate bites/sec */
@@ -229,8 +232,8 @@ disable_all_infos(void)
 /*
  * ctime_no_lf()
  *
- * This function simply truncates the string returned 
- * from the ctime() function to remove the trailing 
+ * This function simply truncates the string returned
+ * from the ctime() function to remove the trailing
  * '\n' character.
  *
  * The ctime() function returns a string formatted as:
@@ -244,7 +247,29 @@ ctime_no_lf(const time_t* timer)
   gchar *time_string;
   time_string = ctime(timer);
   time_string[24] = '\0';
-  return(time_string);  
+  return(time_string);
+}
+
+static gchar *
+time_string(const time_t *timer, capture_info *cf_info, gboolean want_lf)
+{
+  const gchar *lf = want_lf ? "\n" : "";
+  static gchar time_string_buf[15];
+
+  if (cf_info->packet_count > 0) {
+    if (time_as_secs) {
+      /* XXX - Would it be useful to show sub-second precision? */
+      g_snprintf(time_string_buf, 15, "%lu%s", (unsigned long) *timer, lf);
+      return time_string_buf;
+    } else if (want_lf) {
+      return ctime(timer);
+    } else {
+      return ctime_no_lf(timer);
+    }
+  }
+
+  g_snprintf(time_string_buf, 15, "n/a%s", lf);
+  return time_string_buf;
 }
 
 static double
@@ -253,7 +278,7 @@ 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) {
+static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
   if (value > 0.0)
     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
   else
@@ -279,9 +304,9 @@ print_stats(const gchar *filename, capture_info *cf_info)
   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_duration)       print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
+  if (cap_start_time)     printf     ("Start time:          %s", time_string(&start_time_t, cf_info, TRUE));
+  if (cap_end_time)       printf     ("End time:            %s", time_string(&stop_time_t, cf_info, TRUE));
   if (cap_data_rate_byte) print_value("Data byte rate:      ", 2, " bytes/sec",   cf_info->data_rate);
   if (cap_data_rate_bit)  print_value("Data bit rate:       ", 2, " bits/sec",    cf_info->data_rate*8);
   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
@@ -308,7 +333,7 @@ putquote(void)
 }
 
 static void
-print_stats_table_header_label(gchar *label)
+print_stats_table_header_label(const gchar *label)
 {
   putsep();
   putquote();
@@ -336,7 +361,7 @@ print_stats_table_header(void)
   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
 #ifdef HAVE_LIBGCRYPT
-  if (cap_packet_rate) {
+  if (cap_file_hashes) {
                           print_stats_table_header_label("SHA1");
                           print_stats_table_header_label("RIPEMD160");
                           print_stats_table_header_label("MD5");
@@ -410,14 +435,14 @@ print_stats_table(const gchar *filename, capture_info *cf_info)
   if (cap_start_time) {
     putsep();
     putquote();
-    printf("%s", (cf_info->packet_count>0) ? ctime_no_lf (&start_time_t) : "n/a");
+    printf("%s", time_string(&start_time_t, cf_info, FALSE));
     putquote();
   }
 
   if (cap_end_time) {
     putsep();
     putquote();
-    printf("%s", (cf_info->packet_count>0) ? ctime_no_lf (&stop_time_t) : "n/a");
+    printf("%s", time_string(&stop_time_t, cf_info, FALSE));
     putquote();
   }
 
@@ -554,7 +579,7 @@ process_cap_file(wtap *wth, const char *filename)
   cf_info.packet_size = 0.0;
 
   if (packet > 0) {
-    if (cf_info.duration > 0.0) { 
+    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 */
     }
@@ -609,6 +634,7 @@ usage(gboolean is_error)
   fprintf(output, "  -u display the capture duration (in seconds)\n");
   fprintf(output, "  -a display the capture start time\n");
   fprintf(output, "  -e display the capture end time\n");
+  fprintf(output, "  -S display start and end times as seconds\n");
   fprintf(output, "\n");
   fprintf(output, "Statistic infos:\n");
   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
@@ -664,7 +690,7 @@ failure_message(const char *msg_format _U_, va_list ap _U_)
 static void
 hash_to_str(const unsigned char *hash, size_t length, char *str) {
   int i;
-  
+
   for (i = 0; i < (int) length; i++) {
     sprintf(str+(i*2), "%02x", hash[i]);
   }
@@ -684,8 +710,8 @@ main(int argc, char *argv[])
 #endif
 #ifdef HAVE_LIBGCRYPT
   FILE *fh;
-  char hash_buf[HASH_BUF_SIZE];
-  gcry_md_hd_t hd;
+  char *hash_buf = NULL;
+  gcry_md_hd_t hd = NULL;
   size_t hash_bytes;
 #endif
 
@@ -708,7 +734,7 @@ main(int argc, char *argv[])
 
   /* Process the options */
 
-  while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "duaeyizvhxCALTRrNqQBmb")) !=-1) {
+  while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "duaeyizvhxCALTRrSNqQBmb")) !=-1) {
 
     switch (opt) {
 
@@ -752,6 +778,10 @@ main(int argc, char *argv[])
       cap_end_time = TRUE;
       break;
 
+    case 'S':
+      time_as_secs = TRUE;
+      break;
+
     case 'y':
       if (report_all_infos) disable_all_infos();
       cap_data_rate_byte = TRUE;
@@ -856,6 +886,7 @@ main(int argc, char *argv[])
       gcry_md_enable(hd, GCRY_MD_RMD160);
       gcry_md_enable(hd, GCRY_MD_MD5);
     }
+    hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
   }
 #endif
 
@@ -867,7 +898,7 @@ main(int argc, char *argv[])
     strcpy(file_md5, "<unknown>");
 
     if (cap_file_hashes) {
-      fh = ws_fopen(argv[opt], "r");
+      fh = ws_fopen(argv[opt], "rb");
       if (fh && hd) {
         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
           gcry_md_write(hd, hash_buf, hash_bytes);
@@ -877,8 +908,8 @@ main(int argc, char *argv[])
         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
       }
-      fclose(fh);
-      gcry_md_reset(hd);
+      if (fh) fclose(fh);
+      if (hd) gcry_md_reset(hd);
     }
 #endif /* HAVE_LIBGCRYPT */