diff --git a/libc/Makefile b/libc/Makefile index 59fa2b92..db355cea 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -237,6 +237,14 @@ getopt/getopt_long.o \ getopt/getopt.o \ grp/grent.o \ init/init.o \ +ioleast/preadall.o \ +ioleast/preadleast.o \ +ioleast/pwriteall.o \ +ioleast/pwriteleast.o \ +ioleast/readall.o \ +ioleast/readleast.o \ +ioleast/writeall.o \ +ioleast/writeleast.o \ libgen/basename.o \ libgen/dirname.o \ locale/localeconv.o \ @@ -430,7 +438,6 @@ unistd/getpgid.o \ unistd/getpid.o \ unistd/getppid.o \ unistd/getuid.o \ -unistd/ioleast.o \ unistd/isatty.o \ unistd/lchown.o \ unistd/linkat.o \ diff --git a/libc/include/ioleast.h b/libc/include/ioleast.h new file mode 100644 index 00000000..3880a250 --- /dev/null +++ b/libc/include/ioleast.h @@ -0,0 +1,51 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast.h + Versions of {,p}{read,write} that don't return until it has returned as much + data as requested, end of file, or an error occurs. This is sometimes needed + as read(2) and write(2) is not always guaranteed to fill up the entire + buffer or write it all. + +*******************************************************************************/ + +#ifndef INCLUDE_IOLEAST_H +#define INCLUDE_IOLEAST_H + +#include + +#include + +__BEGIN_DECLS + +@include(off_t.h) +@include(size_t.h) + +size_t preadall(int fd, void* buf, size_t count, off_t off); +size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off); +size_t pwriteall(int fd, const void* buf, size_t count, off_t off); +size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off); +size_t readall(int fd, void* buf, size_t count); +size_t readleast(int fd, void* buf, size_t least, size_t max); +size_t writeall(int fd, const void* buf, size_t count); +size_t writeleast(int fd, const void* buf, size_t least, size_t max); + +__END_DECLS + +#endif diff --git a/libc/include/unistd.h b/libc/include/unistd.h index cfa16d4c..4639916e 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -41,6 +41,7 @@ __BEGIN_DECLS @include(time_t.h) __END_DECLS #include +#include #endif #include #include @@ -366,12 +367,6 @@ int getdtablesize(void); size_t getpagesize(void); int memstat(size_t* memused, size_t* memtotal); int mkpartition(int fd, off_t start, off_t length); -size_t preadall(int fd, void* buf, size_t count, off_t off); -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off); -size_t pwriteall(int fd, const void* buf, size_t count, off_t off); -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off); -size_t readall(int fd, void* buf, size_t count); -size_t readleast(int fd, void* buf, size_t least, size_t max); pid_t sfork(int flags); pid_t tfork(int flags, tforkregs_t* regs); size_t writeall(int fd, const void* buf, size_t count); diff --git a/libc/ioleast/preadall.cpp b/libc/ioleast/preadall.cpp new file mode 100644 index 00000000..277dfc8b --- /dev/null +++ b/libc/ioleast/preadall.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/preadall.cpp + Reads as much data as requested from the given offset. + +*******************************************************************************/ + +#include + +extern "C" size_t preadall(int fd, void* buf, size_t count, off_t off) +{ + return preadleast(fd, buf, count, count, off); +} diff --git a/libc/ioleast/preadleast.cpp b/libc/ioleast/preadleast.cpp new file mode 100644 index 00000000..b28d072c --- /dev/null +++ b/libc/ioleast/preadleast.cpp @@ -0,0 +1,47 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/preadleast.cpp + Reads at least as much data as requested or more from the given offset. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" +size_t preadleast(int fd, void* buf, 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 ) + { + 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; +} diff --git a/libc/ioleast/pwriteall.cpp b/libc/ioleast/pwriteall.cpp new file mode 100644 index 00000000..3da542e9 --- /dev/null +++ b/libc/ioleast/pwriteall.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/pwriteall.cpp + Writes as much data as requested at the given offset. + +*******************************************************************************/ + +#include + +extern "C" size_t pwriteall(int fd, const void* buf, size_t count, off_t off) +{ + return pwriteleast(fd, buf, count, count, off); +} diff --git a/libc/ioleast/pwriteleast.cpp b/libc/ioleast/pwriteleast.cpp new file mode 100644 index 00000000..04b13ea7 --- /dev/null +++ b/libc/ioleast/pwriteleast.cpp @@ -0,0 +1,47 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/pwriteleast.cpp + Writes at least as much data as requested or more at the given offset. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" +size_t pwriteleast(int fd, const void* buf, 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 ) + { + 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; +} diff --git a/libc/ioleast/readall.cpp b/libc/ioleast/readall.cpp new file mode 100644 index 00000000..1bd57416 --- /dev/null +++ b/libc/ioleast/readall.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/readall.cpp + Reads as much data as requested. + +*******************************************************************************/ + +#include + +extern "C" size_t readall(int fd, void* buf, size_t count) +{ + return readleast(fd, buf, count, count); +} diff --git a/libc/ioleast/readleast.cpp b/libc/ioleast/readleast.cpp new file mode 100644 index 00000000..cf5dfd9b --- /dev/null +++ b/libc/ioleast/readleast.cpp @@ -0,0 +1,45 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/readleast.cpp + Reads at least as much data as requested or more. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" size_t readleast(int fd, void* buf, 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 ) + { + void* nextbuf = (uint8_t*) buf + amount; + size_t nextleast = least - amount; + size_t nextmax = max - amount; + amount += readleast(fd, nextbuf, nextleast, nextmax); + } + return amount; +} diff --git a/libc/ioleast/writeall.cpp b/libc/ioleast/writeall.cpp new file mode 100644 index 00000000..cc3abb55 --- /dev/null +++ b/libc/ioleast/writeall.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/writeall.cpp + Writes as much data as requested. + +*******************************************************************************/ + +#include + +extern "C" size_t writeall(int fd, const void* buf, size_t count) +{ + return writeleast(fd, buf, count, count); +} diff --git a/libc/ioleast/writeleast.cpp b/libc/ioleast/writeleast.cpp new file mode 100644 index 00000000..a27870bf --- /dev/null +++ b/libc/ioleast/writeleast.cpp @@ -0,0 +1,45 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + ioleast/writeleast.cpp + Writes at least as much data as requested or more. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" size_t writeleast(int fd, const void* buf, 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 ) + { + 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; +} diff --git a/mkinitrd/Makefile b/mkinitrd/Makefile index 1067996b..310507d2 100644 --- a/mkinitrd/Makefile +++ b/mkinitrd/Makefile @@ -11,9 +11,8 @@ ifndef CXXFLAGS endif SORTIXKERNEL=../sortix -LIBC=../libc -CPPFLAGS:=$(CPPFLAGS) -I$(SORTIXKERNEL)/include +CPPFLAGS:=$(CPPFLAGS) -I$(SORTIXKERNEL)/include -I. CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti BINARIES=mkinitrd initrdfs @@ -24,8 +23,8 @@ all: $(BINARIES) .PHONY: all install uninstall clean -%: %.cpp crc32.cpp rules.cpp $(LIBC)/unistd/ioleast.cpp - $(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< crc32.cpp rules.cpp $(LIBC)/unistd/ioleast.cpp -o $@ +%: %.cpp crc32.cpp rules.cpp + $(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< crc32.cpp rules.cpp -o $@ clean: rm -f $(BINARIES) diff --git a/mkinitrd/initrdfs.cpp b/mkinitrd/initrdfs.cpp index ab5708fd..c8f32198 100644 --- a/mkinitrd/initrdfs.cpp +++ b/mkinitrd/initrdfs.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -35,19 +36,6 @@ #include "crc32.h" -#if !defined(sortix) -__BEGIN_DECLS -size_t preadall(int fd, void* buf, size_t count, off_t off); -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off); -size_t pwriteall(int fd, const void* buf, size_t count, off_t off); -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off); -size_t readall(int fd, void* buf, size_t count); -size_t readleast(int fd, void* buf, size_t least, size_t max); -size_t writeall(int fd, const void* buf, size_t count); -size_t writeleast(int fd, const void* buf, size_t least, size_t max); -__END_DECLS -#endif - char* Substring(const char* str, size_t start, size_t length) { char* result = (char*) malloc(length+1); diff --git a/libc/unistd/ioleast.cpp b/mkinitrd/ioleast.h similarity index 64% rename from libc/unistd/ioleast.cpp rename to mkinitrd/ioleast.h index 7f1835b9..55ce27c7 100644 --- a/libc/unistd/ioleast.cpp +++ b/mkinitrd/ioleast.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2013. This file is part of the Sortix C Library. @@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - unistd/ioleast.cpp + ioleast.h Versions of {,p}{read,write} that don't return until it has returned as much data as requested, end of file, or an error occurs. This is sometimes needed as read(2) and write(2) is not always guaranteed to fill up the entire @@ -25,20 +25,34 @@ *******************************************************************************/ +#ifndef SORTIX_COMPATIBILITY_INCLUDE_IOLEAST_H +#define SORTIX_COMPATIBILITY_INCLUDE_IOLEAST_H + +#if defined(__sortix__) || defined(__sortix_libc__) + +#include_next + +#else + #include + +#include +#include #include #include -#include -#ifndef EEOF +#if !defined(EEOF) && defined(EIO) #define EEOF EIO #endif -extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max) +__attribute__((unused)) static inline +size_t readleast(int fd, void* buf, size_t least, size_t max) { ssize_t amount = read(fd, buf, max); - if ( amount < 0 ) { return 0; } - if ( least && !amount ) { errno = EEOF; return 0; } + if ( amount < 0 ) + return 0; + if ( least && !amount ) + return errno = EEOF, 0; if ( (size_t) amount < least ) { void* nextbuf = (uint8_t*) buf + amount; @@ -49,17 +63,32 @@ extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max) return amount; } -extern "C" size_t readall(int fd, void* buf, size_t count) +__attribute__((unused)) static inline +size_t writeleast(int fd, const void* buf, size_t least, size_t max) { - return readleast(fd, buf, count, count); + ssize_t amount = write(fd, buf, max); + if ( amount < 0 ) + return 0; + if ( least && !amount ) + return errno = EEOF, 0; + if ( (size_t) amount < least ) + { + 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; } -extern "C" size_t preadleast(int fd, void* buf, size_t least, size_t max, - off_t off) +__attribute__((unused)) static inline +size_t preadleast(int fd, void* buf, 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 ) { errno = EEOF; return 0; } + if ( amount < 0 ) + return 0; + if ( least && !amount ) + return errno = EEOF, 0; if ( (size_t) amount < least ) { void* nextbuf = (uint8_t*) buf + amount; @@ -71,37 +100,14 @@ extern "C" size_t preadleast(int fd, void* buf, size_t least, size_t max, return amount; } -extern "C" size_t preadall(int fd, void* buf, size_t count, off_t off) -{ - return preadleast(fd, buf, count, count, off); -} - -extern "C" size_t writeleast(int fd, const void* buf, size_t least, size_t max) -{ - ssize_t amount = write(fd, buf, max); - if ( amount < 0 ) { return 0; } - if ( least && !amount ) { errno = EEOF; return 0; } - if ( (size_t) amount < least ) - { - 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; -} - -extern "C" size_t writeall(int fd, const void* buf, size_t count) -{ - return writeleast(fd, buf, count, count); -} - -extern "C" size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, - off_t off) +__attribute__((unused)) static inline +size_t pwriteleast(int fd, const void* buf, 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 ) { errno = EEOF; return 0; } + if ( amount < 0 ) + return 0; + if ( least && !amount ) + return errno = EEOF, 0; if ( (size_t) amount < least ) { const void* nextbuf = (const uint8_t*) buf + amount; @@ -113,7 +119,30 @@ extern "C" size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, return amount; } -extern "C" size_t pwriteall(int fd, const void* buf, size_t count, off_t off) +__attribute__((unused)) static inline +size_t readall(int fd, void* buf, size_t count) +{ + return readleast(fd, buf, count, count); +} + +__attribute__((unused)) static inline +size_t writeall(int fd, const void* buf, size_t count) +{ + return writeleast(fd, buf, count, count); +} + +__attribute__((unused)) static inline +size_t preadall(int fd, void* buf, size_t count, off_t off) +{ + return preadleast(fd, buf, count, count, off); +} + +__attribute__((unused)) static inline +size_t pwriteall(int fd, const void* buf, size_t count, off_t off) { return pwriteleast(fd, buf, count, count, off); } + +#endif + +#endif diff --git a/mkinitrd/mkinitrd.cpp b/mkinitrd/mkinitrd.cpp index 6aa269e1..3b3cf389 100644 --- a/mkinitrd/mkinitrd.cpp +++ b/mkinitrd/mkinitrd.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -38,19 +39,6 @@ #include "crc32.h" #include "rules.h" -#if !defined(sortix) -__BEGIN_DECLS -size_t preadall(int fd, void* buf, size_t count, off_t off); -size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off); -size_t pwriteall(int fd, const void* buf, size_t count, off_t off); -size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off); -size_t readall(int fd, void* buf, size_t count); -size_t readleast(int fd, void* buf, size_t least, size_t max); -size_t writeall(int fd, const void* buf, size_t count); -size_t writeleast(int fd, const void* buf, size_t least, size_t max); -__END_DECLS -#endif - uint32_t HostModeToInitRD(mode_t mode) { uint32_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.