+/* This function rewinds the requested file back to the line requested,
+ reads it in again into the buffer provided and then restores the file
+ back to its original location. */
+
+static void
+rebuffer_line (file_info_type * file,
+ unsigned int linenum,
+ char * buffer,
+ unsigned int size)
+{
+ unsigned int count = 0;
+ unsigned int current_line;
+ char * p = buffer;
+ long pos;
+ long pos2;
+ int c;
+ bfd_boolean found = FALSE;
+
+ /* Sanity checks. */
+ if (file == NULL || buffer == NULL || size <= 1 || 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. */
+ pos2 = pos = ftell (last_open_file);
+ if (pos < 3)
+ return;
+ current_line = file->linenum;
+
+ /* Leave room for the nul at the end of the buffer. */
+ size -= 1;
+ buffer[size] = 0;
+
+ /* Increment the current line count by one.
+ This is to allow for the fact that we are searching for the
+ start of a previous line, but we do this by detecting end-of-line
+ character(s) not start-of-line characters. */
+ ++ current_line;
+
+ while (pos2 > 0 && ! found)
+ {
+ char * ptr;
+
+ /* Move backwards through the file, looking for earlier lines. */
+ pos2 = (long) size > pos2 ? 0 : pos2 - size;
+ fseek (last_open_file, pos2, SEEK_SET);
+
+ /* Our caller has kindly provided us with a buffer, so we use it. */
+ if (fread (buffer, 1, size, last_open_file) != size)
+ {
+ as_warn (_("unable to rebuffer file: %s\n"), file->filename);
+ return;
+ }
+
+ for (ptr = buffer + size; ptr >= buffer; -- ptr)
+ {
+ if (*ptr == '\n')
+ {
+ -- current_line;
+
+ if (current_line == linenum)
+ {
+ /* We have found the start of the line we seek. */
+ found = TRUE;
+
+ /* FIXME: We could skip the read-in-the-line code
+ below if we know that we already have the whole
+ line in the buffer. */
+
+ /* Advance pos2 to the newline character we have just located. */
+ pos2 += (ptr - buffer);
+
+ /* Skip the newline and, if present, the carriage return. */
+ if (ptr + 1 == buffer + size)
+ {
+ ++pos2;
+ if (fgetc (last_open_file) == '\r')
+ ++ pos2;
+ }
+ else
+ pos2 += (ptr[1] == '\r' ? 2 : 1);
+
+ /* Move the file pointer to this location. */
+ fseek (last_open_file, pos2, SEEK_SET);
+ break;
+ }
+ }
+ }
+ }
+
+ /* Read in the line. */
+ c = fgetc (last_open_file);
+
+ while (c != EOF && c != '\n' && c != '\r')
+ {
+ if (count < size)
+ *p++ = c;
+ count++;