Rename WTAP_ERR_UNSUPPORTED_ENCAP to WTAP_ERR_UNWRITABLE_ENCAP.
[metze/wireshark/wip.git] / capinfos.c
1 /* capinfos.c
2  * Reports capture file information including # of packets, duration, others
3  *
4  * Copyright 2004 Ian Schorr
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 /*
26  * 2009-09-19: jyoung
27  *
28  * New capinfos features
29  *
30  * Continue processing additional files after
31  * a wiretap open failure.  The new -C option
32  * reverts to capinfos' original behavior which
33  * is to cancel any further file processing at
34  * first file open failure.
35  *
36  * Change the behavior of how the default display
37  * of all infos is initiated.  This gets rid of a
38  * special post getopt() argument count test.
39  *
40  * Add new table output format (with related options)
41  * This feature allows outputting the various infos
42  * into a tab delimited text file, or to a comma
43  * separated variables file (*.csv) instead of the
44  * original "long" format.
45  *
46  * 2011-04-05: wmeier
47  * behaviour changed: Upon exit capinfos will return
48  *  an error status if an error occurred at any
49  *  point during "continuous" file processing.
50  *  (Previously a success status was always
51  *   returned if the -C option was not used).
52  *
53
54  */
55
56
57 #include <config.h>
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdarg.h>
63 #include <locale.h>
64 #include <errno.h>
65
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69
70 #ifdef HAVE_GETOPT_H
71 #include <getopt.h>
72 #endif
73
74 #ifdef HAVE_SYS_TIME_H
75 #include <sys/time.h>
76 #endif
77
78 #include <glib.h>
79
80 #ifdef HAVE_LIBZ
81 #include <zlib.h>     /* to get the libz version number */
82 #endif
83
84 #include <wsutil/privileges.h>
85 #include <wsutil/filesystem.h>
86 #include <wsutil/crash_info.h>
87 #include <wsutil/copyright_info.h>
88 #include <wsutil/os_version_info.h>
89 #include <wsutil/ws_version_info.h>
90
91 #ifdef HAVE_PLUGINS
92 #include <wsutil/plugins.h>
93 #endif
94
95 #include "wtap.h"
96 #include <wsutil/report_err.h>
97 #include <wsutil/str_util.h>
98 #include <wsutil/file_util.h>
99
100 #ifdef HAVE_LIBGCRYPT
101 #include <wsutil/wsgcrypt.h>
102 #endif
103
104 #ifndef HAVE_GETOPT
105 #include "wsutil/wsgetopt.h"
106 #endif
107
108 #ifdef _WIN32
109 #include <wsutil/unicode-utils.h>
110 #endif /* _WIN32 */
111
112 #include "version_info.h"
113
114 /*
115  * By default capinfos now continues processing
116  * the next filename if and when wiretap detects
117  * a problem opening a file.
118  * Use the '-C' option to revert back to original
119  * capinfos behavior which is to abort any
120  * additional file processing at first open file
121  * failure.
122  */
123
124 static gboolean continue_after_wtap_open_offline_failure = TRUE;
125
126 /*
127  * table report variables
128  */
129
130 static gboolean long_report        = TRUE;  /* By default generate long report       */
131 static gchar table_report_header   = TRUE;  /* Generate column header by default     */
132 static gchar field_separator       = '\t';  /* Use TAB as field separator by default */
133 static gchar quote_char            = '\0';  /* Do NOT quote fields by default        */
134 static gboolean machine_readable   = FALSE; /* Display machine-readable numbers      */
135
136 /*
137  * capinfos has the ability to report on a number of
138  * various characteristics ("infos") for each input file.
139  *
140  * By default reporting of all info fields is enabled.
141  *
142  * Optionally the reporting of any specific info field
143  * or combination of info fields can be enabled with
144  * individual options.
145  */
146
147 static gboolean report_all_infos   = TRUE;  /* Report all infos           */
148
149 static gboolean cap_file_type      = TRUE;  /* Report capture type        */
150 static gboolean cap_file_encap     = TRUE;  /* Report encapsulation       */
151 static gboolean cap_snaplen        = TRUE;  /* Packet size limit (snaplen)*/
152 static gboolean cap_packet_count   = TRUE;  /* Report packet count        */
153 static gboolean cap_file_size      = TRUE;  /* Report file size           */
154 static gboolean cap_comment        = TRUE;  /* Display the capture comment */
155
156 static gboolean cap_data_size      = TRUE;  /* Report packet byte size    */
157 static gboolean cap_duration       = TRUE;  /* Report capture duration    */
158 static gboolean cap_start_time     = TRUE;  /* Report capture start time  */
159 static gboolean cap_end_time       = TRUE;  /* Report capture end time    */
160 static gboolean time_as_secs       = FALSE; /* Report time values as raw seconds */
161
162 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
163 static gboolean cap_data_rate_bit  = TRUE;  /* Report data rate bites/sec */
164 static gboolean cap_packet_size    = TRUE;  /* Report average packet size */
165 static gboolean cap_packet_rate    = TRUE;  /* Report average packet rate */
166 static gboolean cap_order          = TRUE;  /* Report if packets are in chronological order (True/False) */
167
168 #ifdef HAVE_LIBGCRYPT
169 static gboolean cap_file_hashes    = TRUE;  /* Calculate file hashes */
170 #endif
171
172 #ifdef USE_GOPTION
173 static gboolean cap_help     = FALSE;
174 static gboolean table_report = FALSE;
175
176 static GOptionEntry general_entries[] =
177 {
178   /* General */
179   { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
180     "display the capture file type", NULL },
181   { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
182     "display the capture file encapsulation", NULL },
183 #ifdef HAVE_LIBGCRYPT
184   { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
185     "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
186 #endif /* HAVE_LIBGCRYPT */
187   { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
188     "display the capture comment ", NULL },
189   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
190 };
191 static GOptionEntry size_entries[] =
192 {
193   /* Size */
194   { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
195     "display the number of packets", NULL },
196   { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
197     "display the size of the file (in bytes)", NULL },
198   { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
199     "display the total length of all packets (in bytes)", NULL },
200   { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
201     "display the packet size limit (snapshot length)", NULL },
202   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
203 };
204 static GOptionEntry time_entries[] =
205 {
206   /* Time */
207   { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
208     "display the capture duration (in seconds)", NULL },
209   { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
210     "display the capture start time", NULL },
211   { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
212     "display the capture end time", NULL },
213   { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
214     "display the capture file chronological status (True/False)", NULL },
215   { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
216     "display start and end times as seconds", NULL },
217   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
218 };
219
220 static GOptionEntry stats_entries[] =
221 {
222   /* Statistics */
223   { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
224     "display average data rate (in bytes/s)", NULL },
225   { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
226     "display average data rate (in bits/s)", NULL },
227   { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
228     "display average packet size (in bytes)", NULL },
229   { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
230     "display average packet rate (in packets/s)", NULL },
231   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
232 };
233
234 static GOptionEntry output_format_entries[] =
235 {
236   /* Output format */
237   { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
238     "generate long report (default)", NULL },
239   { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
240     "generate table report", NULL },
241   { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
242     "display machine-readable (unabbreviated) values in long reports", NULL },
243   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
244 };
245
246 static GOptionEntry table_report_entries[] =
247 {
248   /* Table report */
249   { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
250     "generate header record (default)", NULL },
251   { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
252     "do not generate header record", NULL },
253   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
254 };
255
256 static GOptionEntry misc_entries[] =
257 {
258   { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
259     "display help", NULL },
260   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
261 };
262
263 GOptionContext *ctx;
264 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
265 GError *parse_err = NULL;
266
267 #endif /* USE_GOPTION */
268
269 #ifdef HAVE_LIBGCRYPT
270 #define HASH_SIZE_SHA1   20
271 #define HASH_SIZE_RMD160 20
272 #define HASH_SIZE_MD5    16
273
274 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
275 #define HASH_BUF_SIZE (1024 * 1024)
276
277
278 static gchar file_sha1[HASH_STR_SIZE];
279 static gchar file_rmd160[HASH_STR_SIZE];
280 static gchar file_md5[HASH_STR_SIZE];
281
282 #define FILE_HASH_OPT "H"
283 #else
284 #define FILE_HASH_OPT ""
285 #endif /* HAVE_LIBGCRYPT */
286
287 /*
288  * If we have at least two packets with time stamps, and they're not in
289  * order - i.e., the later packet has a time stamp older than the earlier
290  * packet - the time stamps are known not to be in order.
291  *
292  * If every packet has a time stamp, and they're all in order, the time
293  * stamp is known to be in order.
294  *
295  * Otherwise, we have no idea.
296  */
297 typedef enum {
298   IN_ORDER,
299   NOT_IN_ORDER,
300   ORDER_UNKNOWN
301 } order_t;
302
303 typedef struct _capture_info {
304   const char    *filename;
305   guint16        file_type;
306   gboolean       iscompressed;
307   int            file_encap;
308   gint64         filesize;
309   gchar         *comment;
310
311   guint64        packet_bytes;
312   gboolean       times_known;
313   double         start_time;
314   double         stop_time;
315   guint32        packet_count;
316   gboolean       snap_set;                /* If set in capture file header      */
317   guint32        snaplen;                 /* value from the capture file header */
318   guint32        snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
319   guint32        snaplen_max_inferred;    /*  ...                               */
320   gboolean       drops_known;
321   guint32        drop_count;
322
323   double         duration;
324   double         packet_rate;
325   double         packet_size;
326   double         data_rate;              /* in bytes */
327   gboolean       know_order;
328   order_t        order;
329
330   int           *encap_counts;           /* array of per_packet encap counts; array has one entry per wtap_encap type */
331 } capture_info;
332
333
334 static void
335 enable_all_infos(void)
336 {
337   report_all_infos   = TRUE;
338
339   cap_file_type      = TRUE;
340   cap_file_encap     = TRUE;
341   cap_snaplen        = TRUE;
342   cap_packet_count   = TRUE;
343   cap_file_size      = TRUE;
344   cap_comment        = TRUE;
345
346   cap_data_size      = TRUE;
347   cap_duration       = TRUE;
348   cap_start_time     = TRUE;
349   cap_end_time       = TRUE;
350   cap_order          = TRUE;
351
352   cap_data_rate_byte = TRUE;
353   cap_data_rate_bit  = TRUE;
354   cap_packet_size    = TRUE;
355   cap_packet_rate    = TRUE;
356
357 #ifdef HAVE_LIBGCRYPT
358   cap_file_hashes    = TRUE;
359 #endif /* HAVE_LIBGCRYPT */
360 }
361
362 static void
363 disable_all_infos(void)
364 {
365   report_all_infos   = FALSE;
366
367   cap_file_type      = FALSE;
368   cap_file_encap     = FALSE;
369   cap_snaplen        = FALSE;
370   cap_packet_count   = FALSE;
371   cap_file_size      = FALSE;
372   cap_comment        = FALSE;
373
374   cap_data_size      = FALSE;
375   cap_duration       = FALSE;
376   cap_start_time     = FALSE;
377   cap_end_time       = FALSE;
378   cap_order          = FALSE;
379
380   cap_data_rate_byte = FALSE;
381   cap_data_rate_bit  = FALSE;
382   cap_packet_size    = FALSE;
383   cap_packet_rate    = FALSE;
384
385 #ifdef HAVE_LIBGCRYPT
386   cap_file_hashes    = FALSE;
387 #endif /* HAVE_LIBGCRYPT */
388 }
389
390 static const gchar *
391 order_string(order_t order)
392 {
393   switch (order) {
394
395     case IN_ORDER:
396       return "True";
397
398     case NOT_IN_ORDER:
399       return "False";
400
401     case ORDER_UNKNOWN:
402       return "Unknown";
403
404     default:
405       return "???";  /* "cannot happen" (the next step is "Profit!") */
406   }
407 }
408
409 static gchar *
410 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
411 {
412   const gchar  *lf = want_lf ? "\n" : "";
413   static gchar  time_string_buf[20];
414   char         *time_string_ctime;
415
416   if (cf_info->times_known && cf_info->packet_count > 0) {
417     if (time_as_secs) {
418       /* XXX - Would it be useful to show sub-second precision? */
419       g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
420       return time_string_buf;
421     } else {
422       time_string_ctime = ctime(&timer);
423       if (time_string_ctime == NULL) {
424         g_snprintf(time_string_buf, 20, "Not representable%s", lf);
425         return time_string_buf;
426       }
427       if (!want_lf) {
428         /*
429          * The ctime() function returns a string formatted as:
430          *   "Www Mmm dd hh:mm:ss yyyy\n"
431          * The unwanted '\n' is the 24th character.
432          */
433         time_string_ctime[24] = '\0';
434       }
435       return time_string_ctime;
436     }
437   }
438
439   g_snprintf(time_string_buf, 15, "n/a%s", lf);
440   return time_string_buf;
441 }
442
443 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
444   if (value > 0.0)
445     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
446   else
447     printf("%sn/a\n", text_p1);
448 }
449
450 static void
451 print_stats(const gchar *filename, capture_info *cf_info)
452 {
453   const gchar           *file_type_string, *file_encap_string;
454   time_t                 start_time_t;
455   time_t                 stop_time_t;
456   gchar                 *size_string;
457
458   /* Build printable strings for various stats */
459   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
460   file_encap_string = wtap_encap_string(cf_info->file_encap);
461   start_time_t = (time_t)cf_info->start_time;
462   stop_time_t = (time_t)cf_info->stop_time;
463
464   if (filename)           printf     ("File name:           %s\n", filename);
465   if (cap_file_type)      printf     ("File type:           %s%s\n",
466       file_type_string,
467       cf_info->iscompressed ? " (gzip compressed)" : "");
468   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
469   if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
470     int i;
471     for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
472       if (cf_info->encap_counts[i] > 0)
473         printf("                       %s\n", wtap_encap_string(i));
474     }
475   }
476   if (cap_snaplen && cf_info->snap_set)
477     printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
478   else if (cap_snaplen && !cf_info->snap_set)
479     printf     ("Packet size limit:   file hdr: (not set)\n");
480   if (cf_info->snaplen_max_inferred > 0) {
481     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
482       printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
483     else
484       printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
485           cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
486   }
487   if (cap_packet_count) {
488     printf     ("Number of packets:   ");
489     if (machine_readable) {
490       printf ("%u\n", cf_info->packet_count);
491     } else {
492       size_string = format_size(cf_info->packet_count, format_size_unit_none);
493       printf ("%s\n", size_string);
494       g_free(size_string);
495     }
496   }
497   if (cap_file_size) {
498     printf     ("File size:           ");
499     if (machine_readable) {
500       printf     ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
501     } else {
502       size_string = format_size(cf_info->filesize, format_size_unit_bytes);
503       printf ("%s\n", size_string);
504       g_free(size_string);
505     }
506   }
507   if (cap_data_size) {
508     printf     ("Data size:           ");
509     if (machine_readable) {
510       printf     ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
511     } else {
512       size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
513       printf ("%s\n", size_string);
514       g_free(size_string);
515     }
516   }
517   if (cf_info->times_known) {
518     if (cap_duration) /* XXX - shorten to hh:mm:ss */
519                           print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
520     if (cap_start_time)
521                           printf     ("Start time:          %s", time_string(start_time_t, cf_info, TRUE));
522     if (cap_end_time)
523                           printf     ("End time:            %s", time_string(stop_time_t, cf_info, TRUE));
524     if (cap_data_rate_byte) {
525                           printf     ("Data byte rate:      ");
526       if (machine_readable) {
527         print_value("", 2, " bytes/sec",   cf_info->data_rate);
528       } else {
529         size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
530         printf ("%s\n", size_string);
531         g_free(size_string);
532       }
533     }
534     if (cap_data_rate_bit) {
535                           printf     ("Data bit rate:       ");
536       if (machine_readable) {
537         print_value("", 2, " bits/sec",    cf_info->data_rate*8);
538       } else {
539         size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
540         printf ("%s\n", size_string);
541         g_free(size_string);
542       }
543     }
544   }
545   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
546   if (cf_info->times_known) {
547     if (cap_packet_rate) {
548                           printf     ("Average packet rate: ");
549       if (machine_readable) {
550         print_value("", 2, " packets/sec", cf_info->packet_rate);
551       } else {
552         size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
553         printf ("%s\n", size_string);
554         g_free(size_string);
555       }
556     }
557   }
558 #ifdef HAVE_LIBGCRYPT
559   if (cap_file_hashes) {
560     printf     ("SHA1:                %s\n", file_sha1);
561     printf     ("RIPEMD160:           %s\n", file_rmd160);
562     printf     ("MD5:                 %s\n", file_md5);
563   }
564 #endif /* HAVE_LIBGCRYPT */
565   if (cap_order)          printf     ("Strict time order:   %s\n", order_string(cf_info->order));
566   if (cap_comment && cf_info->comment)
567     printf     ("Capture comment:     %s\n", cf_info->comment);
568 }
569
570 static void
571 putsep(void)
572 {
573   if (field_separator) putchar(field_separator);
574 }
575
576 static void
577 putquote(void)
578 {
579   if (quote_char) putchar(quote_char);
580 }
581
582 static void
583 print_stats_table_header_label(const gchar *label)
584 {
585   putsep();
586   putquote();
587   printf("%s", label);
588   putquote();
589 }
590
591 static void
592 print_stats_table_header(void)
593 {
594   putquote();
595   printf("File name");
596   putquote();
597
598   if (cap_file_type)      print_stats_table_header_label("File type");
599   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
600   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
601   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
602   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
603   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
604   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
605   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
606   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
607   if (cap_start_time)     print_stats_table_header_label("Start time");
608   if (cap_end_time)       print_stats_table_header_label("End time");
609   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
610   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
611   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
612   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
613 #ifdef HAVE_LIBGCRYPT
614   if (cap_file_hashes) {
615     print_stats_table_header_label("SHA1");
616     print_stats_table_header_label("RIPEMD160");
617     print_stats_table_header_label("MD5");
618   }
619 #endif /* HAVE_LIBGCRYPT */
620   if (cap_order)          print_stats_table_header_label("Strict time order");
621   if (cap_comment)        print_stats_table_header_label("Capture comment");
622
623   printf("\n");
624 }
625
626 static void
627 print_stats_table(const gchar *filename, capture_info *cf_info)
628 {
629   const gchar           *file_type_string, *file_encap_string;
630   time_t                 start_time_t;
631   time_t                 stop_time_t;
632
633   /* Build printable strings for various stats */
634   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
635   file_encap_string = wtap_encap_string(cf_info->file_encap);
636   start_time_t = (time_t)cf_info->start_time;
637   stop_time_t = (time_t)cf_info->stop_time;
638
639   if (filename) {
640     putquote();
641     printf("%s", filename);
642     putquote();
643   }
644
645   if (cap_file_type) {
646     putsep();
647     putquote();
648     printf("%s", file_type_string);
649     putquote();
650   }
651
652   /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
653    *       Output a line for each different encap with all fields repeated except
654    *        the encapsulation field which has "Per Packet: ..." for each
655    *        encapsulation type seen ?
656    */
657   if (cap_file_encap) {
658     putsep();
659     putquote();
660     printf("%s", file_encap_string);
661     putquote();
662   }
663
664   if (cap_snaplen) {
665     putsep();
666     putquote();
667     if (cf_info->snap_set)
668       printf("%u", cf_info->snaplen);
669     else
670       printf("(not set)");
671     putquote();
672     if (cf_info->snaplen_max_inferred > 0) {
673       putsep();
674       putquote();
675       printf("%u", cf_info->snaplen_min_inferred);
676       putquote();
677       putsep();
678       putquote();
679       printf("%u", cf_info->snaplen_max_inferred);
680       putquote();
681     }
682     else {
683       putsep();
684       putquote();
685       printf("n/a");
686       putquote();
687       putsep();
688       putquote();
689       printf("n/a");
690       putquote();
691     }
692   }
693
694   if (cap_packet_count) {
695     putsep();
696     putquote();
697     printf("%u", cf_info->packet_count);
698     putquote();
699   }
700
701   if (cap_file_size) {
702     putsep();
703     putquote();
704     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
705     putquote();
706   }
707
708   if (cap_data_size) {
709     putsep();
710     putquote();
711     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
712     putquote();
713   }
714
715   if (cap_duration) {
716     putsep();
717     putquote();
718     if (cf_info->times_known)
719       printf("%f", cf_info->duration);
720     else
721       printf("n/a");
722     putquote();
723   }
724
725   if (cap_start_time) {
726     putsep();
727     putquote();
728     printf("%s", time_string(start_time_t, cf_info, FALSE));
729     putquote();
730   }
731
732   if (cap_end_time) {
733     putsep();
734     putquote();
735     printf("%s", time_string(stop_time_t, cf_info, FALSE));
736     putquote();
737   }
738
739   if (cap_data_rate_byte) {
740     putsep();
741     putquote();
742     if (cf_info->times_known)
743       printf("%.2f", cf_info->data_rate);
744     else
745       printf("n/a");
746     putquote();
747   }
748
749   if (cap_data_rate_bit) {
750     putsep();
751     putquote();
752     if (cf_info->times_known)
753       printf("%.2f", cf_info->data_rate*8);
754     else
755       printf("n/a");
756     putquote();
757   }
758
759   if (cap_packet_size) {
760     putsep();
761     putquote();
762     printf("%.2f", cf_info->packet_size);
763     putquote();
764   }
765
766   if (cap_packet_rate) {
767     putsep();
768     putquote();
769     if (cf_info->times_known)
770       printf("%.2f", cf_info->packet_rate);
771     else
772       printf("n/a");
773     putquote();
774   }
775
776 #ifdef HAVE_LIBGCRYPT
777   if (cap_file_hashes) {
778     putsep();
779     putquote();
780     printf("%s", file_sha1);
781     putquote();
782
783     putsep();
784     putquote();
785     printf("%s", file_rmd160);
786     putquote();
787
788     putsep();
789     putquote();
790     printf("%s", file_md5);
791     putquote();
792   }
793 #endif /* HAVE_LIBGCRYPT */
794
795   if (cap_order) {
796     putsep();
797     putquote();
798     printf("%s", order_string(cf_info->order));
799     putquote();
800   }
801
802   if (cap_comment) {
803     putsep();
804     putquote();
805     printf("%s", cf_info->comment);
806     putquote();
807   }
808
809
810   printf("\n");
811 }
812
813 static int
814 process_cap_file(wtap *wth, const char *filename)
815 {
816   int                   status = 0;
817   int                   err;
818   gchar                *err_info;
819   gint64                size;
820   gint64                data_offset;
821
822   guint32               packet = 0;
823   gint64                bytes  = 0;
824   guint32               snaplen_min_inferred = 0xffffffff;
825   guint32               snaplen_max_inferred =          0;
826   const struct wtap_pkthdr *phdr;
827   capture_info          cf_info;
828   gboolean              have_times = TRUE;
829   double                start_time = 0;
830   double                stop_time  = 0;
831   double                cur_time   = 0;
832   double                prev_time = 0;
833   gboolean              know_order = FALSE;
834   order_t               order = IN_ORDER;
835   wtapng_section_t     *shb_inf;
836   gchar                *p;
837
838
839   cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
840
841   /* Tally up data that we need to parse through the file to find */
842   while (wtap_read(wth, &err, &err_info, &data_offset))  {
843     phdr = wtap_phdr(wth);
844     if (phdr->presence_flags & WTAP_HAS_TS) {
845       prev_time = cur_time;
846       cur_time = nstime_to_sec(&phdr->ts);
847       if (packet == 0) {
848         start_time = cur_time;
849         stop_time  = cur_time;
850         prev_time  = cur_time;
851       }
852       if (cur_time < prev_time) {
853         order = NOT_IN_ORDER;
854       }
855       if (cur_time < start_time) {
856         start_time = cur_time;
857       }
858       if (cur_time > stop_time) {
859         stop_time = cur_time;
860       }
861     } else {
862       have_times = FALSE; /* at least one packet has no time stamp */
863       if (order != NOT_IN_ORDER)
864         order = ORDER_UNKNOWN;
865     }
866
867     if (phdr->rec_type == REC_TYPE_PACKET) {
868       bytes+=phdr->len;
869       packet++;
870
871       /* If caplen < len for a rcd, then presumably           */
872       /* 'Limit packet capture length' was done for this rcd. */
873       /* Keep track as to the min/max actual snapshot lengths */
874       /*  seen for this file.                                 */
875       if (phdr->caplen < phdr->len) {
876         if (phdr->caplen < snaplen_min_inferred)
877           snaplen_min_inferred = phdr->caplen;
878         if (phdr->caplen > snaplen_max_inferred)
879           snaplen_max_inferred = phdr->caplen;
880       }
881
882       /* Per-packet encapsulation */
883       if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
884         if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
885           cf_info.encap_counts[phdr->pkt_encap] += 1;
886         } else {
887           fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
888                   phdr->pkt_encap, packet, filename);
889         }
890       }
891     }
892
893   } /* while */
894
895   if (err != 0) {
896     fprintf(stderr,
897         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
898         packet, filename, wtap_strerror(err));
899     switch (err) {
900
901       case WTAP_ERR_SHORT_READ:
902         status = 1;
903         fprintf(stderr,
904           "  (will continue anyway, checksums might be incorrect)\n");
905         break;
906
907       case WTAP_ERR_UNSUPPORTED:
908       case WTAP_ERR_UNWRITABLE_ENCAP:
909       case WTAP_ERR_BAD_FILE:
910       case WTAP_ERR_DECOMPRESS:
911         fprintf(stderr, "(%s)\n", err_info);
912         g_free(err_info);
913         /* fallthrough */
914
915       default:
916         g_free(cf_info.encap_counts);
917         return 1;
918     }
919   }
920
921   /* File size */
922   size = wtap_file_size(wth, &err);
923   if (size == -1) {
924     fprintf(stderr,
925         "capinfos: Can't get size of \"%s\": %s.\n",
926         filename, g_strerror(err));
927     g_free(cf_info.encap_counts);
928     return 1;
929   }
930
931   cf_info.filesize = size;
932
933   /* File Type */
934   cf_info.file_type = wtap_file_type_subtype(wth);
935   cf_info.iscompressed = wtap_iscompressed(wth);
936
937   /* File Encapsulation */
938   cf_info.file_encap = wtap_file_encap(wth);
939
940   /* Packet size limit (snaplen) */
941   cf_info.snaplen = wtap_snapshot_length(wth);
942   if (cf_info.snaplen > 0)
943     cf_info.snap_set = TRUE;
944   else
945     cf_info.snap_set = FALSE;
946
947   cf_info.snaplen_min_inferred = snaplen_min_inferred;
948   cf_info.snaplen_max_inferred = snaplen_max_inferred;
949
950   /* # of packets */
951   cf_info.packet_count = packet;
952
953   /* File Times */
954   cf_info.times_known = have_times;
955   cf_info.start_time = start_time;
956   cf_info.stop_time = stop_time;
957   cf_info.duration = stop_time-start_time;
958   cf_info.know_order = know_order;
959   cf_info.order = order;
960
961   /* Number of packet bytes */
962   cf_info.packet_bytes = bytes;
963
964   cf_info.data_rate   = 0.0;
965   cf_info.packet_rate = 0.0;
966   cf_info.packet_size = 0.0;
967
968   if (packet > 0) {
969     if (cf_info.duration > 0.0) {
970       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
971       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
972     }
973     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
974   }
975
976   cf_info.comment = NULL;
977   shb_inf = wtap_file_get_shb_info(wth);
978   if (shb_inf) {
979     /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
980     cf_info.comment = g_strdup(shb_inf->opt_comment);
981   }
982   g_free(shb_inf);
983   if (cf_info.comment) {
984     /* multi-line comments would conflict with the formatting that capinfos uses
985        we replace linefeeds with spaces */
986     p = cf_info.comment;
987     while (*p != '\0') {
988       if (*p == '\n')
989         *p = ' ';
990       p++;
991     }
992   }
993
994   if (long_report) {
995     print_stats(filename, &cf_info);
996   } else {
997     print_stats_table(filename, &cf_info);
998   }
999
1000   g_free(cf_info.encap_counts);
1001   g_free(cf_info.comment);
1002
1003   return status;
1004 }
1005
1006 static void
1007 show_version(GString *comp_info_str, GString *runtime_info_str)
1008 {
1009     printf("Capinfos (Wireshark) %s\n"
1010            "\n"
1011            "%s"
1012            "\n"
1013            "%s"
1014            "\n"
1015            "%s",
1016            get_ws_vcs_version_info(), get_copyright_info(),
1017            comp_info_str->str, runtime_info_str->str);
1018 }
1019
1020 static void
1021 print_usage(FILE *output)
1022 {
1023   fprintf(output, "\n");
1024   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1025   fprintf(output, "\n");
1026   fprintf(output, "General infos:\n");
1027   fprintf(output, "  -t display the capture file type\n");
1028   fprintf(output, "  -E display the capture file encapsulation\n");
1029 #ifdef HAVE_LIBGCRYPT
1030   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1031 #endif
1032   fprintf(output, "  -k display the capture comment\n");
1033   fprintf(output, "\n");
1034   fprintf(output, "Size infos:\n");
1035   fprintf(output, "  -c display the number of packets\n");
1036   fprintf(output, "  -s display the size of the file (in bytes)\n");
1037   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1038   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1039   fprintf(output, "\n");
1040   fprintf(output, "Time infos:\n");
1041   fprintf(output, "  -u display the capture duration (in seconds)\n");
1042   fprintf(output, "  -a display the capture start time\n");
1043   fprintf(output, "  -e display the capture end time\n");
1044   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1045   fprintf(output, "  -S display start and end times as seconds\n");
1046   fprintf(output, "\n");
1047   fprintf(output, "Statistic infos:\n");
1048   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1049   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1050   fprintf(output, "  -z display average packet size (in bytes)\n");
1051   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1052   fprintf(output, "\n");
1053   fprintf(output, "Output format:\n");
1054   fprintf(output, "  -L generate long report (default)\n");
1055   fprintf(output, "  -T generate table report\n");
1056   fprintf(output, "  -M display machine-readable values in long reports\n");
1057   fprintf(output, "\n");
1058   fprintf(output, "Table report options:\n");
1059   fprintf(output, "  -R generate header record (default)\n");
1060   fprintf(output, "  -r do not generate header record\n");
1061   fprintf(output, "\n");
1062   fprintf(output, "  -B separate infos with TAB character (default)\n");
1063   fprintf(output, "  -m separate infos with comma (,) character\n");
1064   fprintf(output, "  -b separate infos with SPACE character\n");
1065   fprintf(output, "\n");
1066   fprintf(output, "  -N do not quote infos (default)\n");
1067   fprintf(output, "  -q quote infos with single quotes (')\n");
1068   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1069   fprintf(output, "\n");
1070   fprintf(output, "Miscellaneous:\n");
1071   fprintf(output, "  -h display this help and exit\n");
1072   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1073   fprintf(output, "  -A generate all infos (default)\n");
1074   fprintf(output, "\n");
1075   fprintf(output, "Options are processed from left to right order with later options superceding\n");
1076   fprintf(output, "or adding to earlier options.\n");
1077   fprintf(output, "\n");
1078   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1079   fprintf(output, "output format.\n");
1080 #ifndef HAVE_LIBGCRYPT
1081   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1082 #endif
1083 }
1084
1085 #ifdef HAVE_PLUGINS
1086 /*
1087  *  Don't report failures to load plugins because most (non-wiretap) plugins
1088  *  *should* fail to load (because we're not linked against libwireshark and
1089  *  dissector plugins need libwireshark).
1090  */
1091 static void
1092 failure_message(const char *msg_format _U_, va_list ap _U_)
1093 {
1094   return;
1095 }
1096 #endif
1097
1098 #ifdef HAVE_LIBGCRYPT
1099 static void
1100 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1101   int i;
1102
1103   for (i = 0; i < (int) length; i++) {
1104     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1105   }
1106 }
1107 #endif /* HAVE_LIBGCRYPT */
1108
1109 static void
1110 get_capinfos_compiled_info(GString *str)
1111 {
1112   /* LIBZ */
1113   g_string_append(str, ", ");
1114 #ifdef HAVE_LIBZ
1115   g_string_append(str, "with libz ");
1116 #ifdef ZLIB_VERSION
1117   g_string_append(str, ZLIB_VERSION);
1118 #else /* ZLIB_VERSION */
1119   g_string_append(str, "(version unknown)");
1120 #endif /* ZLIB_VERSION */
1121 #else /* HAVE_LIBZ */
1122   g_string_append(str, "without libz");
1123 #endif /* HAVE_LIBZ */
1124 }
1125
1126 static void
1127 get_capinfos_runtime_info(GString *str)
1128 {
1129   /* zlib */
1130 #if defined(HAVE_LIBZ) && !defined(_WIN32)
1131   g_string_append_printf(str, ", with libz %s", zlibVersion());
1132 #endif
1133 }
1134
1135 int
1136 main(int argc, char *argv[])
1137 {
1138   GString *comp_info_str;
1139   GString *runtime_info_str;
1140   wtap  *wth;
1141   int    err;
1142   gchar *err_info;
1143   int    opt;
1144   int    overall_error_status;
1145   static const struct option long_options[] = {
1146       {(char *)"help", no_argument, NULL, 'h'},
1147       {(char *)"version", no_argument, NULL, 'v'},
1148       {0, 0, 0, 0 }
1149   };
1150
1151   int status = 0;
1152 #ifdef HAVE_PLUGINS
1153   char  *init_progfile_dir_error;
1154 #endif
1155 #ifdef HAVE_LIBGCRYPT
1156   FILE  *fh;
1157   char  *hash_buf = NULL;
1158   gcry_md_hd_t hd = NULL;
1159   size_t hash_bytes;
1160 #endif
1161
1162   /* Assemble the compile-time version information string */
1163   comp_info_str = g_string_new("Compiled ");
1164   get_compiled_version_info(comp_info_str, NULL, get_capinfos_compiled_info);
1165
1166   /* Assemble the run-time version information string */
1167   runtime_info_str = g_string_new("Running ");
1168   get_runtime_version_info(runtime_info_str, get_capinfos_runtime_info);
1169
1170   /* Add it to the information to be reported on a crash. */
1171   ws_add_crash_info("Capinfos (Wireshark) %s\n"
1172          "\n"
1173          "%s"
1174          "\n"
1175          "%s",
1176       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1177
1178 #ifdef _WIN32
1179   arg_list_utf_16to8(argc, argv);
1180   create_app_running_mutex();
1181 #endif /* _WIN32 */
1182
1183   /*
1184    * Get credential information for later use.
1185    */
1186   init_process_policies();
1187   init_open_routines();
1188
1189 #ifdef HAVE_PLUGINS
1190   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1191     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1192     g_free(init_progfile_dir_error);
1193   } else {
1194     /* Register all the plugin types we have. */
1195     wtap_register_plugin_types(); /* Types known to libwiretap */
1196
1197     init_report_err(failure_message, NULL, NULL, NULL);
1198
1199     /* Scan for plugins.  This does *not* call their registration routines;
1200        that's done later. */
1201     scan_plugins();
1202
1203     /* Register all libwiretap plugin modules. */
1204     register_all_wiretap_modules();
1205   }
1206 #endif
1207
1208   /* Process the options */
1209 #ifdef USE_GOPTION
1210   ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1211   general_grp = g_option_group_new("gen", "General infos:",
1212       "Show general options", NULL, NULL);
1213   size_grp = g_option_group_new("size", "Size infos:",
1214       "Show size options", NULL, NULL);
1215   time_grp = g_option_group_new("time", "Time infos:",
1216       "Show time options", NULL, NULL);
1217   stats_grp = g_option_group_new("stats", "Statistics infos:",
1218       "Show statistics options", NULL, NULL);
1219   output_grp = g_option_group_new("output", "Output format:",
1220       "Show output format options", NULL, NULL);
1221   table_report_grp = g_option_group_new("table", "Table report options:",
1222       "Show table report options", NULL, NULL);
1223   g_option_group_add_entries(general_grp, general_entries);
1224   g_option_group_add_entries(size_grp, size_entries);
1225   g_option_group_add_entries(time_grp, time_entries);
1226   g_option_group_add_entries(stats_grp, stats_entries);
1227   g_option_group_add_entries(output_grp, output_format_entries);
1228   g_option_group_add_entries(table_report_grp, table_report_entries);
1229   g_option_context_add_main_entries(ctx, misc_entries, NULL);
1230   g_option_context_add_group(ctx, general_grp);
1231   g_option_context_add_group(ctx, size_grp);
1232   g_option_context_add_group(ctx, time_grp);
1233   g_option_context_add_group(ctx, stats_grp);
1234   g_option_context_add_group(ctx, output_grp);
1235   g_option_context_add_group(ctx, table_report_grp);
1236   /* There's probably a better way to do this, but this works for now.
1237      GOptions displays the name in argv[0] as the name of the
1238      application.  This is reasonable, but because we actually have a
1239      script wrapper that calls the executable.  The name that gets
1240      displayed is not exactly the same as the command the user used
1241      ran.
1242    */
1243   argv[0] = (char *)"capinfos";
1244
1245   /* if we have at least one cmdline option, we disable printing all infos */
1246   if (argc > 2 && report_all_infos)
1247     disable_all_infos();
1248
1249   if ( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1250     if (parse_err)
1251       g_printerr ("option parsing failed: %s\n", parse_err->message);
1252     g_printerr("%s", g_option_context_get_help (ctx, TRUE, NULL));
1253     exit(1);
1254   }
1255   if ( cap_help ) {
1256     g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1257     exit(0);
1258   }
1259   if ( argc < 2 ) {
1260     g_printerr("%s", g_option_context_get_help (ctx, FALSE, NULL));
1261     exit(1);
1262   }
1263   g_option_context_free(ctx);
1264
1265 #endif /* USE_GOPTION */
1266   while ((opt = getopt_long(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb", long_options, NULL)) !=-1) {
1267
1268     switch (opt) {
1269
1270       case 't':
1271         if (report_all_infos) disable_all_infos();
1272         cap_file_type = TRUE;
1273         break;
1274
1275       case 'E':
1276         if (report_all_infos) disable_all_infos();
1277         cap_file_encap = TRUE;
1278         break;
1279
1280       case 'l':
1281         if (report_all_infos) disable_all_infos();
1282         cap_snaplen = TRUE;
1283         break;
1284
1285       case 'c':
1286         if (report_all_infos) disable_all_infos();
1287         cap_packet_count = TRUE;
1288         break;
1289
1290       case 's':
1291         if (report_all_infos) disable_all_infos();
1292         cap_file_size = TRUE;
1293         break;
1294
1295       case 'd':
1296         if (report_all_infos) disable_all_infos();
1297         cap_data_size = TRUE;
1298         break;
1299
1300       case 'u':
1301         if (report_all_infos) disable_all_infos();
1302         cap_duration = TRUE;
1303         break;
1304
1305       case 'a':
1306         if (report_all_infos) disable_all_infos();
1307         cap_start_time = TRUE;
1308         break;
1309
1310       case 'e':
1311         if (report_all_infos) disable_all_infos();
1312         cap_end_time = TRUE;
1313         break;
1314
1315       case 'S':
1316         time_as_secs = TRUE;
1317         break;
1318
1319       case 'y':
1320         if (report_all_infos) disable_all_infos();
1321         cap_data_rate_byte = TRUE;
1322         break;
1323
1324       case 'i':
1325         if (report_all_infos) disable_all_infos();
1326         cap_data_rate_bit = TRUE;
1327         break;
1328
1329       case 'z':
1330         if (report_all_infos) disable_all_infos();
1331         cap_packet_size = TRUE;
1332         break;
1333
1334       case 'x':
1335         if (report_all_infos) disable_all_infos();
1336         cap_packet_rate = TRUE;
1337         break;
1338
1339 #ifdef HAVE_LIBGCRYPT
1340       case 'H':
1341         if (report_all_infos) disable_all_infos();
1342         cap_file_hashes = TRUE;
1343         break;
1344 #endif
1345
1346       case 'o':
1347         if (report_all_infos) disable_all_infos();
1348         cap_order = TRUE;
1349         break;
1350
1351       case 'k':
1352         if (report_all_infos) disable_all_infos();
1353         cap_comment = TRUE;
1354         break;
1355
1356       case 'C':
1357         continue_after_wtap_open_offline_failure = FALSE;
1358         break;
1359
1360       case 'A':
1361         enable_all_infos();
1362         break;
1363
1364       case 'L':
1365         long_report = TRUE;
1366         break;
1367
1368       case 'T':
1369         long_report = FALSE;
1370         break;
1371
1372       case 'M':
1373         machine_readable = TRUE;
1374         break;
1375
1376       case 'R':
1377         table_report_header = TRUE;
1378         break;
1379
1380       case 'r':
1381         table_report_header = FALSE;
1382         break;
1383
1384       case 'N':
1385         quote_char = '\0';
1386         break;
1387
1388       case 'q':
1389         quote_char = '\'';
1390         break;
1391
1392       case 'Q':
1393         quote_char = '"';
1394         break;
1395
1396       case 'B':
1397         field_separator = '\t';
1398         break;
1399
1400       case 'm':
1401         field_separator = ',';
1402         break;
1403
1404       case 'b':
1405         field_separator = ' ';
1406         break;
1407
1408       case 'h':
1409         printf("Capinfos (Wireshark) %s\n"
1410                "Print various information (infos) about capture files.\n"
1411                "See http://www.wireshark.org for more information.\n",
1412                get_ws_vcs_version_info());
1413         print_usage(stdout);
1414         exit(0);
1415         break;
1416
1417       case 'v':
1418         show_version(comp_info_str, runtime_info_str);
1419         g_string_free(comp_info_str, TRUE);
1420         g_string_free(runtime_info_str, TRUE);
1421         exit(0);
1422         break;
1423
1424       case '?':              /* Bad flag - print usage message */
1425         print_usage(stderr);
1426         exit(1);
1427         break;
1428     }
1429   }
1430
1431   /* Set the C-language locale to the native environment. */
1432   setlocale(LC_ALL, "");
1433
1434   if ((argc - optind) < 1) {
1435     print_usage(stderr);
1436     exit(1);
1437   }
1438
1439   if (!long_report && table_report_header) {
1440     print_stats_table_header();
1441   }
1442
1443 #ifdef HAVE_LIBGCRYPT
1444   if (cap_file_hashes) {
1445     gcry_check_version(NULL);
1446     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1447     if (hd) {
1448       gcry_md_enable(hd, GCRY_MD_RMD160);
1449       gcry_md_enable(hd, GCRY_MD_MD5);
1450     }
1451     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1452   }
1453 #endif
1454
1455   overall_error_status = 0;
1456
1457   for (opt = optind; opt < argc; opt++) {
1458
1459 #ifdef HAVE_LIBGCRYPT
1460     g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1461     g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1462     g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1463
1464     if (cap_file_hashes) {
1465       fh = ws_fopen(argv[opt], "rb");
1466       if (fh && hd) {
1467         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1468           gcry_md_write(hd, hash_buf, hash_bytes);
1469         }
1470         gcry_md_final(hd);
1471         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1472         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1473         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1474       }
1475       if (fh) fclose(fh);
1476       if (hd) gcry_md_reset(hd);
1477     }
1478 #endif /* HAVE_LIBGCRYPT */
1479
1480     wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1481
1482     if (!wth) {
1483       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1484           wtap_strerror(err));
1485       switch (err) {
1486
1487         case WTAP_ERR_UNSUPPORTED:
1488         case WTAP_ERR_UNWRITABLE_ENCAP:
1489         case WTAP_ERR_BAD_FILE:
1490         case WTAP_ERR_DECOMPRESS:
1491           fprintf(stderr, "(%s)\n", err_info);
1492           g_free(err_info);
1493           break;
1494       }
1495       overall_error_status = 1; /* remember that an error has occurred */
1496       if (!continue_after_wtap_open_offline_failure)
1497         exit(1); /* error status */
1498     }
1499
1500     if (wth) {
1501       if ((opt > optind) && (long_report))
1502         printf("\n");
1503       status = process_cap_file(wth, argv[opt]);
1504
1505       wtap_close(wth);
1506       if (status)
1507         exit(status);
1508     }
1509   }
1510
1511   return overall_error_status;
1512 }
1513
1514 /*
1515  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1516  *
1517  * Local variables:
1518  * c-basic-offset: 2
1519  * tab-width: 8
1520  * indent-tabs-mode: nil
1521  * End:
1522  *
1523  * vi: set shiftwidth=2 tabstop=8 expandtab:
1524  * :indentSize=2:tabSize=8:noTabs=true:
1525  */