From Stephen Fisher:
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete packets, adjust timestamps, or
2  * simply convert from one format to another format.
3  *
4  * $Id$
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  * Further improved by Richard Sharpe.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <glib.h>
19
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23
24
25 /*
26  * Just make sure we include the prototype for strptime as well
27  * (needed for glibc 2.2)
28  */
29 #define __USE_XOPEN
30
31 #include <time.h>
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35
36 #include <string.h>
37 #include "wtap.h"
38
39 #ifdef NEED_GETOPT_H
40 #include "getopt.h"
41 #endif
42
43 #ifdef _WIN32
44 #include <process.h>    /* getpid */
45 #endif
46
47 #ifdef NEED_STRPTIME_H
48 # include "strptime.h"
49 #endif
50
51 #include "epan/crypt-md5.h"
52
53 #include "svnversion.h"
54
55 /*
56  * Some globals so we can pass things to various routines
57  */
58
59 struct select_item {
60
61   int inclusive;
62   int first, second;
63
64 };
65
66
67 /*
68  * Duplicate frame detection
69  */
70 typedef struct _fd_hash_t {
71   md5_byte_t digest[16];
72   guint32 len;
73 } fd_hash_t;
74
75 #define DUP_DEPTH 5
76 fd_hash_t fd_hash[DUP_DEPTH];
77 int cur_dup = 0;
78
79 #define ONE_MILLION 1000000
80
81 /* Weights of different errors we can introduce */
82 /* We should probably make these command-line arguments */
83 /* XXX - Should we add a bit-level error? */
84 #define ERR_WT_BIT   5  /* Flip a random bit */
85 #define ERR_WT_BYTE  5  /* Substitute a random byte */
86 #define ERR_WT_ALNUM 5  /* Substitute a random character in [A-Za-z0-9] */
87 #define ERR_WT_FMT   2  /* Substitute "%s" */
88 #define ERR_WT_AA    1  /* Fill the remainder of the buffer with 0xAA */
89 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
90
91 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
92 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
93
94
95 struct time_adjustment {
96   struct timeval tv;
97   int is_negative;
98 };
99
100 static struct select_item selectfrm[100];
101 static int max_selected = -1;
102 static int keep_em = 0;
103 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
104 static int out_frame_type = -2;              /* Leave frame type alone */
105 static int verbose = 0;                      /* Not so verbose         */
106 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
107 static double err_prob = 0.0;
108 static time_t starttime = 0;
109 static time_t stoptime = 0;
110 static gboolean check_startstop = FALSE;
111 static gboolean dup_detect = FALSE;
112
113 /* Add a selection item, a simple parser for now */
114
115 static void add_selection(char *sel)
116 {
117   char *locn;
118   char *next;
119
120   if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
121     return;
122
123   printf("Add_Selected: %s\n", sel);
124
125   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
126
127     printf("Not inclusive ...");
128
129     max_selected++;
130     selectfrm[max_selected].inclusive = 0;
131     selectfrm[max_selected].first = atoi(sel);
132
133     printf(" %i\n", selectfrm[max_selected].first);
134
135   }
136   else {
137
138     printf("Inclusive ...");
139
140     next = locn + 1;
141     max_selected++;
142     selectfrm[max_selected].inclusive = 1;
143     selectfrm[max_selected].first = atoi(sel);
144     selectfrm[max_selected].second = atoi(next);
145
146     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
147
148   }
149
150
151 }
152
153 /* Was the packet selected? */
154
155 static int selected(int recno)
156 {
157   int i = 0;
158
159   for (i = 0; i<= max_selected; i++) {
160
161     if (selectfrm[i].inclusive) {
162       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
163         return 1;
164     }
165     else {
166       if (recno == selectfrm[i].first)
167         return 1;
168     }
169   }
170
171   return 0;
172
173 }
174
175 /* is the packet in the selected timeframe */
176 static gboolean check_timestamp(wtap *wth) {
177         struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
178         return ( (time_t) pkthdr->ts.secs >= starttime ) && ( (time_t) pkthdr->ts.secs <= stoptime );
179 }
180
181 static void
182 set_time_adjustment(char *optarg)
183 {
184   char *frac, *end;
185   long val;
186   int frac_digits;
187
188   if (!optarg)
189     return;
190
191   /* skip leading whitespace */
192   while (*optarg == ' ' || *optarg == '\t') {
193       optarg++;
194   }
195
196   /* check for a negative adjustment */
197   if (*optarg == '-') {
198       time_adj.is_negative = 1;
199       optarg++;
200   }
201
202   /* collect whole number of seconds, if any */
203   if (*optarg == '.') {         /* only fractional (i.e., .5 is ok) */
204       val  = 0;
205       frac = optarg;
206   } else {
207       val = strtol(optarg, &frac, 10);
208       if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
209           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
210                   optarg);
211           exit(1);
212       }
213       if (val < 0) {            /* implies '--' since we caught '-' above  */
214           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
215                   optarg);
216           exit(1);
217       }
218   }
219   time_adj.tv.tv_sec = val;
220
221   /* now collect the partial seconds, if any */
222   if (*frac != '\0') {             /* chars left, so get fractional part */
223     val = strtol(&(frac[1]), &end, 10);
224     if (*frac != '.' || end == NULL || end == frac
225         || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
226       fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
227               optarg);
228       exit(1);
229     }
230   }
231   else {
232     return;                     /* no fractional digits */
233   }
234
235   /* adjust fractional portion from fractional to numerator
236    * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
237   if (frac && end) {            /* both are valid */
238     frac_digits = end - frac - 1;   /* fractional digit count (remember '.') */
239     while(frac_digits < 6) {    /* this is frac of 10^6 */
240       val *= 10;
241       frac_digits++;
242     }
243   }
244   time_adj.tv.tv_usec = val;
245 }
246
247 static gboolean
248 is_duplicate(guint8* fd, guint32 len) {
249   int i;
250   md5_state_t ms;
251
252   cur_dup++;
253   if (cur_dup >= DUP_DEPTH)
254     cur_dup = 0;
255
256   /* Calculate our digest */
257   md5_init(&ms);
258   md5_append(&ms, fd, len);
259   md5_finish(&ms, fd_hash[cur_dup].digest);
260
261   fd_hash[cur_dup].len = len;
262
263   /* Look for duplicates */
264   for (i = 0; i < DUP_DEPTH; i++) {
265     if (i == cur_dup)
266       continue;
267
268     if (fd_hash[i].len == fd_hash[cur_dup].len &&
269         memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
270       return TRUE;
271     }
272   }
273
274   return FALSE;
275 }
276
277 static void usage(void)
278 {
279   fprintf(stderr, "Editcap %s"
280 #ifdef SVNVERSION
281           " (" SVNVERSION ")"
282 #endif
283           "\n", VERSION);
284   fprintf(stderr, "Edit and/or translate the format of capture files.\n");
285   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
286   fprintf(stderr, "\n");
287   fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
288   fprintf(stderr, "\n");
289   fprintf(stderr, "A single packet or a range of packets can be selected.\n");
290   fprintf(stderr, "\n");
291   fprintf(stderr, "Packets:\n");
292   fprintf(stderr, "  -C <choplen>           chop each packet at the end by <choplen> bytes\n");
293   fprintf(stderr, "  -d                     remove duplicate packets\n");
294   fprintf(stderr, "  -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
295   fprintf(stderr, "                         that a particular packet byte will be randomly changed\n");
296   fprintf(stderr, "  -r                     keep the selected packets, default is to delete them\n");
297   fprintf(stderr, "  -s <snaplen>           truncate packets to max. <snaplen> bytes of data\n");
298   fprintf(stderr, "  -t <time adjustment>   adjust the timestamp of selected packets,\n");
299   fprintf(stderr, "                         <time adjustment> is in relative seconds (e.g. -0.5)\n");
300   fprintf(stderr, "  -A <start time>        don't output packets whose timestamp is before the\n");
301   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
302   fprintf(stderr, "  -B <stop time>         don't output packets whose timestamp is after the\n");
303   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
304   fprintf(stderr, "\n");
305   fprintf(stderr, "Output File(s):\n");
306   fprintf(stderr, "  -c <packets per file>  split the packet output to different files,\n");
307   fprintf(stderr, "                         with a maximum of <packets per file> each\n");
308   fprintf(stderr, "  -F <capture type>      set the output file type, default is libpcap\n");
309   fprintf(stderr, "                         an empty \"-F\" option will list the file types\n");
310   fprintf(stderr, "  -T <encap type>        set the output file encapsulation type,\n");
311   fprintf(stderr, "                         default is the same as the input file\n");
312   fprintf(stderr, "                         an empty \"-T\" option will list the encapsulation types\n");
313   fprintf(stderr, "\n");
314   fprintf(stderr, "Miscellaneous:\n");
315   fprintf(stderr, "  -h                     display this help and exit\n");
316   fprintf(stderr, "  -v                     verbose output\n");
317   fprintf(stderr, "\n");
318 }
319
320 static void list_capture_types(void) {
321     int i;
322
323     fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
324     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
325       if (wtap_dump_can_open(i))
326         fprintf(stderr, "    %s - %s\n",
327           wtap_file_type_short_string(i), wtap_file_type_string(i));
328     }
329 }
330
331 static void list_encap_types(void) {
332     int i;
333     const char *string;
334
335     fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
336     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
337         string = wtap_encap_short_string(i);
338         if (string != NULL)
339           fprintf(stderr, "    %s - %s\n",
340             string, wtap_encap_string(i));
341     }
342 }
343
344 int main(int argc, char *argv[])
345
346 {
347   wtap *wth;
348   int i, j, err;
349   gchar *err_info;
350   extern char *optarg;
351   extern int optind;
352   int opt;
353   char *p;
354   unsigned int snaplen = 0;             /* No limit               */
355   unsigned int choplen = 0;             /* No chop                */
356   wtap_dumper *pdh;
357   int count = 1;
358   long data_offset;
359   struct wtap_pkthdr snap_phdr;
360   const struct wtap_pkthdr *phdr;
361   int err_type;
362   guint8 *buf;
363   int split_packet_count = 0;
364   int written_count = 0;
365   char *filename;
366
367   /* Process the options first */
368
369   while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
370
371     switch (opt) {
372
373     case 'E':
374       err_prob = strtod(optarg, &p);
375       if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
376         fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
377             optarg);
378         exit(1);
379       }
380       srand(time(NULL) + getpid());
381       break;
382
383     case 'F':
384       out_file_type = wtap_short_string_to_file_type(optarg);
385       if (out_file_type < 0) {
386         fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
387             optarg);
388         list_capture_types();
389         exit(1);
390       }
391       break;
392
393     case 'c':
394       split_packet_count = strtol(optarg, &p, 10);
395       if (p == optarg || *p != '\0') {
396         fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
397             optarg);
398         exit(1);
399       }
400       if (split_packet_count <= 0) {
401         fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
402                 split_packet_count);
403         exit(1);
404       }
405       break;
406
407     case 'C':
408       choplen = strtol(optarg, &p, 10);
409       if (p == optarg || *p != '\0') {
410         fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
411             optarg);
412         exit(1);
413       }
414       break;
415
416     case 'd':
417       dup_detect = TRUE;
418       for (i = 0; i < DUP_DEPTH; i++) {
419         memset(&fd_hash[i].digest, 0, 16);
420         fd_hash[i].len = 0;
421       }
422       break;
423
424     case '?':              /* Bad options if GNU getopt */
425       switch(optopt) {
426       case'F':
427         list_capture_types();
428         break;
429       case'T':
430         list_encap_types();
431         break;
432       default:
433         usage();
434       }
435       exit(1);
436       break;
437
438     case 'h':
439       usage();
440       exit(1);
441       break;
442
443     case 'r':
444       keep_em = !keep_em;  /* Just invert */
445       break;
446
447     case 's':
448       snaplen = strtol(optarg, &p, 10);
449       if (p == optarg || *p != '\0') {
450         fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
451             optarg);
452         exit(1);
453       }
454       break;
455
456     case 't':
457       set_time_adjustment(optarg);
458       break;
459
460     case 'T':
461       out_frame_type = wtap_short_string_to_encap(optarg);
462       if (out_frame_type < 0) {
463         fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
464             optarg);
465         list_encap_types();
466         exit(1);
467       }
468       break;
469
470     case 'v':
471       verbose = !verbose;  /* Just invert */
472       break;
473
474         case 'A':
475         {
476                 struct tm starttm;
477
478                 memset(&starttm,0,sizeof(struct tm));
479                 
480                 if(!strptime(optarg,"%F %T",&starttm)) {
481                         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
482                                         optarg);
483                         exit(1);
484                 }
485
486                 check_startstop = TRUE;
487                 starttm.tm_isdst = -1;
488                 
489                 starttime = mktime(&starttm);
490                 break;
491         }
492         case 'B':
493         {
494                 struct tm stoptm;
495
496                 memset(&stoptm,0,sizeof(struct tm));
497
498                 if(!strptime(optarg,"%F %T",&stoptm)) {
499                         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
500                                         optarg);
501                         exit(1);
502                 }
503                 check_startstop = TRUE;
504                 stoptm.tm_isdst = -1;
505                 stoptime = mktime(&stoptm);
506                 break;
507         }
508     }
509
510   }
511
512 #ifdef DEBUG
513   printf("Optind = %i, argc = %i\n", optind, argc);
514 #endif
515
516   if ((argc - optind) < 1) {
517
518     usage();
519     exit(1);
520
521   }
522
523   if (check_startstop && !stoptime) {
524           struct tm stoptm;
525           /* XXX: will work until 2035 */
526           memset(&stoptm,0,sizeof(struct tm));
527           stoptm.tm_year = 135;
528           stoptm.tm_mday = 31;
529           stoptm.tm_mon = 11;
530
531           stoptime = mktime(&stoptm);
532   }
533
534   if (starttime > stoptime) {
535           fprintf(stderr, "editcap: start time is after the stop time\n");
536           exit(1);
537   }
538
539   wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
540
541   if (!wth) {
542     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
543         wtap_strerror(err));
544     switch (err) {
545
546     case WTAP_ERR_UNSUPPORTED:
547     case WTAP_ERR_UNSUPPORTED_ENCAP:
548     case WTAP_ERR_BAD_RECORD:
549       fprintf(stderr, "(%s)\n", err_info);
550       g_free(err_info);
551       break;
552     }
553     exit(1);
554
555   }
556
557   if (verbose) {
558
559     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
560             wtap_file_type_string(wtap_file_type(wth)));
561
562   }
563
564   /*
565    * Now, process the rest, if any ... we only write if there is an extra
566    * argument or so ...
567    */
568
569   if ((argc - optind) >= 2) {
570
571     if (out_frame_type == -2)
572       out_frame_type = wtap_file_encap(wth);
573
574     if (split_packet_count > 0) {
575       filename = (char *) malloc(strlen(argv[optind+1]) + 20);
576       if (!filename) {
577         exit(5);
578       }
579       sprintf(filename, "%s-%05d", argv[optind+1], 0);
580     } else {
581       filename = argv[optind+1];
582     }
583
584     pdh = wtap_dump_open(filename, out_file_type,
585                          out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
586     if (pdh == NULL) {
587
588       fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
589               wtap_strerror(err));
590       exit(1);
591
592     }
593
594     for (i = optind + 2; i < argc; i++)
595       add_selection(argv[i]);
596
597     while (wtap_read(wth, &err, &err_info, &data_offset)) {
598
599       if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
600         if (!wtap_dump_close(pdh, &err)) {
601
602           fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
603                   wtap_strerror(err));
604           exit(1);
605         }
606
607         sprintf(filename, "%s-%05d",argv[optind+1], count / split_packet_count);
608
609         if (verbose) {
610           fprintf(stderr, "Continuing writing in file %s\n", filename);
611         }
612
613         pdh = wtap_dump_open(filename, out_file_type,
614                              out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
615         if (pdh == NULL) {
616
617           fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
618                   wtap_strerror(err));
619           exit(1);
620
621         }
622       }
623
624       if ( ((check_startstop && check_timestamp(wth)) || (!check_startstop && !check_timestamp(wth))) && ((!selected(count) && !keep_em) ||
625           (selected(count) && keep_em)) ) {
626
627         if (verbose)
628           printf("Packet: %u\n", count);
629
630         /* We simply write it, perhaps after truncating it; we could do other
631            things, like modify it. */
632
633         phdr = wtap_phdr(wth);
634
635         if (choplen != 0 && phdr->caplen > choplen) {
636           snap_phdr = *phdr;
637           snap_phdr.caplen -= choplen;
638           phdr = &snap_phdr;
639         }
640
641         if (snaplen != 0 && phdr->caplen > snaplen) {
642           snap_phdr = *phdr;
643           snap_phdr.caplen = snaplen;
644           phdr = &snap_phdr;
645         }
646
647         /* assume that if the frame's tv_sec is 0, then
648          * the timestamp isn't supported */
649         if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
650           snap_phdr = *phdr;
651           if (time_adj.is_negative)
652             snap_phdr.ts.secs -= time_adj.tv.tv_sec;
653           else
654             snap_phdr.ts.secs += time_adj.tv.tv_sec;
655           phdr = &snap_phdr;
656         }
657
658         /* assume that if the frame's tv_sec is 0, then
659          * the timestamp isn't supported */
660         if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
661           snap_phdr = *phdr;
662           if (time_adj.is_negative) { /* subtract */
663             if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
664               snap_phdr.ts.secs--;
665               snap_phdr.ts.nsecs += ONE_MILLION * 1000;
666             }
667             snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
668           } else {                  /* add */
669             if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
670               /* carry */
671               snap_phdr.ts.secs++;
672               snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
673             } else {
674               snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
675             }
676           }
677           phdr = &snap_phdr;
678         }
679
680         if (dup_detect) {
681           buf = wtap_buf_ptr(wth);
682           if (is_duplicate(buf, phdr->caplen)) {
683             if (verbose)
684               printf("Skipping duplicate: %u\n", count);
685             count++;
686             continue;
687           }
688         }
689
690         if (err_prob > 0.0) {
691           buf = wtap_buf_ptr(wth);
692           for (i = 0; i < (int) phdr->caplen; i++) {
693             if (rand() <= err_prob * RAND_MAX) {
694               err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
695
696               if (err_type < ERR_WT_BIT) {
697                 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
698                 err_type = ERR_WT_TOTAL;
699               } else {
700                 err_type -= ERR_WT_BYTE;
701               }
702
703               if (err_type < ERR_WT_BYTE) {
704                 buf[i] = rand() / (RAND_MAX / 255 + 1);
705                 err_type = ERR_WT_TOTAL;
706               } else {
707                 err_type -= ERR_WT_BYTE;
708               }
709
710               if (err_type < ERR_WT_ALNUM) {
711                 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
712                 err_type = ERR_WT_TOTAL;
713               } else {
714                 err_type -= ERR_WT_ALNUM;
715               }
716
717               if (err_type < ERR_WT_FMT) {
718                 if ((unsigned int)i < phdr->caplen - 2)
719                   strcpy((char*) &buf[i],  "%s");
720                 err_type = ERR_WT_TOTAL;
721               } else {
722                 err_type -= ERR_WT_FMT;
723               }
724
725               if (err_type < ERR_WT_AA) {
726                 for (j = i; j < (int) phdr->caplen; j++) {
727                   buf[j] = 0xAA;
728                 }
729                 i = phdr->caplen;
730               }
731             }
732           }
733         }
734
735         if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
736                        &err)) {
737
738           fprintf(stderr, "editcap: Error writing to %s: %s\n",
739                   filename, wtap_strerror(err));
740           exit(1);
741
742         }
743
744         written_count++;
745
746       }
747
748       count++;
749
750     }
751
752     if (err != 0) {
753       /* Print a message noting that the read failed somewhere along the line. */
754       fprintf(stderr,
755               "editcap: An error occurred while reading \"%s\": %s.\n",
756               argv[optind], wtap_strerror(err));
757       switch (err) {
758
759       case WTAP_ERR_UNSUPPORTED:
760       case WTAP_ERR_UNSUPPORTED_ENCAP:
761       case WTAP_ERR_BAD_RECORD:
762         fprintf(stderr, "(%s)\n", err_info);
763         break;
764       }
765     }
766
767     if (!wtap_dump_close(pdh, &err)) {
768
769       fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
770               wtap_strerror(err));
771       exit(1);
772
773     }
774   }
775
776   return 0;
777 }
778