Question
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:
// 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
// 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
/* 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started