From fb29ff42e0f9a0b2bf9414588cf8bae6fc5e24c9 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 21 May 2015 21:36:01 +0200 Subject: [PATCH] Optimize puts and fputs. --- libc/stdio/fputs_unlocked.cpp | 7 +++---- libc/stdio/puts.cpp | 12 ++++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/libc/stdio/fputs_unlocked.cpp b/libc/stdio/fputs_unlocked.cpp index aad83945..777353ad 100644 --- a/libc/stdio/fputs_unlocked.cpp +++ b/libc/stdio/fputs_unlocked.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015. This file is part of the Sortix C Library. @@ -28,8 +28,7 @@ extern "C" int fputs_unlocked(const char* str, FILE* fp) { size_t stringlen = strlen(str); - size_t result = fwrite_unlocked(str, 1, stringlen, fp); - if ( result < stringlen ) + if ( fwrite_unlocked(str, 1, stringlen, fp) < stringlen ) return EOF; - return result; + return 0; } diff --git a/libc/stdio/puts.cpp b/libc/stdio/puts.cpp index 84f9b6a1..5027e67d 100644 --- a/libc/stdio/puts.cpp +++ b/libc/stdio/puts.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2014. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2014, 2015. This file is part of the Sortix C Library. @@ -23,8 +23,16 @@ *******************************************************************************/ #include +#include extern "C" int puts(const char* str) { - return printf("%s\n", str) < 0 ? EOF : 1; + int ret = 0; + flockfile(stdout); + size_t stringlen = strlen(str); + if ( fwrite_unlocked(str, 1, stringlen, stdout) < stringlen || + fputc_unlocked('\n', stdout) == EOF ) + ret = EOF; + funlockfile(stdout); + return ret; }