diff --git a/ext/ioleast.h b/ext/ioleast.h index 55ce27c7..b60c9c8d 100644 --- a/ext/ioleast.h +++ b/ext/ioleast.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -36,6 +36,7 @@ #include +#include #include #include #include @@ -46,77 +47,83 @@ #endif __attribute__((unused)) static inline -size_t readleast(int fd, void* buf, size_t least, size_t max) +size_t readleast(int fd, void* buf_ptr, size_t least, size_t max) { - ssize_t amount = read(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += readleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = read(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t writeleast(int fd, const void* buf, size_t least, size_t max) +size_t writeleast(int fd, const void* buf_ptr, size_t least, size_t max) { - ssize_t amount = write(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += writeleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = write(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off) +size_t preadleast(int fd, void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pread(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += preadleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pread(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off) +size_t pwriteleast(int fd, const void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pwrite(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += pwriteleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pwrite(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline diff --git a/libc/ioleast/preadleast.cpp b/libc/ioleast/preadleast.cpp index b28d072c..b86a3232 100644 --- a/libc/ioleast/preadleast.cpp +++ b/libc/ioleast/preadleast.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -22,26 +22,28 @@ *******************************************************************************/ +#include #include #include #include #include extern "C" -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off) +size_t preadleast(int fd, void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pread(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += preadleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pread(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } diff --git a/libc/ioleast/pwriteleast.cpp b/libc/ioleast/pwriteleast.cpp index 04b13ea7..b33ad737 100644 --- a/libc/ioleast/pwriteleast.cpp +++ b/libc/ioleast/pwriteleast.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -22,26 +22,28 @@ *******************************************************************************/ +#include #include #include #include #include extern "C" -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off) +size_t pwriteleast(int fd, const void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pwrite(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += pwriteleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pwrite(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } diff --git a/libc/ioleast/readleast.cpp b/libc/ioleast/readleast.cpp index cf5dfd9b..0b0cd52f 100644 --- a/libc/ioleast/readleast.cpp +++ b/libc/ioleast/readleast.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -22,24 +22,28 @@ *******************************************************************************/ +#include #include #include #include #include -extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max) +extern "C" +size_t readleast(int fd, void* buf_ptr, size_t least, size_t max) { - ssize_t amount = read(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += readleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = read(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } diff --git a/libc/ioleast/writeleast.cpp b/libc/ioleast/writeleast.cpp index a27870bf..2db3435b 100644 --- a/libc/ioleast/writeleast.cpp +++ b/libc/ioleast/writeleast.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -22,24 +22,28 @@ *******************************************************************************/ +#include #include #include #include #include -extern "C" size_t writeleast(int fd, const void* buf, size_t least, size_t max) +extern "C" +size_t writeleast(int fd, const void* buf_ptr, size_t least, size_t max) { - ssize_t amount = write(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += writeleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = write(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } diff --git a/mkinitrd/ioleast.h b/mkinitrd/ioleast.h index 55ce27c7..b60c9c8d 100644 --- a/mkinitrd/ioleast.h +++ b/mkinitrd/ioleast.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015. This file is part of the Sortix C Library. @@ -36,6 +36,7 @@ #include +#include #include #include #include @@ -46,77 +47,83 @@ #endif __attribute__((unused)) static inline -size_t readleast(int fd, void* buf, size_t least, size_t max) +size_t readleast(int fd, void* buf_ptr, size_t least, size_t max) { - ssize_t amount = read(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += readleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = read(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t writeleast(int fd, const void* buf, size_t least, size_t max) +size_t writeleast(int fd, const void* buf_ptr, size_t least, size_t max) { - ssize_t amount = write(fd, buf, max); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - amount += writeleast(fd, nextbuf, nextleast, nextmax); - } - return amount; + ssize_t amount = write(fd, buf + done, max - done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off) +size_t preadleast(int fd, void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pread(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + unsigned char* buf = (unsigned char*) buf_ptr; + size_t done = 0; + do { - void* nextbuf = (uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += preadleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pread(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off) +size_t pwriteleast(int fd, const void* buf_ptr, size_t least, size_t max, off_t off) { - ssize_t amount = pwrite(fd, buf, max, off); - if ( amount < 0 ) - return 0; - if ( least && !amount ) - return errno = EEOF, 0; - if ( (size_t) amount < least ) + assert(least <= max); + const unsigned char* buf = (const unsigned char*) buf_ptr; + size_t done = 0; + do { - const void* nextbuf = (const uint8_t*) buf + amount; - size_t nextleast = least - amount; - size_t nextmax = max - amount; - off_t nextoff = off + amount; - amount += pwriteleast(fd, nextbuf, nextleast, nextmax, nextoff); - } - return amount; + ssize_t amount = pwrite(fd, buf + done, max - done, off + done); + if ( amount < 0 ) + return done; + if ( !amount && done < least ) + return errno = EEOF, done; + if ( !amount ) + break; + done += amount; + } while ( done < least ); + return done; } __attribute__((unused)) static inline