- struct temp_file *tf = xmalloc (sizeof *tf);
- tf->file = tmpfile ();
- if (tf->file == NULL)
- error (0, errno, _("failed to create temporary file"));
- tf->position = 0;
- return tf;
-}
-
-/* Closes and destroys temporary file TF. Returns true if I/O on
- TF always succeeded, false if an I/O error occurred at some
- point. */
-bool
-temp_file_destroy (struct temp_file *tf)
-{
- bool ok = true;
- if (tf != NULL)
- {
- ok = !temp_file_error (tf);
- if (tf->file != NULL)
- fclose (tf->file);
- free (tf);
- }
- return ok;
-}
-
-/* Seeks TF's underlying file to the start of `union value'
- VALUE_IDX within case CASE_IDX.
- Returns true if the seek is successful and TF is not
- otherwise tainted, false otherwise. */
-static bool
-do_seek (const struct temp_file *tf_, off_t offset)
-{
- struct temp_file *tf = CONST_CAST (struct temp_file *, tf_);
-
- if (!temp_file_error (tf))
- {
- if (tf->position == offset)
- return true;
- else if (fseeko (tf->file, offset, SEEK_SET) == 0)
- {
- tf->position = offset;
- return true;
- }
- else
- error (0, errno, _("seeking in temporary file"));
- }
-
- return false;
-}
-
-/* Reads BYTES bytes from TF's underlying file into BUFFER.
- TF must not be tainted upon entry into this function.
- Returns true if successful, false upon an I/O error (in which
- case TF is marked tainted). */
-static bool
-do_read (const struct temp_file *tf_, void *buffer, size_t bytes)
-{
- struct temp_file *tf = CONST_CAST (struct temp_file *, tf_);
-
- assert (!temp_file_error (tf));
- if (bytes > 0 && fread (buffer, bytes, 1, tf->file) != 1)
- {
- if (ferror (tf->file))
- error (0, errno, _("reading temporary file"));
- else if (feof (tf->file))
- error (0, 0, _("unexpected end of file reading temporary file"));
- else
- NOT_REACHED ();
- return false;
- }
- tf->position += bytes;
- return true;
-}