From 57510da98a112699188012b8505136d58594c274 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 22 Dec 2012 09:42:44 +0100 Subject: digital/ucoolib/ucoolib/arch: implement read and write --- digital/ucoolib/ucoolib/arch/syscalls.newlib.cc | 55 +++++++++++++++++++++++-- digital/ucoolib/ucoolib/arch/syscalls.newlib.hh | 35 ++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 digital/ucoolib/ucoolib/arch/syscalls.newlib.hh (limited to 'digital') diff --git a/digital/ucoolib/ucoolib/arch/syscalls.newlib.cc b/digital/ucoolib/ucoolib/arch/syscalls.newlib.cc index f18712cc..02dd26f7 100644 --- a/digital/ucoolib/ucoolib/arch/syscalls.newlib.cc +++ b/digital/ucoolib/ucoolib/arch/syscalls.newlib.cc @@ -22,11 +22,14 @@ * * }}} */ #include "ucoolib/common.hh" +#include "syscalls.newlib.hh" #include #include #include +ucoo::Stream *ucoo::syscalls_streams[3]; + /** This is needed by C++ ABI, this simple definition will do. See: * http://lists.debian.org/debian-gcc/2003/07/msg00057.html */ void *__dso_handle = (void*) &__dso_handle; @@ -117,14 +120,60 @@ _open_r (struct _reent *ptr, const char *file, int flags, int mode) extern "C" int _read_r (struct _reent *ptr, int fd, void *buf, size_t cnt) { - return 0; + if (fd == 0) + { + if (ucoo::syscalls_streams[0]) + { + ucoo::Stream &s = *ucoo::syscalls_streams[0]; + int r = s.read (reinterpret_cast (buf), cnt); + switch (r) + { + case -2: + return 0; + case -1: + ptr->_errno = EIO; + return -1; + case 0: + ptr->_errno = EAGAIN; + return -1; + default: + return r; + } + } + else + return 0; + } + else + { + ptr->_errno = EBADF; + return -1; + } } /** Write to file, to be improved to write to stream. */ extern "C" int _write_r (struct _reent *ptr, int fd, const void *buf, size_t cnt) { - ptr->_errno = EBADF; - return -1; + if ((fd == 1 || fd == 2) && ucoo::syscalls_streams[fd]) + { + ucoo::Stream &s = *ucoo::syscalls_streams[fd]; + int r = s.write (reinterpret_cast (buf), cnt); + switch (r) + { + case -1: + ptr->_errno = EIO; + return -1; + case 0: + ptr->_errno = EAGAIN; + return -1; + default: + return r; + } + } + else + { + ptr->_errno = EBADF; + return -1; + } } diff --git a/digital/ucoolib/ucoolib/arch/syscalls.newlib.hh b/digital/ucoolib/ucoolib/arch/syscalls.newlib.hh new file mode 100644 index 00000000..be83e85e --- /dev/null +++ b/digital/ucoolib/ucoolib/arch/syscalls.newlib.hh @@ -0,0 +1,35 @@ +#ifndef ucoolib_arch_syscalls_newlib_hh +#define ucoolib_arch_syscalls_newlib_hh +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2012 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "ucoolib/intf/stream.hh" + +namespace ucoo { + +// Streams used as stdin, stdout and stderr. +extern Stream *syscalls_streams[3]; + +} // namespace ucoo + +#endif // ucoolib_arch_syscalls_newlib_hh -- cgit v1.2.3