Skip to content

Commit

Permalink
Fix #36 #38 #40 #41
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 5, 2021
1 parent b02864b commit 558b20c
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions cfe_ts_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Inputs: One string containing the filename of the table file to CRC.
*
* Outputs: Prints to the terminal the filename, size, and CRC.
* Returns the CRC.
* Returns 0 if successful.
*
* Author: Mike Blau, GSFC Code 582
*/
Expand All @@ -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,20 @@ 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);
exit(1);
}
/* Set to skip the header (116 bytes) */
skipSize = sizeof(CFE_FS_Header_t) + sizeof(CFE_TBL_File_Hdr_t);
Expand All @@ -125,31 +126,44 @@ int main(int argc, char **argv)
if (fd < 0)
{
printf("\ncfe_ts_crc error: can't open input file!\n");
exit(0);
perror(argv[1]);
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);
readSize = read(fd, buffer, sizeof(buffer));
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 > 0);

/* 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);
return (0);
}

0 comments on commit 558b20c

Please sign in to comment.