Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write in C: // mypager.c /* mypager utility * Prints a file to standard output, one page worth of lines * at a time. It

Write in C:

image text in transcribed

// 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 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 *

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

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

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: 1. The usage of mypager is as follows: mypager 2. 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 3. 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. 4. 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: this function retrieves the next word of text by reading bytes from a buffer; see - refill_buffer: this function refills the buffer with bytes read from the file; see mypager.c for more fetch_next_word; see mypager.c for more details mypager.c for more details details 5. You may not revise or add any other functions. 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: 1. The usage of mypager is as follows: mypager 2. 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 3. 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. 4. 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: this function retrieves the next word of text by reading bytes from a buffer; see - refill_buffer: this function refills the buffer with bytes read from the file; see mypager.c for more fetch_next_word; see mypager.c for more details mypager.c for more details details 5. You may not revise or add any other functions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions