+ count = 1;
+ for (;;) {
+ if (do_append)
+ in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
+ &err_info);
+ else
+ in_file = merge_read_packet(in_file_count, in_files, &read_err,
+ &err_info);
+ if (in_file == NULL) {
+ /* EOF */
+ break;
+ }
+
+ if (read_err != 0) {
+ /* I/O error reading from in_file */
+ got_read_error = TRUE;
+ break;
+ }
+
+ if (verbose)
+ fprintf(stderr, "Record: %u\n", count++);
+
+ /* We simply write it, perhaps after truncating it; we could do other
+ * things, like modify it. */
+ phdr = wtap_phdr(in_file->wth);
+ if (snaplen != 0 && phdr->caplen > snaplen) {
+ snap_phdr = *phdr;
+ snap_phdr.caplen = snaplen;
+ phdr = &snap_phdr;
+ }
+
+ if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) {
+ got_write_error = TRUE;
+ break;
+ }
+ }
+
+ merge_close_in_files(in_file_count, in_files);
+ if (!got_write_error) {
+ if (!wtap_dump_close(pdh, &write_err))
+ got_write_error = TRUE;
+ } else {
+ /*
+ * We already got a write error; no need to report another
+ * write error on close.
+ *
+ * Don't overwrite the earlier write error.
+ */
+ (void)wtap_dump_close(pdh, &close_err);
+ }
+
+ if (got_read_error) {
+ /*
+ * Find the file on which we got the error, and report the error.
+ */
+ for (i = 0; i < in_file_count; i++) {
+ if (in_files[i].state == GOT_ERROR) {
+ fprintf(stderr, "mergecap: Error reading %s: %s\n",
+ in_files[i].filename, wtap_strerror(read_err));
+ if (err_info != NULL) {
+ fprintf(stderr, "(%s)\n", err_info);
+ g_free(err_info);
+ }
+ }
+ }
+ }
+
+ if (got_write_error) {
+ switch (write_err) {
+
+ case WTAP_ERR_UNWRITABLE_ENCAP:
+ /*
+ * This is a problem with the particular frame we're writing and
+ * the file type and subtype we're wwriting; note that, and
+ * report the frame number and file type/subtype.
+ */
+ fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n",
+ in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
+ wtap_file_type_subtype_string(file_type));
+ break;
+
+ case WTAP_ERR_PACKET_TOO_LARGE:
+ /*
+ * This is a problem with the particular frame we're writing and
+ * the file type and subtype we're wwriting; note that, and
+ * report the frame number and file type/subtype.
+ */
+ fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
+ in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
+ wtap_file_type_subtype_string(file_type));
+ break;
+
+ case WTAP_ERR_UNWRITABLE_REC_TYPE:
+ /*
+ * This is a problem with the particular record we're writing and
+ * the file type and subtype we're wwriting; note that, and
+ * report the record number and file type/subtype.
+ */
+ fprintf(stderr, "mergecap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
+ in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
+ wtap_file_type_subtype_string(file_type));
+ break;
+
+ case WTAP_ERR_UNWRITABLE_REC_DATA:
+ /*
+ * This is a problem with the particular record we're writing and
+ * the file type and subtype we're wwriting; note that, and
+ * report the record number and file type/subtype.
+ */
+ fprintf(stderr, "mergecap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
+ in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
+ wtap_file_type_subtype_string(file_type),
+ write_err_info != NULL ? write_err_info : "no information supplied");
+ g_free(write_err_info);
+ break;