From 1bb64ec48f17a1932f86840c97a3e3956d84bd4f Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 28 Jul 2019 13:01:37 +0200 Subject: [PATCH] mescc: Mes C Library: Have write behave well with buffered reads. * lib/mes/write.c: New file. * build-aux/configure-lib.sh (libc_mini_SOURCES): Add it. (libc_mini_shared_SOURCES): New variable. (libc_mini_SOURCES, libmes_SOURCES): Use it. (libmes_SOURCES): Add lib/linux/lseek.c, lib/posix/write.c. (libc_tcc_SOURCES): Remove lib/linux/lseek.c. * lib/posix/write.c (write): Correct for read buffer. * lib/stdio/fwrite.c (fwrite): Remove read buffer correction. * simple.sh: Update. --- build-aux/configure-lib.sh | 40 +++++++++++++++++++++++++------------- lib/mes/write.c | 36 ++++++++++++++++++++++++++++++++++ lib/posix/write.c | 10 +++------- lib/stdio/fwrite.c | 4 ---- simple.sh | 6 ++++-- 5 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 lib/mes/write.c diff --git a/build-aux/configure-lib.sh b/build-aux/configure-lib.sh index 159aa11b..6871ab8e 100644 --- a/build-aux/configure-lib.sh +++ b/build-aux/configure-lib.sh @@ -29,47 +29,60 @@ fi . ./config.sh -libc_mini_SOURCES=" +libc_mini_shared_SOURCES=" lib/mes/eputs.c lib/mes/oputs.c " if test $mes_libc = mes; then - libc_mini_SOURCES="$libc_mini_SOURCES -lib/posix/write.c -lib/string/strlen.c -lib/stdlib/puts.c -lib/stdlib/exit.c + libc_mini_shared_SOURCES="$libc_mini_shared_SOURCES lib/$mes_kernel/$mes_cpu-mes-$compiler/mini.c +lib/stdlib/exit.c +lib/stdlib/puts.c +lib/string/strlen.c +" +fi + +libc_mini_SOURCES="$libc_mini_shared_SOURCES" + +if test $mes_libc = mes; then + libc_mini_SOURCES="$libc_mini_SOURCES +lib/mes/write.c " fi libmes_SOURCES=" -$libc_mini_SOURCES +$libc_mini_shared_SOURCES lib/ctype/isnumber.c lib/mes/abtol.c -lib/mes/itoa.c -lib/mes/ltoa.c -lib/mes/ltoab.c -lib/mes/ultoa.c -lib/mes/utoa.c lib/mes/eputc.c lib/mes/fdgetc.c lib/mes/fdputc.c lib/mes/fdputs.c lib/mes/fdungetc.c +lib/mes/itoa.c +lib/mes/ltoa.c +lib/mes/ltoab.c lib/mes/mes_open.c lib/mes/ntoab.c lib/mes/oputc.c +lib/mes/ultoa.c +lib/mes/utoa.c " if test $mes_libc = mes; then libmes_SOURCES="$libmes_SOURCES -lib/stdlib/atoi.c lib/ctype/isdigit.c lib/ctype/isspace.c lib/ctype/isxdigit.c +lib/posix/write.c +lib/stdlib/atoi.c " + if test $mes_kernel = linux; then + libmes_SOURCES="$libmes_SOURCES +lib/linux/lseek.c +" + fi else libmes_SOURCES="$libmes_SOURCES " @@ -192,7 +205,6 @@ lib/$mes_cpu-mes-$compiler/setjmp.c if test $mes_kernel = linux; then libc_tcc_SOURCES="$libc_tcc_SOURCES lib/linux/close.c -lib/linux/lseek.c lib/linux/rmdir.c lib/linux/stat.c " diff --git a/lib/mes/write.c b/lib/mes/write.c new file mode 100644 index 00000000..30344cad --- /dev/null +++ b/lib/mes/write.c @@ -0,0 +1,36 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include + +ssize_t +write (int filedes, void const *buffer, size_t size) +{ + int r = _write (filedes, buffer, size); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} diff --git a/lib/posix/write.c b/lib/posix/write.c index 6843cd55..c36b2a1c 100644 --- a/lib/posix/write.c +++ b/lib/posix/write.c @@ -18,21 +18,17 @@ * along with GNU Mes. If not, see . */ -#include +#include #include +#include +#include ssize_t write (int filedes, void const *buffer, size_t size) { -#if 0 // !MES_MINI - // FIXME: libc-mini has no __buffered_read_clear, lseek. - // and libc includes libc-mini...how to override? - // Let's hope everyone uses fwrite, or lseek for RDWR - // semantics... size_t skip = __buffered_read_clear (filedes); if (skip) lseek (filedes, -skip, SEEK_CUR); -#endif int r = _write (filedes, buffer, size); if (r < 0) { diff --git a/lib/stdio/fwrite.c b/lib/stdio/fwrite.c index 6e048ee2..2e2030bd 100644 --- a/lib/stdio/fwrite.c +++ b/lib/stdio/fwrite.c @@ -36,11 +36,7 @@ fwrite (void const *data, size_t size, size_t count, FILE * stream) if (!size || !count) return 0; - // FIXME: should be in write, but that's libc-mini. int filedes = (long) stream; - size_t skip = __buffered_read_clear (filedes); - if (skip) - lseek (filedes, -skip, SEEK_CUR); int bytes = write (filedes, data, size * count); if (__mes_debug () > 2) diff --git a/simple.sh b/simple.sh index 97e249e6..5107f7b4 100755 --- a/simple.sh +++ b/simple.sh @@ -93,7 +93,7 @@ MES_DEBUG=2 MES=out-system-libc/mes sh -x scripts/mescc -m $mes_bits -nostdlib\ \ lib/linux/$mes_cpu-mes-mescc/mini.c\ \ - lib/posix/write.c\ + lib/mes/write.c\ lib/string/strlen.c\ \ scaffold/hello.c @@ -197,6 +197,8 @@ $CC -g -D HAVE_CONFIG_H=1 -I include -I include/$mes_kernel/$mes_cpu\ lib/string/strcpy.c\ lib/string/strncmp.c\ \ + lib/linux/lseek.c\ + \ lib/linux/access.c\ lib/linux/brk.c\ lib/linux/chmod.c\ @@ -252,7 +254,7 @@ MES_DEBUG=2 MES=out-mes/mes sh -x scripts/mescc -m $mes_bits -nostdlib\ \ lib/linux/$mes_cpu-mes-mescc/mini.c\ \ - lib/posix/write.c\ + lib/mes/write.c\ lib/string/strlen.c\ \ scaffold/hello.c