Skip to content

Commit

Permalink
bug/error fixes (DON'T SHOW THIS TO OUR PROFESSOR)
Browse files Browse the repository at this point in the history
  • Loading branch information
iluxonchik committed Nov 15, 2014
1 parent 8441dbc commit ae63e69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,36 @@ int file_contents_are_valid(int fd, int line_length, int first_line, int last_li

lseek( fd, first_line * line_size, SEEK_SET );

// compare all lines with the first line
// compare all lines with the first line of the file, except the last one
int i;
for ( i = 0 ; read(fd, line_buffer, line_size) == line_size; i++ )
for ( i = 0 ; i < line_count && read(fd, line_buffer, line_size) == line_size; i++ )
{
if ( strncmp(line_buffer, first_line_buf, line_length) != 0 || i >= line_count ){
if ( strncmp(line_buffer, first_line_buf, line_length) != 0 ) {
DBG_PRINTF( "invalid file detected here; fd=%d, first_line=%d, i=%d\n",
fd, first_line, i );
free(first_line_buf);
free(line_buffer);
DBG_PRINTF("fd=%d, invalid file detected here; first_line=%d, i=%d\nline_buff='%s'; first_line_buf='%s'\n",
fd,first_line, i, line_buffer, first_line_buf);
return FALSE; // file is invalid
}
}

free(first_line_buf);
free(line_buffer);

if (i != line_count) {
// did we read all expected lines?
if( i < line_count ) {
DBG_PRINT("invalid file detected here\n");
return FALSE;
}

// is the file longer than expected?
if ( (first_line + i) >= LINES_PER_FILE ) { // we are in the last line of the file
if( read(fd, line_buffer, 1) > 0 ) { // file is longer than expected
DBG_PRINT("invalid file detected here\n");
return FALSE;
}
}

free(first_line_buf);
free(line_buffer);

return TRUE; // file is valid
}

Expand Down
2 changes: 1 addition & 1 deletion src/reader_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define READER_2_THREAD_COUNT 3

/* Constant K for Exercise 4 pdf */
#define READER_3_THREAD_COUNT 2
#define READER_3_THREAD_COUNT 7

#define FILE_IS_VALID 0
#define FILE_IS_INVALID -1
Expand Down

0 comments on commit ae63e69

Please sign in to comment.