libc read()/write() syscalls - return EBADF if fd is not stdin/stdout as applicable

As discussed in #41

Also fixes indent in _read_r
This commit is contained in:
Angus Gratton 2015-09-03 11:14:44 +10:00
parent 7e30f48650
commit 568ebc0fdc

View file

@ -6,6 +6,7 @@
*/ */
#include <sys/reent.h> #include <sys/reent.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/errno.h>
#include <espressif/sdk_private.h> #include <espressif/sdk_private.h>
#include <common_macros.h> #include <common_macros.h>
#include <stdlib.h> #include <stdlib.h>
@ -37,6 +38,10 @@ IRAM caddr_t _sbrk_r (struct _reent *r, int incr)
*/ */
long _write_r(struct _reent *r, int fd, const char *ptr, int len ) long _write_r(struct _reent *r, int fd, const char *ptr, int len )
{ {
if(fd != r->_stdout->_file) {
r->_errno = EBADF;
return -1;
}
for(int i = 0; i < len; i++) for(int i = 0; i < len; i++)
sdk_os_putc(ptr[i]); sdk_os_putc(ptr[i]);
return len; return len;
@ -48,13 +53,16 @@ long _write_r(struct _reent *r, int fd, const char *ptr, int len )
*/ */
long _read_r( struct _reent *r, int fd, char *ptr, int len ) long _read_r( struct _reent *r, int fd, char *ptr, int len )
{ {
for(int i = 0; i < len; i++) { if(fd != r->_stdin->_file) {
char ch; r->_errno = EBADF;
while (sdk_uart_rx_one_char(&ch)) ; return -1;
ptr[i] = ch; }
for(int i = 0; i < len; i++) {
} char ch;
return len; while (sdk_uart_rx_one_char(&ch)) ;
ptr[i] = ch;
}
return len;
} }
/* These are stub implementations for the reentrant syscalls that /* These are stub implementations for the reentrant syscalls that