Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #38, Handle lseek error. #39

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions cfe_ts_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,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;
int done = 0;
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,10 +126,16 @@ int main(int argc, char **argv)
if (fd < 0)
{
printf("\ncfe_ts_crc error: can't open input file!\n");
exit(0);
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(offsetReturn));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strerror() takes an errno, not the return code (the latter is -1 by definition).

So it should be strerror(errno) ... OR just use perror() routine instead. That just simplifies when you want to print the current errno.

We should also do this EVERY time we do exit(1) - not just here.

I do recommend using this function when all you want to do is print the error in the simplest way possible. It's quick and easy. See: https://man7.org/linux/man-pages/man3/perror.3p.html

exit(1);
}

/* read the input file 100 bytes at a time */
while (done == 0)
Expand All @@ -140,15 +147,15 @@ int main(int argc, char **argv)
done = 1;
}
/* 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);
exit(1);
}

return (fileCRC);
Expand Down