Give Palmer co-credit for last patch.
[deliverable/binutils-gdb.git] / gdb / xml-support.c
index 76d03b90c707f15fcf43e0313bd4989758accd63..2b59180c92c868868d0b4e380d0ec7347c0511f6 100644 (file)
@@ -998,7 +998,6 @@ xml_fetch_content_from_file (const char *filename, void *baton)
 {
   const char *dirname = (const char *) baton;
   gdb_file_up file;
-  size_t len, offset;
 
   if (dirname && *dirname)
     {
@@ -1015,34 +1014,25 @@ xml_fetch_content_from_file (const char *filename, void *baton)
   if (file == NULL)
     return NULL;
 
-  /* Read in the whole file, one chunk at a time.  */
-  len = 4096;
-  offset = 0;
-  gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len));
-  while (1)
-    {
-      size_t bytes_read;
+  /* Read in the whole file.  */
 
-      /* Continue reading where the last read left off.  Leave at least
-        one byte so that we can NUL-terminate the result.  */
-      bytes_read = fread (text.get () + offset, 1, len - offset - 1,
-                         file.get ());
-      if (ferror (file.get ()))
-       {
-         warning (_("Read error from \"%s\""), filename);
-         return NULL;
-       }
+  size_t len;
 
-      offset += bytes_read;
+  if (fseek (file.get (), 0, SEEK_END) == -1)
+    perror_with_name (_("seek to end of file"));
+  len = ftell (file.get ());
+  rewind (file.get ());
 
-      if (feof (file.get ()))
-       break;
+  gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len + 1));
 
-      len = len * 2;
-      text.reset ((char *) xrealloc (text.get (), len));
+  if (fread (text.get (), 1, len, file.get ()) != len
+      || ferror (file.get ()))
+    {
+      warning (_("Read error from \"%s\""), filename);
+      return NULL;
     }
 
-  text.get ()[offset] = '\0';
+  text.get ()[len] = '\0';
   return text;
 }
 
This page took 0.024031 seconds and 4 git commands to generate.