#include <stdio.h>
#include <stdlib.h>

#define LINES   21                  /* lines to display + 1     */
#define MAXLEN  134                 /* maximum length of a line */

char lines[LINES][MAXLEN];

int main( int argc, char *argv[] )
    {
    int  chr, status, curr, index;
    FILE *input;

    if ( argc != 2 )
        {
        fprintf( stderr, "\n\nUSE: tail <pathname>\n" );
        exit( 2 );
        }

    if ( ( input = fopen( argv[1], "rb" ) ) == NULL )
        status = 1;
    else
        {
        status = fseek( input, 0L, SEEK_END );
                
        if ( status == 0 )
            {
            status = fseek( input, (long)LINES * (long)MAXLEN * -1L, SEEK_CUR );
            
            if ( ftell( input ) < 0 )
                status = fseek( input, 0L, SEEK_SET );
            }
        }

    if ( status != 0 )
        {
        fprintf( stderr, "\n\nError during processing of input file\n" );
        exit( 2 );
        }

    for ( curr = 0; curr < LINES; curr++ );
        lines[curr][0] = '\0';
        
    curr = 0;        

    while ( fgets( lines[curr], MAXLEN, input ) != NULL )
        curr = ( curr + 1 ) % LINES;

    lines[curr][0] = '\0';    
    curr = ( curr + 1 ) % LINES;

    for ( index = 0; index < LINES; index++ )
        {
        fputs( lines[curr], stdout );
        curr = ( curr + 1 ) % LINES;
        }
    
    exit( 0 );
    }
