Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to create one simple file utility using the C language-- to display the contents of text files. Following are the specifications: mypager This

You are to create one simple file utility using the C language-- to display the contents of text files.

Following are the specifications:

mypager

This utility displays the contents of a file as ASCII characters, one "page" at at time. By default, a page is equivalent to at most 20 lines of text, each line not having more than 80 characters displayed. It is, essentially, a crude version of the less Unix file utility. Following are the specifications:The usage of mypager is as follows:

mypager 

When started, it displays the first page of text, meaning up to 20 lines of text from the file, each line having 80 characters or less.

It then waits for user input. If the user presses the 'f' key, then the next page is displayed. If the user presses the 'q' key, then the utility quits. This user input code has already been written and is included within the main function.

You will be supplied with an incomplete version of mypager.c that does not actually display any text. You are to modify several functions within mypager.c in order to make it work correctly. Specifically, you must revise three (3) functions, as follows:

fetch_next_line: this function retrieves the next line of text by repeatedly calling fetch_next_word; see mypager.c for more details

fetch_next_word: this function retrieves the next word of text by reading bytes from a buffer; see mypager.c for more details

refill_buffer: this function refills the buffer with bytes read from the file; see mypager.c for more details

You may not revise or add any other functions.

// mypager.c

/* mypager utility

* Prints a file to standard output, one page worth of lines

* at a time. It is designed for text files because it prints

* each byte to the screen as an ASCII character.

* The user controls the output by pressing keys, as follows:

* 'f': forwards to the next page

* 'q': quits

* NOTE: Each keypress is read immediately; the user does not

* press the Enter key. To learn how immediate input mode is

* effectuated, see the eliminate_stdio_buffering() function

* (below) or see 'man termios'.

*/

#include

#include

#include

#include

#include

#include

// preprocessor definitions

#define PAGE_SIZE 20

#define LINE_WIDTH 80

// forward declarations

void display_page();

int fetch_next_line( char line[] );

int fetch_next_word( char word[], int max_size );

int refill_buffer( int start );

void eliminate_stdio_buffering();

void restore_stdio_buffering();

// global variables

int fd;

struct termios old, new;

int usage( char * name )

{

fprintf( stderr, "Usage: " );

fprintf( stderr, "%s ", name );

return 1;

}

/* void display_page()

* Calls fetch_next_line() until either:

* a) all of the lines on a page have been retrieved, or

* b) there are no more lines to retrieve from the file.

*/

void display_page()

{

int number_of_chars;

int number_of_lines;

char line[LINE_WIDTH];

int i;

for( number_of_lines= 0; number_of_lines < PAGE_SIZE;

number_of_lines++ )

{

number_of_chars= fetch_next_line( line );

if ( number_of_chars > 0 )

{

// print chars

fwrite( line, sizeof(char), number_of_chars, stdout );

// print ' ' if not included in line

if ( line[number_of_chars-1] != ' ' )

printf( " " );

}

else // EOF or error

{

if ( number_of_chars == 0 )

printf( "=== EOF === " );

else

printf( "(error reading file) " );

return;

}

}

}

/* int fetch_next_line( char line[] )

* Retrieves the next line of text from the buffer by

* repeatedly calling fetch_next_word().

* Each line breaks at either:

* a) a LF (' ') character, or

* b) the last whitespace encountered at a position that is

* <= (LINE_WIDTH + 1) (Why? Because if a line can contain 80

* characters, but the last space between words occurs at

* character 81, then the line can be broken at character 81.)

* The line of text is stored in the line parameter.

* Returns: the number of characters in the line, or -1 if

* an error occurred.

*/

int fetch_next_line( char line[] )

{

return 0;

}

/* int fetch_next_word( char word[], int max_size )

* Reads bytes from the buffer until the next word is found.

* Punctuation marks count as word characters; thus, the period

* at the end of a sentence is included with the last word.

* If the size of the word, plus trailing whitespace characters,

* is <= (max_size + 1)

* copy the word to the buffer and return the number of chars

* else

* return an error code

* If the end of the buffer is reached while trying to determine

* the next word, this function moves the remaining contents to

* the beginning of the buffer and calls refill_buffer() to

* refill the remainder of the buffer.

* Returns: the number of bytes in the word if successful;

* otherwise, returns the following error values:

* 0 if the EOF is reached;

* -1 if an error occurs; or

* -2 if the word length exceeds the max_size parameter.

*/

int fetch_next_word( char word[], int max_size )

{

return 0;

}

/* int refill_buffer( int start )

* Refills the buffer, starting at the buffer index designated by the

* start parameter, by reading bytes from the file.

* Returns: the number of bytes read if successful; otherwise,

* returns the error value returned from the call to read().

*/

int refill_buffer( int start )

{

return 0;

}

int main( int argc, char * argv[] )

{

// get the first command line argument

// open the file

// wait for commands--

// f forward (next page)

// q quit

if ( argc != 2 )

return usage( argv[0] );

printf( "Opening file %s... ", argv[1] );

fd= open( argv[1], O_RDONLY );

if ( fd == -1 )

{

perror( "open() failed" );

return 1;

}

refill_buffer( 0 );

// set up the terminal to eliminate buffering for stdio

eliminate_stdio_buffering();

char command= 'f'; // triggers display of first page

do

{

switch( command )

{

case 'f':

display_page();

break;

case 'q':

// nothing to do now, but we could modify the code

break;

default:

break;

}

command= (char) getchar();

} while ( command != 'q' );

close( fd );

restore_stdio_buffering();

}

void eliminate_stdio_buffering()

{

tcgetattr( 0, &old );

new= old;

new.c_lflag&= ~ICANON; // disable canonical mode

new.c_lflag&= ~ECHO; // disable input echo

tcsetattr( 0, TCSANOW, &new );

}

void restore_stdio_buffering()

{

tcsetattr( 0, TCSANOW, &old );

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Employ effective vocal cues Employ effective visual cues

Answered: 1 week ago