diff --git a/libmaxsi/c/hsrc/features.h b/libmaxsi/c/hsrc/features.h index 8d0e28fc..4a2c9c26 100644 --- a/libmaxsi/c/hsrc/features.h +++ b/libmaxsi/c/hsrc/features.h @@ -43,9 +43,15 @@ #define __POSIX_OBSOLETE 200112L #endif -#include +/* Whether sortix-specific extensions to the C library are available. */ +#ifndef SORTIX_NO_EXTENSIONS +#define SORTIX_EXTENSIONS +#endif -// Don't provide things from standard headers that is not implemented in libmaxsi/sortix. +/* Don't provide things from standard headers that is not implemented. */ #define SORTIX_UNIMPLEMENTED +#include + + #endif diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index 079f7d4b..d538dfd8 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -15,7 +15,7 @@ more details. You should have received a copy of the GNU Lesser General Public License - along with LibMaxsi. If not, see . + along with LibMaxsi. If not, see . unistd.h The header defines miscellaneous symbolic constants and types, @@ -170,6 +170,9 @@ int usleep(useconds_t useconds); #endif int unlink(const char*); ssize_t write(int, const void*, size_t); +#ifdef SORTIX_EXTENSIONS +int writeall(int fd, const void* buffer, size_t len); +#endif __END_DECLS diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index 1eb06f3f..cd907fe1 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -26,7 +26,9 @@ #include "syscall.h" #include "io.h" #include "format.h" +#include "string.h" #include +#include namespace Maxsi { @@ -42,14 +44,17 @@ namespace Maxsi DEFN_SYSCALL2(char*, SysGetCWD, 26, char*, size_t); DEFN_SYSCALL1(int, SysUnlink, 27, const char*); - size_t Print(const char* Message) + size_t Print(const char* string) { - return SysPrint(Message); + size_t stringlen = String::Length(string); + if ( writeall(1, string, stringlen) ) { return 0; } + return stringlen; } size_t PrintCallback(void* user, const char* string, size_t stringlen) { - return SysPrint(string); + if ( writeall(1, string, stringlen) ) { return 0; } + return stringlen; } size_t PrintF(const char* format, ...) @@ -81,6 +86,20 @@ namespace Maxsi return SysWrite(fd, buf, count); } + extern "C" int writeall(int fd, const void* buffer, size_t len) + { + const char* buf = (const char*) buffer; + while ( len ) + { + ssize_t byteswritten = write(fd, buf, len); + if ( byteswritten < 0 ) { return (int) byteswritten; } + buf += byteswritten; + len -= byteswritten; + } + + return 0; + } + extern "C" int pipe(int pipefd[2]) { return SysPipe(pipefd); diff --git a/utils/cat.cpp b/utils/cat.cpp index 8722535e..e98475b5 100644 --- a/utils/cat.cpp +++ b/utils/cat.cpp @@ -5,20 +5,6 @@ #include #include -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - int cat(int argc, char* argv[]) { int result = 0; @@ -48,7 +34,7 @@ int cat(int argc, char* argv[]) result = 1; break; } - if ( !writeall(outfd, buffer, bytesread) ) + if ( writeall(outfd, buffer, bytesread) ) { printf("%s: /dev/tty: %s\n", argv[0], strerror(errno)); result = 1; diff --git a/utils/cp.cpp b/utils/cp.cpp index 974235ca..acbbba4f 100644 --- a/utils/cp.cpp +++ b/utils/cp.cpp @@ -4,20 +4,6 @@ #include #include -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - const char* basename(const char* path) { size_t len = strlen(path); @@ -62,6 +48,6 @@ int main(int argc, char* argv[]) ssize_t bytesread = read(fromfd, buffer, BUFFER_SIZE); if ( bytesread < 0 ) { printf("%s: %s: %s\n", argv[0], frompath, strerror(errno)); return 1; } if ( bytesread == 0 ) { return 0; } - if ( !writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; } + if ( writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; } } } diff --git a/utils/editor.cpp b/utils/editor.cpp index a242d906..ac16bf34 100644 --- a/utils/editor.cpp +++ b/utils/editor.cpp @@ -192,20 +192,6 @@ unsigned confirmquit() } } -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - bool savetofile(const char* path) { int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0777); @@ -215,7 +201,7 @@ bool savetofile(const char* path) { size_t len = strlen(buffers[y]); buffers[y][len] = '\n'; - bool result = writeall(fd, buffers[y], len+1); + bool result = !writeall(fd, buffers[y], len+1); buffers[y][len] = 0; if ( !result ) { printf("%s: %s\n", path, strerror(errno)); close(fd); return false; } } diff --git a/utils/init.cpp b/utils/init.cpp index 1c83839c..b9aaf4c4 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,10 @@ int child() int main(int argc, char* argv[]) { + if ( open("/dev/tty", O_RDONLY) != 0 ) { return 2; } + if ( open("/dev/tty", O_WRONLY | O_APPEND) != 1 ) { return 2; } + if ( open("/dev/tty", O_WRONLY | O_APPEND) != 2 ) { return 2; } + // Reset the terminal's color and the rest of it. printf("\r\e[m\e[J");