From ab16fcf76e616dfa7cac2cb3dd909ba2a8eb2a9f Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 5 Feb 2011 13:43:35 -0800 Subject: Slightly better errno support. --- libc/include/reent.h | 6 ++---- libc/include/stdio.h | 16 ++++++++-------- libc/src/close.c | 2 +- libc/src/lseek.c | 6 +++--- libc/src/open.c | 2 +- libc/src/qsort.c | 0 libc/src/read.c | 4 ++-- libc/src/write.c | 4 ++-- libc/src/xscanf.c | 0 os/src/sbrk.c | 2 +- 10 files changed, 20 insertions(+), 22 deletions(-) mode change 100755 => 100644 libc/src/qsort.c mode change 100755 => 100644 libc/src/xscanf.c diff --git a/libc/include/reent.h b/libc/include/reent.h index c3633ee..ddd19d3 100644 --- a/libc/include/reent.h +++ b/libc/include/reent.h @@ -7,10 +7,8 @@ struct _reent { enum errno_t _errno; }; -static inline void _REENT_INIT_PTR(struct _reent * reent) { - reent->_errno = ENOERROR; -} - +static inline void _REENT_INIT_PTR(struct _reent * reent) { reent->_errno = ENOERROR; } extern struct _reent * _impure_ptr; +static inline void set_errno(enum errno_t _errno) { _impure_ptr->_errno = errno = _errno; } #endif diff --git a/libc/include/stdio.h b/libc/include/stdio.h index a2655ea..9a8b491 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -57,7 +57,7 @@ static inline FILE * fopen(const char * fname, const char * mode) { FILE * r = NULL; int flags = 0, plus = 0, append = 0, fd; if (!mode || !mode[0]) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return NULL; } @@ -65,7 +65,7 @@ static inline FILE * fopen(const char * fname, const char * mode) { plus = mode[2] == '+'; } else if (mode[1]) { if (mode[1] != '+') { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return NULL; } plus = 1; @@ -95,7 +95,7 @@ static inline FILE * fopen(const char * fname, const char * mode) { } break; default: - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return NULL; } @@ -114,7 +114,7 @@ static inline int fclose(FILE * stream) { int fd; if (!stream) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } @@ -131,7 +131,7 @@ static inline size_t fread(void * _ptr, size_t size, size_t nmemb, FILE * stream size_t r; if (!stream) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } @@ -164,7 +164,7 @@ static inline size_t fwrite(const void * _ptr, size_t size, size_t nmemb, FILE * const uint8_t * ptr = (const uint8_t *) _ptr; if (!stream) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } @@ -186,7 +186,7 @@ static inline int fgetc(FILE * stream) { uint8_t v; if (!stream) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } @@ -200,7 +200,7 @@ static inline int fgetc(FILE * stream) { static inline int fseek(FILE * stream, off_t offset, int wheel) { int r; if (!stream) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } diff --git a/libc/src/close.c b/libc/src/close.c index fd4256f..f508a2c 100644 --- a/libc/src/close.c +++ b/libc/src/close.c @@ -4,7 +4,7 @@ int close(int fd) { if (!fio_is_open(fd)) { - _impure_ptr->_errno = EBADF; + set_errno(EBADF); return -1; } diff --git a/libc/src/lseek.c b/libc/src/lseek.c index bffc81f..1d36c90 100644 --- a/libc/src/lseek.c +++ b/libc/src/lseek.c @@ -6,19 +6,19 @@ off_t lseek(int fd, off_t seek, int wheel) { off_t r; if ((wheel != SEEK_SET) && (wheel != SEEK_CUR) && (wheel != SEEK_END)) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } if (!fio_is_open(fd)) { - _impure_ptr->_errno = EBADF; + set_errno(EBADF); return -1; } r = fio_seek(fd, seek, wheel); if (r < 0) - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return r; } diff --git a/libc/src/open.c b/libc/src/open.c index 71a96d1..78d40be 100644 --- a/libc/src/open.c +++ b/libc/src/open.c @@ -12,6 +12,6 @@ int open(const char * path, int flags) { if (r >= 0) return r; - _impure_ptr->_errno = EACCES; + set_errno(EACCES); return -1; } diff --git a/libc/src/qsort.c b/libc/src/qsort.c old mode 100755 new mode 100644 diff --git a/libc/src/read.c b/libc/src/read.c index aeea537..dcc03e8 100644 --- a/libc/src/read.c +++ b/libc/src/read.c @@ -6,14 +6,14 @@ ssize_t read(int fd, void * buf, size_t size) { ssize_t r; if (!fio_is_open(fd)) { - _impure_ptr->_errno = EBADF; + set_errno(EBADF); return -1; } r = fio_read(fd, buf, size); if (r < 0) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } diff --git a/libc/src/write.c b/libc/src/write.c index e44806c..52b1457 100644 --- a/libc/src/write.c +++ b/libc/src/write.c @@ -6,14 +6,14 @@ ssize_t write(int fd, const void * buf, size_t size) { ssize_t r; if (!fio_is_open(fd)) { - _impure_ptr->_errno = EBADF; + set_errno(EBADF); return -1; } r = fio_write(fd, buf, size); if (r < 0) { - _impure_ptr->_errno = EINVAL; + set_errno(EINVAL); return -1; } diff --git a/libc/src/xscanf.c b/libc/src/xscanf.c old mode 100755 new mode 100644 diff --git a/os/src/sbrk.c b/os/src/sbrk.c index fcb6b7a..e831d38 100644 --- a/os/src/sbrk.c +++ b/os/src/sbrk.c @@ -25,7 +25,7 @@ void * sbrk(ptrdiff_t incr) { /* Check if this allocation would exceed the end of the ram - would probably get into the stack first however */ if (next_heap_end > stack_min) { - _impure_ptr->_errno = ENOMEM; + set_errno(ENOMEM); ret = NULL; } else { heap_end = next_heap_end; -- cgit v1.2.3