+ /* Sanity checks. */
+ if (file == NULL || buffer == NULL || size == 0 || file->linenum <= linenum)
+ return "";
+
+ /* Check the cache and see if we last used this file. */
+ if (last_open_file_info == NULL || file != last_open_file_info)
+ {
+ if (last_open_file)
+ {
+ last_open_file_info->pos = ftell (last_open_file);
+ fclose (last_open_file);
+ }
+
+ /* Open the file in the binary mode so that ftell above can
+ return a reliable value that we can feed to fseek below. */
+ last_open_file_info = file;
+ last_open_file = fopen (file->filename, FOPEN_RB);
+ if (last_open_file == NULL)
+ {
+ file->at_end = 1;
+ return "";
+ }
+
+ /* Seek to where we were last time this file was open. */
+ if (file->pos)
+ fseek (last_open_file, file->pos, SEEK_SET);
+ }
+
+ /* Remember where we are in the current file. */
+ pos = ftell (last_open_file);
+
+ /* Go back to the beginning. */
+ fseek (last_open_file, 0, SEEK_SET);
+
+ /* Skip lines prior to the one we are interested in. */
+ while (current_line < linenum)
+ {
+ /* fgets only stops on newlines and has a size limit,
+ so we read one character at a time instead. */
+ do
+ {
+ c = fgetc (last_open_file);
+ }
+ while (c != EOF && c != '\n' && c != '\r');
+
+ ++ current_line;
+
+ if (c == '\r' || c == '\n')
+ {
+ int next = fgetc (last_open_file);
+
+ /* If '\r' is followed by '\n', swallow that. Likewise, if '\n'
+ is followed by '\r', swallow that as well. */
+ if ((c == '\r' && next != '\n')
+ || (c == '\n' && next != '\r'))
+ ungetc (next, last_open_file);
+ }
+ }
+
+ /* Leave room for the nul at the end of the buffer. */
+ size -= 1;
+
+ /* Read in the line. */
+ c = fgetc (last_open_file);
+
+ while (c != EOF && c != '\n' && c != '\r')
+ {
+ if (count < size)
+ *p++ = c;
+ count++;
+
+ c = fgetc (last_open_file);
+ }
+
+ /* If '\r' is followed by '\n', swallow that. Likewise, if '\n'
+ is followed by '\r', swallow that as well. */
+ if (c == '\r' || c == '\n')
+ {
+ int next = fgetc (last_open_file);
+
+ if ((c == '\r' && next != '\n')
+ || (c == '\n' && next != '\r'))
+ ungetc (next, last_open_file);
+ }
+
+ /* Terminate the line. */
+ *p++ = 0;
+
+ /* Reset the file position. */
+ fseek (last_open_file, pos, SEEK_SET);
+
+ return buffer;
+}
+
+static const char *fn;
+
+static unsigned int eject; /* Eject pending */
+static unsigned int page; /* Current page number */
+static char *title; /* Current title */
+static char *subtitle; /* Current subtitle */
+static unsigned int on_page; /* Number of lines printed on current page */