+ return file_stream_new(fileref, fmode, rock, TRUE);
+}
+
+/**
+ * glk_stream_close:
+ * @str: Stream to close.
+ * @result: Pointer to a #stream_result_t, or %NULL.
+ *
+ * Closes the stream @str. The @result argument points to a structure which is
+ * filled in with the final character counts of the stream. If you do not care
+ * about these, you may pass %NULL as the @result argument.
+ *
+ * If @str is the current output stream, the current output stream is set to
+ * %NULL.
+ *
+ * You cannot close window streams; use glk_window_close() instead.
+ */
+void
+glk_stream_close(strid_t str, stream_result_t *result)
+{
+ g_return_if_fail(str != NULL);
+
+ /* Free resources associated with one specific type of stream */
+ switch(str->stream_type)
+ {
+ case STREAM_TYPE_WINDOW:
+ g_warning("%s: Attempted to close a window stream. Use glk_window_"
+ "close() instead.", __func__);
+ return;
+
+ case STREAM_TYPE_MEMORY:
+ /* Do nothing */
+ break;
+
+ case STREAM_TYPE_FILE:
+ if(fclose(str->file_pointer) != 0)
+ g_warning("%s: Failed to close file '%s'.", __func__,
+ str->filename);
+ g_free(str->filename);
+ break;
+ default:
+ g_warning("%s: Closing this type of stream not supported.",
+ __func__);
+ return;
+ }
+
+ /* Remove the stream from the global stream list */
+ stream_list = g_list_delete_link(stream_list, str->stream_list);
+ /* If it was the current output stream, set that to NULL */
+ if(current_stream == str)
+ current_stream = NULL;
+ /* If it was one or more windows' echo streams, set those to NULL */
+ winid_t win;
+ for(win = glk_window_iterate(NULL, NULL); win;
+ win = glk_window_iterate(win, NULL))
+ if(win->echo_stream == str)
+ win->echo_stream = NULL;
+ /* Return the character counts */
+ if(result)
+ {
+ result->readcount = str->read_count;
+ result->writecount = str->write_count;
+ }
+ g_free(str);