Add error checking for fwrites; replacement for one of patches provided in bug #1864

svn path=/trunk/; revision=22964
This commit is contained in:
Bill Meier 2007-09-25 19:59:50 +00:00
parent 4d4c9a4d34
commit 151a51d956
1 changed files with 38 additions and 12 deletions

View File

@ -570,9 +570,24 @@ int catapult_dct2000_dump_can_write_encap(int encap)
/*****************************************/
/* Write a single packet out to the file */
/*****************************************/
static gboolean do_fwrite(const void *data, size_t size, size_t count, FILE *stream, int *err_p) {
size_t nwritten;
nwritten = fwrite(data, size, count, stream);
if (nwritten != count) {
if (nwritten == 0 && ferror(stream))
*err_p = errno;
else
*err_p = WTAP_ERR_SHORT_WRITE;
return FALSE;
}
return TRUE;
}
gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
const union wtap_pseudo_header *pseudo_header,
const guchar *pd, int *err _U_)
const guchar *pd, int *err)
{
guint32 n;
line_prefix_info_t *prefix = NULL;
@ -591,14 +606,18 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
/* Write out saved first line */
fwrite(file_externals->firstline, 1, file_externals->firstline_length, wdh->fh);
fwrite("\n", 1, 1, wdh->fh);
if (! do_fwrite(file_externals->firstline, 1, file_externals->firstline_length, wdh->fh, err))
return FALSE;
if (! do_fwrite("\n", 1, 1, wdh->fh, err))
return FALSE;
/* Also write out saved second line with timestamp corresponding to the
opening time of the log.
*/
fwrite(file_externals->secondline, 1, file_externals->secondline_length, wdh->fh);
fwrite("\n", 1, 1, wdh->fh);
if (! do_fwrite(file_externals->secondline, 1, file_externals->secondline_length, wdh->fh, err))
return FALSE;
if (! do_fwrite("\n", 1, 1, wdh->fh, err))
return FALSE;
/* Allocate the dct2000-specific dump structure */
wdh->dump.dct2000 = g_malloc(sizeof(catapult_dct2000_t));
@ -622,7 +641,8 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
(const void*)&(pseudo_header->dct2000.seek_off));
/* Write out text before timestamp */
fwrite(prefix->before_time, 1, strlen(prefix->before_time), wdh->fh);
if (! do_fwrite(prefix->before_time, 1, strlen(prefix->before_time), wdh->fh, err))
return FALSE;
/* Calculate time of this packet to write, relative to start of dump */
if (phdr->ts.nsecs >= wdh->dump.dct2000->start_time.nsecs)
@ -639,16 +659,19 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
}
/* Write out the calculated timestamp */
fwrite(time_string, 1, strlen(time_string), wdh->fh);
if (! do_fwrite(time_string, 1, strlen(time_string), wdh->fh, err))
return FALSE;
/* Write out text between timestamp and start of hex data */
if (prefix->after_time == NULL)
{
fwrite(" l ", 1, strlen(" l "), wdh->fh);
if (! do_fwrite(" l ", 1, strlen(" l "), wdh->fh, err))
return FALSE;
}
else
{
fwrite(prefix->after_time, 1, strlen(prefix->after_time), wdh->fh);
if (! do_fwrite(prefix->after_time, 1, strlen(prefix->after_time), wdh->fh, err))
return FALSE;
}
@ -684,7 +707,8 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
/**************************************/
/* Remainder is encapsulated protocol */
fwrite("$", 1, 1, wdh->fh);
if (! do_fwrite("$", 1, 1, wdh->fh, err))
return FALSE;
/* Each binary byte is written out as 2 hex string chars */
for (; n < phdr->len; n++)
@ -694,11 +718,13 @@ gboolean catapult_dct2000_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
c[1] = char_from_hex((guchar)(pd[n] & 0x0f));
/* Write both hex chars of byte together */
fwrite(c, 1, 2, wdh->fh);
if (! do_fwrite(c, 1, 2, wdh->fh, err))
return FALSE;
}
/* End the line */
fwrite("\n", 1, 1, wdh->fh);
if (! do_fwrite("\n", 1, 1, wdh->fh, err))
return FALSE;
return TRUE;
}