/*
 * Problem 1-16
 * Revise the main routine of the longest-line program so it 
 * it will correctly print the length of arbitrarily long 
 * input lines, and as much as possible of the text.
 * Copyright (c) 1998 by John Weber.  All rights reserved.
 */
#include <stdio.h>
#define MAXLINE 1000		/* maximum input line size */

int getline(char line[], int maxline);
void copy(char to[], char from[], int len);

/* print longest input line */
int main(void)
{
	int len;		/* current line length */
	int max;		/* maximum length seen so far */
	char line[MAXLINE];	/* current input line */
	char longest[MAXLINE];	/* longest line saved here */
	
	max = 0;
	while( (len += getline(line,MAXLINE)) > 0 ) {
		if( len > max ) {
			max = len;
			copy(longest, line);
		}
		if( line[len-1] == '\n') /* line is a valid line */
			len = 0;
	}
	if ( max > 0 ) {	/* there was a line */
		printf("Longest Line\n------------\n");
		printf("%s\n",longest);
		printf("Line Length\n-----------\n");
		printf("%d characters\n",max);
	}
	return 0;
}

int getline(char s[])
{
	int c,i = 0;
	
	for(i=0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) 
		s[i] = c;
	if( c == '\n' ) { s[i] = c; ++i; }
	s[i] = '\0';
	return i;
}

void copy(char to[], char from[])
{
	int i;

	i = 0;
	while( (to[i] = from[i]) != '\0' ) ++i;
}
