aboutsummaryrefslogtreecommitdiffstats
path: root/text2pcap.c
diff options
context:
space:
mode:
authorwmeier <wmeier@f5534014-38df-0310-8fa8-9805f1628bb7>2011-08-10 17:15:08 +0000
committerwmeier <wmeier@f5534014-38df-0310-8fa8-9805f1628bb7>2011-08-10 17:15:08 +0000
commit0b7cfac1a14b3a67308970c3fe94a3a4cbfcfb07 (patch)
tree53af9a5c125c63ee49ee78726e29436776b56a5d /text2pcap.c
parent8ae47f2ce8fe3e7985c6b81049878252463809a6 (diff)
Test return value from fwrite(); If an error occurred then exit with a message.
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@38449 f5534014-38df-0310-8fa8-9805f1628bb7
Diffstat (limited to 'text2pcap.c')
-rw-r--r--text2pcap.c41
1 files changed, 31 insertions, 10 deletions
diff --git a/text2pcap.c b/text2pcap.c
index c2d5f7f532..aaff6b7336 100644
--- a/text2pcap.c
+++ b/text2pcap.c
@@ -577,12 +577,14 @@ write_current_packet (void)
if (ts_fmt == NULL) { ts_usec++; } /* fake packet counter */
ph.incl_len = length;
ph.orig_len = length;
- fwrite(&ph, sizeof(ph), 1, output_file);
+ if (fwrite(&ph, sizeof(ph), 1, output_file) != 1)
+ goto write_current_packet_err;
/* Write Ethernet header */
if (hdr_ethernet) {
HDR_ETHERNET.l3pid = g_htons(hdr_ethernet_proto);
- fwrite(&HDR_ETHERNET, sizeof(HDR_ETHERNET), 1, output_file);
+ if (fwrite(&HDR_ETHERNET, sizeof(HDR_ETHERNET), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* Write IP header */
@@ -591,7 +593,8 @@ write_current_packet (void)
HDR_IP.protocol = (guint8) hdr_ip_proto;
HDR_IP.hdr_checksum = 0;
HDR_IP.hdr_checksum = in_checksum(&HDR_IP, sizeof(HDR_IP));
- fwrite(&HDR_IP, sizeof(HDR_IP), 1, output_file);
+ if (fwrite(&HDR_IP, sizeof(HDR_IP), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* initialize pseudo header for checksum calculation */
@@ -621,7 +624,8 @@ write_current_packet (void)
if (HDR_UDP.checksum == 0) /* differentiate between 'none' and 0 */
HDR_UDP.checksum = g_htons(1);
- fwrite(&HDR_UDP, sizeof(HDR_UDP), 1, output_file);
+ if (fwrite(&HDR_UDP, sizeof(HDR_UDP), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* Write TCP header */
@@ -645,7 +649,8 @@ write_current_packet (void)
if (HDR_TCP.checksum == 0) /* differentiate between 'none' and 0 */
HDR_TCP.checksum = g_htons(1);
- fwrite(&HDR_TCP, sizeof(HDR_TCP), 1, output_file);
+ if (fwrite(&HDR_TCP, sizeof(HDR_TCP), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* Compute DATA chunk header and append padding */
@@ -677,20 +682,24 @@ write_current_packet (void)
x32 = finalize_crc32c(crc32c(packet_buf, curr_offset, HDR_SCTP.checksum));
HDR_SCTP.checksum = g_htonl(x32);
- fwrite(&HDR_SCTP, sizeof(HDR_SCTP), 1, output_file);
+ if (fwrite(&HDR_SCTP, sizeof(HDR_SCTP), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* Write DATA chunk header */
if (hdr_data_chunk) {
- fwrite(&HDR_DATA_CHUNK, sizeof(HDR_DATA_CHUNK), 1, output_file);
+ if (fwrite(&HDR_DATA_CHUNK, sizeof(HDR_DATA_CHUNK), 1, output_file) != 1)
+ goto write_current_packet_err;
}
/* Write packet */
- fwrite(packet_buf, curr_offset, 1, output_file);
+ if (fwrite(packet_buf, curr_offset, 1, output_file) != 1)
+ goto write_current_packet_err;
/* Write Ethernet trailer */
if (hdr_ethernet && eth_trailer_length > 0) {
memset(tempbuf, 0, eth_trailer_length);
- fwrite(tempbuf, eth_trailer_length, 1, output_file);
+ if (fwrite(tempbuf, eth_trailer_length, 1, output_file) != 1)
+ goto write_current_packet_err;
}
if (!quiet)
@@ -702,6 +711,12 @@ write_current_packet (void)
packet_start += curr_offset;
curr_offset = 0;
+ return;
+
+write_current_packet_err:
+ fprintf(stderr, "File write error [%s] : %s\n",
+ output_filename, g_strerror(errno));
+ exit(-1);
}
/*----------------------------------------------------------------------
@@ -720,7 +735,11 @@ write_file_header (void)
fh.snaplen = 102400;
fh.network = pcap_link_type;
- fwrite(&fh, sizeof(fh), 1, output_file);
+ if (fwrite(&fh, sizeof(fh), 1, output_file) != 1) {
+ fprintf(stderr, "File write error [%s] : %s\n",
+ output_filename, g_strerror(errno));
+ exit(-1);
+ }
}
/*----------------------------------------------------------------------
@@ -1389,6 +1408,8 @@ main(int argc, char *argv[])
yylex();
write_current_packet();
+ fclose(input_file);
+ fclose(output_file);
if (debug)
fprintf(stderr, "\n-------------------------\n");
if (!quiet) {