From dd98856afce144eb19104a6f40c1abedc9069558 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sat, 3 Dec 2016 17:57:34 -0800 Subject: Have separate merge APIs for regular file/temporary file/standard output. This is similar to what we have for opening a dump file - one API that uses the file name as specified, one that creates a temporary file and provides the file name, and one that uses the standard output. All of those APIs handle closing the output file. Change-Id: I56beea7be347402773460b9148ab31a8f8bc51e1 Reviewed-on: https://code.wireshark.org/review/19059 Reviewed-by: Guy Harris --- mergecap.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'mergecap.c') diff --git a/mergecap.c b/mergecap.c index 94fb70b7f0..c64341dd3b 100644 --- a/mergecap.c +++ b/mergecap.c @@ -257,7 +257,6 @@ main(int argc, char *argv[]) #else int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */ #endif - int out_fd; int err = 0; gchar *err_info = NULL; int err_fileno; @@ -428,25 +427,21 @@ main(int argc, char *argv[]) /* open the outfile */ if (strcmp(out_filename, "-") == 0) { - /* use stdout as the outfile */ + /* merge the files to the standard output */ use_stdout = TRUE; - out_fd = 1 /*stdout*/; + status = merge_files_to_stdout(file_type, + (const char *const *) &argv[optind], + in_file_count, do_append, mode, snaplen, + "mergecap", verbose ? &cb : NULL, + &err, &err_info, &err_fileno); } else { - /* open the outfile */ - out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); - if (out_fd == -1) { - fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n", - out_filename, g_strerror(errno)); - exit(1); - } + /* merge the files to the outfile */ + status = merge_files(out_filename, file_type, + (const char *const *) &argv[optind], in_file_count, + do_append, mode, snaplen, "mergecap", verbose ? &cb : NULL, + &err, &err_info, &err_fileno); } - /* merge the files */ - status = merge_files(out_fd, out_filename, file_type, - (const char *const *) &argv[optind], in_file_count, - do_append, mode, snaplen, "mergecap", verbose ? &cb : NULL, - &err, &err_info, &err_fileno); - switch (status) { case MERGE_OK: break; @@ -462,10 +457,13 @@ main(int argc, char *argv[]) break; case MERGE_ERR_CANT_OPEN_OUTFILE: - fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename, - wtap_strerror(err)); - if (!use_stdout) - ws_close(out_fd); + if (use_stdout) { + fprintf(stderr, "mergecap: Can't set up the standard output: %s\n", + wtap_strerror(err)); + } else { + fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename, + wtap_strerror(err)); + } break; case MERGE_ERR_CANT_READ_INFILE: /* fall through */ -- cgit v1.2.1