Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
36: Handle read failure
38: handle lseek error
40: use exit (1)
41: fix EOF check
  • Loading branch information
zanzaben committed Mar 4, 2021
1 parent b02864b commit 96bbc6b
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions cfe_ts_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "cfe_ts_crc_version.h"

Expand Down Expand Up @@ -102,20 +103,21 @@ uint32 CalculateCRC(void *DataPtr, uint32 DataLength, uint32 InputCRC)

int main(int argc, char **argv)
{
int readSize;
int skipSize = 0;
int fileSize = 0;
uint32 fileCRC = 0;
int fd;
int done = 0;
char buffer[100];
ssize_t readSize;
off_t skipSize = 0;
ssize_t fileSize = 0;
uint32 fileCRC = 0;
int fd;
char buffer[100];
off_t offsetReturn = 0;

/* check for valid input */
if ((argc != 2) || (strncmp(argv[1], "--help", 100) == 0))
{
printf("%s\n", CFE_TS_CRC_VERSION_STRING);
printf("\nUsage: cfe_ts_crc [filename]\n");
exit(0);
printf("%s\n", strerror(errno));
exit(1);
}
/* Set to skip the header (116 bytes) */
skipSize = sizeof(CFE_FS_Header_t) + sizeof(CFE_TBL_File_Hdr_t);
Expand All @@ -125,30 +127,43 @@ int main(int argc, char **argv)
if (fd < 0)
{
printf("\ncfe_ts_crc error: can't open input file!\n");
exit(0);
printf("%s\n", strerror(errno));
exit(1);
}
/* seek past the number of bytes requested */
lseek(fd, skipSize, SEEK_SET);
offsetReturn = lseek(fd, skipSize, SEEK_SET);
if (offsetReturn != skipSize)
{
printf("\ncfe_ts_crc error: lseek failed!\n");
printf("%s\n", strerror(errno));
exit(1);
}

/* read the input file 100 bytes at a time */
while (done == 0)
do
{
readSize = read(fd, buffer, 100);
fileCRC = CalculateCRC(buffer, readSize, fileCRC);
if (readSize < 0)
{
printf("\ncfe_ts_crc error: file read failed!\n");
printf("%s\n", strerror(errno));
exit(1);
}
fileCRC = CalculateCRC(buffer, readSize, fileCRC);
fileSize += readSize;
if (readSize != 100)
done = 1;
}
} while (readSize != 100);

/* print the size/CRC results */
printf("\nTable File Name: %s\nTable Size: %d Bytes\nExpected TS Validation CRC: "
printf("\nTable File Name: %s\nTable Size: %ld Bytes\nExpected TS Validation CRC: "
"0x%08X\n\n",
argv[1], fileSize, fileCRC);

/* Close file and check*/
if (close(fd) != 0)
{
printf("\nerror: Cannot close file!\n");
exit(0);
printf("%s\n", strerror(errno));
exit(1);
}

return (fileCRC);
Expand Down

0 comments on commit 96bbc6b

Please sign in to comment.