From 8570f46734ddba8072e6cbe6fba347cea32acf90 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 3 Aug 2014 21:44:12 +0200 Subject: [PATCH] Indirectly fflush(3) from fshutdown(3). --- libc/include/FILE.h | 1 + libc/stdio/fclose.cpp | 4 +++- libc/stdio/fshutdown.cpp | 2 +- libc/stdio/setvbuf_unlocked.cpp | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libc/include/FILE.h b/libc/include/FILE.h index 594bd52c..601ae8c4 100644 --- a/libc/include/FILE.h +++ b/libc/include/FILE.h @@ -86,6 +86,7 @@ struct FILE void (*free_func)(void* free_user, struct FILE* fp); /* Application writers shouldn't use anything beyond this point. */ pthread_mutex_t file_lock; + int (*fflush_indirect)(FILE*); void (*buffer_free_indirect)(void*); struct FILE* prev; struct FILE* next; diff --git a/libc/stdio/fclose.cpp b/libc/stdio/fclose.cpp index bb5ab92c..293454d9 100644 --- a/libc/stdio/fclose.cpp +++ b/libc/stdio/fclose.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. This file is part of the Sortix C Library. @@ -26,6 +26,8 @@ extern "C" int fclose(FILE* fp) { + flockfile(fp); + funlockfile(fp); int ret = fshutdown(fp); fdeletefile(fp); return ret; diff --git a/libc/stdio/fshutdown.cpp b/libc/stdio/fshutdown.cpp index caf304e0..86af0991 100644 --- a/libc/stdio/fshutdown.cpp +++ b/libc/stdio/fshutdown.cpp @@ -27,7 +27,7 @@ extern "C" int fshutdown(FILE* fp) { - int ret = fflush(fp); + int ret = fp->fflush_indirect ? fp->fflush_indirect(fp) : 0; if ( ret ) { /* TODO: How to report errors here? fclose may need us to return its diff --git a/libc/stdio/setvbuf_unlocked.cpp b/libc/stdio/setvbuf_unlocked.cpp index a5cea6b6..5dbed046 100644 --- a/libc/stdio/setvbuf_unlocked.cpp +++ b/libc/stdio/setvbuf_unlocked.cpp @@ -35,6 +35,7 @@ extern "C" int setvbuf_unlocked(FILE* fp, char* buf, int mode, size_t size) fp->buffer = (unsigned char*) buf; fp->buffersize = size; fp->flags |= _FILE_BUFFER_MODE_SET; + fp->fflush_indirect = fflush; } return 0; }