diff --git a/src/reader.c b/src/reader.c index cec136d..a8b3067 100644 --- a/src/reader.c +++ b/src/reader.c @@ -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 } diff --git a/src/reader_constants.h b/src/reader_constants.h index 810cbc3..9eb86a0 100644 --- a/src/reader_constants.h +++ b/src/reader_constants.h @@ -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