- /* Expand the result. */
- result = xmalloc (i + special + 1);
- for (i = 0, special = 0; text[i] != '\0'; i++)
- switch (text[i])
- {
- case '\'':
- strcpy (result + i + special, "'");
- special += 5;
- break;
- case '\"':
- strcpy (result + i + special, """);
- special += 5;
- break;
- case '&':
- strcpy (result + i + special, "&");
- special += 4;
- break;
- case '<':
- strcpy (result + i + special, "<");
- special += 3;
- break;
- case '>':
- strcpy (result + i + special, ">");
- special += 3;
- break;
- default:
- result[i + special] = text[i];
+ if (fullname == NULL)
+ malloc_failure (0);
+ file = gdb_fopen_cloexec (fullname, FOPEN_RT);
+ xfree (fullname);
+ }
+ else
+ file = gdb_fopen_cloexec (filename, FOPEN_RT);
+
+ if (file == NULL)
+ return NULL;
+
+ back_to = make_cleanup_fclose (file);
+
+ /* Read in the whole file, one chunk at a time. */
+ len = 4096;
+ offset = 0;
+ text = xmalloc (len);
+ make_cleanup (free_current_contents, &text);
+ while (1)
+ {
+ size_t bytes_read;
+
+ /* 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 + offset, 1, len - offset - 1, file);
+ if (ferror (file))
+ {
+ warning (_("Read error from \"%s\""), filename);
+ do_cleanups (back_to);
+ return NULL;
+ }
+
+ offset += bytes_read;
+
+ if (feof (file))