\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage[left=1in,right=1in,top=0.75in,bottom=0.5in]{geometry}
\usepackage{url}
\usepackage{amsmath}
\usepackage{upquote}
\pagestyle{myheadings}
\markright{MAT 4370: Programming Assignment 9}

\long\def\symbolfootnote[#1]#2{\begingroup%
\def\thefootnote{\fnsymbol{footnote}}\footnote[#1]{#2}\endgroup} 

\begin{document}
\thispagestyle{empty}
\begin{center}
  {\Large MAT 4370: Programming Assignment 9}\\
  Due: Wednesday, March 6
\end{center}

\section*{The Problem}
For the previous homework exercise, you were asked to implement a C
function that would read a line of characters from a \verb!FILE!
stream, storing it in a character array.  The prototype for this
function was:
\begin{quote}
\verb!char *readLine(char *s, int n, FILE *fp, bool *overflow);!
\end{quote}
You can find an implementation of this function in the file
\verb!readfile4.c! on the course website. (See the ``Code Examples''
section, under Homework~8.)

Although correct, this implementation has some shortcomings, including
the following:

\begin{itemize}
 \item The user of this function must be able to estimate a maximum
   line length.
 \item Characters are truncated if the maximum line length is too
   small.
 \item The number of parameters to this function makes it difficult to
   understand.  (As a general rule, as the number of parameters
   increases, it is often more difficult.  Imagine a function with
   twenty parameters!)
\end{itemize}

For this exercise, we will re-implement \verb!readLine()! in a way
which removes these shortcomings.

\section*{What to Do}
\begin{enumerate}
 \item Re-read the sections in our book which describe \verb!malloc()!
   and \verb!realloc()!.  Implement ``safe'' versions of each of
   these.  The idea is that if either memory allocation or
   reallocation fails we want the program to stop.
   (See the solution \verb!pmsort.c! to see how to do this.)

 \item Re-implement \verb!readLine!, using the following prototype:
\begin{quote}
\verb!char *readLine(FILE *fp);!
\end{quote}
Here's the result we want from the revised \verb!readLine()!.  If the
given file pointer is at the end of file, then \verb!readLine()! should
return \verb!NULL!.  Otherwise, it should allocate just the right
amount of memory---no more and no less---to store the next line of input.  All
characters up to and including the newline should be read and
consumed; everything except the terminating newline should be stored in the
allocated memory.  As in the previous implementation, the string of
characters should be terminated with the null character.

If there is not enough space available to store such a string,
\verb!readline()! should abort the program.

For efficiency, use the doubling method of allocation discussed in
class.  Also, for debugging purposes, your function should display the
number of bytes allocated or reallocated for the character string.
This should be output immediately before the function is to return.

Demonstrate your function by using a modified version of \verb!readfile4.c!.

\end{enumerate}


\section*{What to Submit}
When your sort implementation works to your satisfaction,
place it in a folder named {\tt hw09}.  (Source code only please; no
other files need to be submitted.) Drag this
folder onto the SUBMIT icon, found in the Applications folder.


\end{document}
